-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.axaml.cs
275 lines (208 loc) · 10.6 KB
/
MainWindow.axaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
using Avalonia.Controls;
using Avalonia.Interactivity;
using MsgBox;
using System.IO;
using SharedMethods;
using System.Collections.Generic;
using S3_SimpleBackup.Models;
using System.Collections.ObjectModel;
using static System.Reflection.Metadata.BlobBuilder;
using System.Collections;
using System.Threading.Tasks;
using System;
using System.Diagnostics;
using DataProtection;
namespace S3_SimpleBackup
{
public partial class MainWindow : Window
{
S3Methods s3Methods = new S3Methods();
ObservableCollection<BackupJobModel> _Jobs;
public MainWindow()
{
InitializeComponent();
LoadJobConfiguration();
tabDev.IsVisible = AppConfig.ShowDevWindow;
if (DoInputOutput.LoadS3Config(this))
{
edtS3Host.Text = S3Config.Host;
edtAccessKeyID.Text = S3Config.AccessKey;
edtSecretAccessKey.Text = S3Config.SecretKey;
chckbxRequireLogin.IsChecked = S3Config.EncryptSecretKey;
}
}
private async void btnTestConnection_Clicked(object? sender, RoutedEventArgs args)
{
if ((edtS3Host.Text != string.Empty) && (edtAccessKeyID.Text != string.Empty) && (edtSecretAccessKey.Text != string.Empty) && (dev_edtBucketName.Text != string.Empty))
{
dev_btnTestConnection.Content = "Testing Connection...";
string ConnectionSuccess = await s3Methods.Test_BucketConnectionAsync(edtS3Host.Text, edtAccessKeyID.Text, Protect.ConvertToSecureString(edtSecretAccessKey.Text), dev_edtBucketName.Text, this);
dev_btnTestConnection.Content = "Test Connection";
if (!ConnectionSuccess.Contains("Error"))
{
await MessageBox.Show(frmMain, $"Successfully listed objects in bucket: {dev_edtBucketName.Text}", "Connection Verified", MessageBox.MessageBoxButtons.Ok);
}
else
{
await MessageBox.Show(frmMain, ConnectionSuccess, "Connection Failed", MessageBox.MessageBoxButtons.Ok);
}
}
}
private async void btnSaveS3Config_Clicked(object? sender, RoutedEventArgs args)
{
S3Config.Host = edtS3Host.Text;
S3Config.AccessKey = edtAccessKeyID.Text;
S3Config.SecretKey = edtSecretAccessKey.Text;
S3Config.EncryptSecretKey = chckbxRequireLogin.IsChecked == true ? true : false;
DoInputOutput.WriteS3Config(this);
}
private async void btnUploadTestFile_Clicked(object? sender, RoutedEventArgs args)
{
bool UploadSuccess = false;
OpenFileDialog dlgFiletoUpload = new OpenFileDialog { Title = "Select a file to upload as a test", AllowMultiple = false };
string[] outPathStrings = await dlgFiletoUpload.ShowAsync(frmMain);
if (outPathStrings != null)
{
UploadSuccess = await s3Methods.Test_UploadTestFile(edtS3Host.Text, edtAccessKeyID.Text, Protect.ConvertToSecureString(edtSecretAccessKey.Text), dev_edtBucketName.Text, outPathStrings[0], this);
}
if (UploadSuccess)
{
await MessageBox.Show(frmMain, $"Successfully uploaded file to bucket:\n {dev_edtBucketName.Text}", "File Uploaded", MessageBox.MessageBoxButtons.Ok);
Output.WriteToUI($"[TEST] Successfully uploaded file to bucket: {dev_edtBucketName.Text}", this);
}
else
{
await MessageBox.Show(frmMain, $"Failed to upload file to bucket:\n {dev_edtBucketName.Text}", "Upload Failed", MessageBox.MessageBoxButtons.Ok);
Output.WriteToUI($"[TEST] Failed to upload file to bucket: {dev_edtBucketName.Text}", this);
}
}
private async void btnEmptyBucket_Clicked(object? sender, RoutedEventArgs args)
{
var confirmDelete = await MessageBox.Show(this, $"This will delete everything in bucket {dev_edtBucketName.Text}\nWould you like to proceed?\n\nTHIS CANNOT BE UNDONE!", "Important Warning!", MessageBox.MessageBoxButtons.YesNo);
if (confirmDelete == MessageBox.MessageBoxResult.Yes)
{
Output.WriteToUI($"First confirm received to empty bucket {dev_edtBucketName.Text}", this);
var secondConfirmDelete = await MessageBox.Show(this, $"Please confirm again that you'd like to empty bucket:\n{dev_edtBucketName.Text}\n\nThis is the final warning!", "Final Warning!", MessageBox.MessageBoxButtons.YesNo);
if (secondConfirmDelete == MessageBox.MessageBoxResult.Yes)
{
Output.WriteToUI($"Second confirm received to empty bucket {dev_edtBucketName.Text}", this);
s3Methods.DeleteAllObjectsinBucket(edtS3Host.Text, edtAccessKeyID.Text, Protect.ConvertToSecureString(edtSecretAccessKey.Text), edtEmptyBucketTarget.Text, this);
}
}
}
private async void dev_btnTestMsgBox_Clicked(object? sender, RoutedEventArgs args)
{
await MessageBox.Show(this, "This is a custom messagebox test\nThis is a second line", "Test MessageBox", MessageBox.MessageBoxButtons.Ok);
}
private async void dev_btnTestEncrytion_Clicked(object? sender, RoutedEventArgs args)
{
string unEncryptedString = "This is a test string";
await MessageBox.Show(this, $"String to encrypt \n{unEncryptedString}", "", MessageBox.MessageBoxButtons.Ok);
await MessageBox.Show(this, $"Encrypted String \n{UnProtect.ConvertToInsecureString(Protect.EncryptString(unEncryptedString))}", "", MessageBox.MessageBoxButtons.Ok);
}
private void chckbxEnableEmptyBucketTool_CheckChanged(object sender, RoutedEventArgs e)
{
edtEmptyBucketTarget.IsEnabled = chckbxEnableEmptyBucketTool.IsChecked ?? false;
btnEmptyBucket.IsEnabled = chckbxEnableEmptyBucketTool.IsChecked ?? false;
}
private async void dev_btnTestSourceListing_Clicked(object? sender, RoutedEventArgs args)
{
OpenFolderDialog folderDialog = new OpenFolderDialog();
string selectedPath = await folderDialog.ShowAsync(this);
List<FileInformation> sourceFileInfo = new List<FileInformation>();
sourceFileInfo = FileInteraction.FileIndexFromPath(selectedPath, true, true);
foreach (var item in sourceFileInfo)
{
Debug.WriteLine($"{item.ObjectName}, {item.FQPath}, {item.isDirectory}");
}
}
private async void btnNewjob_Clicked(object? sender, RoutedEventArgs args)
{
List<string> profiles = new List<string>();
foreach (var job in _Jobs)
{
if (!profiles.Contains(job.JobProfile))
{
profiles.Add(job.JobProfile);
}
}
JobManager jobManagerWindow = new JobManager("new", null, profiles);
await jobManagerWindow.ShowDialog(this);
_Jobs.Clear();
dbgJobsList.Items = null;
LoadJobConfiguration();
}
private async void btnEditjob_Clicked(object? sender, RoutedEventArgs args)
{
if (dbgJobsList.SelectedIndex > -1)
{
List<string> profiles = new List<string>();
foreach (var job in _Jobs)
{
if (!profiles.Contains(job.JobProfile))
{
profiles.Add(job.JobProfile);
}
}
JobManager jobManagerWindow = new JobManager("edit", _Jobs[dbgJobsList.SelectedIndex], profiles);
await jobManagerWindow.ShowDialog(this);
_Jobs.Clear();
dbgJobsList.Items = null;
LoadJobConfiguration();
}
}
private async void btnDeleteJob_Clicked(object? sender, RoutedEventArgs args)
{
try
{
BackupJobModel selectedJob = dbgJobsList.SelectedItem as BackupJobModel;
var confirmDelete = await MessageBox.Show(this, $"This will delete job {selectedJob.JobName}\nWould you like to proceed?\n\nTHIS CANNOT BE UNDONE!", "Important Warning!", MessageBox.MessageBoxButtons.YesNo);
if (confirmDelete == MessageBox.MessageBoxResult.Yes)
{
await DoInputOutput.DeleteJobAsync(_Jobs[dbgJobsList.SelectedIndex], this);
_Jobs.Clear();
dbgJobsList.Items = null;
LoadJobConfiguration();
}
}
catch (NullReferenceException e)
{
await MessageBox.Show(this, "Select an item to delete first", "No item selected", MessageBox.MessageBoxButtons.Ok);
}
}
private async void btnRunjob_Clicked(object? sender, RoutedEventArgs args)
{
BackupJobModel Src = dbgJobsList.SelectedItem as BackupJobModel;
Output.WriteToUI($"Starting Job {Src.JobName}", this);
s3Methods.UploadToS3(edtS3Host.Text, edtAccessKeyID.Text, Protect.ConvertToSecureString(edtSecretAccessKey.Text), Src.SourceFileFolder, Src.S3BucketName, Src.JobName, Src.JobParameters.Contains("/r"), this);
}
private async void btnRunAllJob_Clicked(object? sender, RoutedEventArgs args)
{
foreach (var item in dbgJobsList.Items)
{
BackupJobModel Src = item as BackupJobModel;
s3Methods.UploadToS3(edtS3Host.Text, edtAccessKeyID.Text, Protect.ConvertToSecureString(edtSecretAccessKey.Text), Src.SourceFileFolder, Src.S3BucketName, Src.JobName, Src.JobParameters.Contains("/r"), this);
}
}
private void LoadJobConfiguration()
{
#region Load Settings
DoInputOutput.LoadSettings();
Debug.WriteLine(AppConfig.ProfileStorageLocation);
#endregion
#region Load Jobs
Output.WriteToUI($"Current Profile Location: {AppConfig.ProfileStorageLocation}", this);
try
{
_Jobs = new ObservableCollection<BackupJobModel>(DoInputOutput.LoadJobsFiles(this));
}
catch (System.Exception e)
{
Output.WriteToUI($"An error occurred while loading profiles: {e.ToString()}", this);
}
dbgJobsList.AutoGenerateColumns = true;
dbgJobsList.Items = _Jobs;
#endregion
}
}
}