-
Notifications
You must be signed in to change notification settings - Fork 31
/
FileExporterImporter.cs
408 lines (349 loc) · 15.1 KB
/
FileExporterImporter.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
using System.Text;
using System.Text.RegularExpressions;
using Avalonia.Platform.Storage;
using Pororoca.Desktop.Localization;
using Pororoca.Desktop.ViewModels;
using Pororoca.Desktop.Views;
using static Pororoca.Domain.Features.ExportCollection.PororocaCollectionExporter;
using static Pororoca.Domain.Features.ExportCollection.PostmanCollectionV21Exporter;
using static Pororoca.Domain.Features.ExportEnvironment.PororocaEnvironmentExporter;
using static Pororoca.Domain.Features.ExportEnvironment.PostmanEnvironmentExporter;
using static Pororoca.Domain.Features.ImportCollection.PororocaCollectionImporter;
using static Pororoca.Domain.Features.ImportCollection.PostmanCollectionV21Importer;
using static Pororoca.Domain.Features.ImportEnvironment.PororocaEnvironmentImporter;
using static Pororoca.Domain.Features.ImportEnvironment.PostmanEnvironmentImporter;
namespace Pororoca.Desktop.ExportImport;
internal static partial class FileExporterImporter
{
internal const string PororocaCollectionExtension = "pororoca_collection.json";
internal const string PostmanCollectionExtension = "postman_collection.json";
internal const string PororocaEnvironmentExtension = "pororoca_environment.json";
internal const string PostmanEnvironmentExtension = "postman_environment.json";
private const string PororocaCollectionExtensionGlob = $"*.{PororocaCollectionExtension}";
private const string PostmanCollectionExtensionGlob = $"*.{PostmanCollectionExtension}";
private const string PororocaEnvironmentExtensionGlob = $"*.{PororocaEnvironmentExtension}";
private const string PostmanEnvironmentExtensionGlob = $"*.{PostmanEnvironmentExtension}";
private const string JsonExtensionGlob = $"*.json";
private static readonly Regex pororocaSchemaRegex = GeneratePororocaSchemaRegex();
[GeneratedRegex("schema\":\\s*\"Pororoca")]
private static partial Regex GeneratePororocaSchemaRegex();
#region EXPORT COLLECTION
internal static Task ExportCollectionAsync(CollectionViewModel cvm)
{
FilePickerSaveOptions opts = new()
{
Title = Localizer.Instance.Collection.ExportCollectionDialogTitle,
FileTypeChoices = new List<FilePickerFileType>()
{
new(Localizer.Instance.Collection.PororocaCollectionFormat)
{
Patterns = new List<string> { PororocaCollectionExtensionGlob }
},
new(Localizer.Instance.Collection.PostmanCollectionFormat)
{
Patterns = new List<string> { PostmanCollectionExtensionGlob }
}
}
};
if (OperatingSystem.IsWindows())
{
opts.DefaultExtension = PororocaCollectionExtensionGlob;
}
return ShowExportCollectionDialogAsync(cvm, opts);
}
internal static Task ExportAsPororocaCollectionAsync(CollectionViewModel cvm)
{
FilePickerSaveOptions opts = new()
{
Title = Localizer.Instance.Collection.ExportAsPororocaCollectionDialogTitle,
SuggestedFileName = $"{cvm.Name}.{PororocaCollectionExtension}"
};
return ShowExportCollectionDialogAsync(cvm, opts);
}
internal static Task ExportAsPostmanCollectionAsync(CollectionViewModel cvm)
{
FilePickerSaveOptions opts = new()
{
Title = Localizer.Instance.Collection.ExportAsPostmanCollectionDialogTitle,
SuggestedFileName = $"{cvm.Name}.{PostmanCollectionExtension}"
};
return ShowExportCollectionDialogAsync(cvm, opts);
}
private static async Task ShowExportCollectionDialogAsync(CollectionViewModel cvm, FilePickerSaveOptions opts)
{
string? destFilePath = await SelectPathForFileToBeSavedAsync(opts);
if (destFilePath != null)
{
var c = cvm.ToCollection();
string json = destFilePath.EndsWith(PostmanCollectionExtension) ?
ExportAsPostmanCollectionV21(c, !cvm.IncludeSecretVariables) :
ExportAsPororocaCollection(c, !cvm.IncludeSecretVariables);
await File.WriteAllTextAsync(destFilePath, json, Encoding.UTF8);
}
}
#endregion
#region EXPORT ENVIRONMENT
internal static Task ExportEnvironmentAsync(EnvironmentViewModel evm)
{
FilePickerSaveOptions opts = new()
{
Title = Localizer.Instance.Environment.ExportEnvironmentDialogTitle,
FileTypeChoices = new List<FilePickerFileType>()
{
new(Localizer.Instance.Environment.PororocaEnvironmentFormat)
{
Patterns = new List<string> { PororocaEnvironmentExtensionGlob }
},
new(Localizer.Instance.Environment.PostmanEnvironmentFormat)
{
Patterns = new List<string> { PostmanEnvironmentExtensionGlob }
}
}
};
if (OperatingSystem.IsWindows())
{
opts.DefaultExtension = PororocaEnvironmentExtensionGlob;
}
return ShowExportEnvironmentDialogAsync(evm, opts);
}
internal static Task ExportAsPororocaEnvironmentAsync(EnvironmentViewModel evm)
{
FilePickerSaveOptions opts = new()
{
Title = Localizer.Instance.Environment.ExportAsPororocaEnvironmentDialogTitle,
SuggestedFileName = $"{evm.Name}.{PororocaEnvironmentExtension}"
};
return ShowExportEnvironmentDialogAsync(evm, opts);
}
internal static Task ExportAsPostmanEnvironmentAsync(EnvironmentViewModel evm)
{
FilePickerSaveOptions opts = new()
{
Title = Localizer.Instance.Environment.ExportAsPostmanEnvironmentDialogTitle,
SuggestedFileName = $"{evm.Name}.{PostmanEnvironmentExtension}"
};
return ShowExportEnvironmentDialogAsync(evm, opts);
}
private static async Task ShowExportEnvironmentDialogAsync(EnvironmentViewModel evm, FilePickerSaveOptions opts)
{
string? destFilePath = await SelectPathForFileToBeSavedAsync(opts);
if (destFilePath != null)
{
var env = evm.ToEnvironment();
string json = destFilePath.EndsWith(PostmanEnvironmentExtension) ?
ExportAsPostmanEnvironment(env, !evm.IncludeSecretVariables) :
ExportAsPororocaEnvironment(env, !evm.IncludeSecretVariables);
await File.WriteAllTextAsync(destFilePath, json, Encoding.UTF8);
}
}
#endregion
#region IMPORT ENVIRONMENTS
public static async Task ImportEnvironmentsAsync(EnvironmentsGroupViewModel egvm)
{
List<FilePickerFileType> fileSelectionfilters = new();
// Mac OSX file dialogs have problems with file filters... TODO: find if there is a way to solve this
if (!OperatingSystem.IsMacOS())
{
fileSelectionfilters.Add(
new(Localizer.Instance.Collection.ImportEnvironmentDialogTypes)
{
Patterns = new List<string> { PororocaEnvironmentExtensionGlob, PostmanEnvironmentExtensionGlob, JsonExtensionGlob }
}
);
}
FilePickerOpenOptions opts = new()
{
Title = Localizer.Instance.Collection.ImportEnvironmentDialogTitle,
AllowMultiple = true,
FileTypeFilter = fileSelectionfilters
};
var filesPaths = await SelectFilesFromStorageAsync(opts);
if (filesPaths != null)
{
foreach (string filePath in filesPaths)
{
string fileContent = await File.ReadAllTextAsync(filePath, Encoding.UTF8) ?? string.Empty;
bool isPororocaEnvironment = pororocaSchemaRegex.IsMatch(fileContent);
// First, tries to import as a Pororoca environment
if (isPororocaEnvironment)
{
if (TryImportPororocaEnvironment(fileContent, out var importedPororocaEnvironment))
{
importedPororocaEnvironment!.IsCurrent = false; // Imported environment should always be disabled
egvm.AddEnvironment(importedPororocaEnvironment!);
}
}
// If not a valid Pororoca environment, then tries to import as a Postman environment
else
{
if (TryImportPostmanEnvironment(fileContent, out var importedPostmanEnvironment))
{
egvm.AddEnvironment(importedPostmanEnvironment!);
}
}
}
}
}
#endregion
#region IMPORT COLLECTIONS
internal static async Task ImportCollectionsAsync(MainWindowViewModel mwvm)
{
List<FilePickerFileType> fileSelectionfilters = new();
// Mac OSX file dialogs have problems with file filters... TODO: find if there is a way to solve this
if (!OperatingSystem.IsMacOS())
{
fileSelectionfilters.Add(
new(Localizer.Instance.Collection.ImportCollectionDialogTypes)
{
Patterns = new List<string> { PororocaCollectionExtensionGlob, PostmanCollectionExtensionGlob, JsonExtensionGlob }
}
);
}
FilePickerOpenOptions opts = new()
{
Title = Localizer.Instance.Collection.ImportCollectionDialogTitle,
AllowMultiple = true,
FileTypeFilter = fileSelectionfilters
};
var filesPaths = await SelectFilesFromStorageAsync(opts);
if (filesPaths != null)
{
foreach (string filePath in filesPaths)
{
string fileContent = await File.ReadAllTextAsync(filePath, Encoding.UTF8) ?? string.Empty;
bool isPororocaCollection = pororocaSchemaRegex.IsMatch(fileContent);
// First, tries to import as a Pororoca collection
if (isPororocaCollection)
{
if (TryImportPororocaCollection(fileContent, preserveId: false, out var importedPororocaCollection))
{
mwvm.AddCollection(importedPororocaCollection!);
}
}
// If not a valid Pororoca collection, then tries to import as a Postman collection
else
{
if (TryImportPostmanCollection(fileContent, out var importedPostmanCollection))
{
mwvm.AddCollection(importedPostmanCollection!);
}
}
}
}
}
#endregion
#region SEARCH CLIENT CERTIFICATE FILES
internal static Task<string?> SearchClientCertificatePkcs12FileAsync()
{
List<FilePickerFileType> fileSelectionfilters = new();
// Mac OSX file dialogs have problems with file filters... TODO: find if there is a way to solve this
if (!OperatingSystem.IsMacOS())
{
fileSelectionfilters.Add(
new(Localizer.Instance.RequestAuth.Pkcs12ImportCertificateFileTypesDescription)
{
Patterns = new List<string> { "*.p12", "*.pfx" }
}
);
}
FilePickerOpenOptions opts = new()
{
Title = Localizer.Instance.RequestAuth.Pkcs12ImportCertificateFileDialogTitle,
AllowMultiple = false,
FileTypeFilter = fileSelectionfilters
};
return SelectFileFromStorageAsync(opts);
}
internal static Task<string?> SearchClientCertificatePemCertFileAsync()
{
List<FilePickerFileType> fileSelectionfilters = new();
// Mac OSX file dialogs have problems with file filters... TODO: find if there is a way to solve this
if (!OperatingSystem.IsMacOS())
{
fileSelectionfilters.Add(
new(Localizer.Instance.RequestAuth.PemImportCertificateFileTypesDescription)
{
Patterns = new List<string> { "*.cer", "*.crt", "*.pem" }
}
);
}
FilePickerOpenOptions opts = new()
{
Title = Localizer.Instance.RequestAuth.PemImportCertificateFileDialogTitle,
AllowMultiple = false,
FileTypeFilter = fileSelectionfilters
};
return SelectFileFromStorageAsync(opts);
}
internal static Task<string?> SearchClientCertificatePemPrivateKeyFileAsync()
{
List<FilePickerFileType> fileSelectionfilters = new();
// Mac OSX file dialogs have problems with file filters... TODO: find if there is a way to solve this
if (!OperatingSystem.IsMacOS())
{
fileSelectionfilters.Add(
new(Localizer.Instance.RequestAuth.PemImportPrivateKeyFileTypesDescription)
{
Patterns = new List<string> { "*.cer", "*.crt", "*.pem", "*.key" }
}
);
}
FilePickerOpenOptions opts = new()
{
Title = Localizer.Instance.RequestAuth.PemImportPrivateKeyFileDialogTitle,
AllowMultiple = false,
FileTypeFilter = fileSelectionfilters
};
return SelectFileFromStorageAsync(opts);
}
#endregion
#region GENERIC FILE IMPORT
internal static Task<string?> SelectFileFromStorageAsync() =>
SelectFileFromStorageAsync(new());
internal static async Task<string?> SelectFileFromStorageAsync(FilePickerOpenOptions opts) =>
(await SelectFilesFromStorageAsync(opts))?.FirstOrDefault();
private static async Task<List<string>?> SelectFilesFromStorageAsync(FilePickerOpenOptions opts)
{
var files = await MainWindow.Instance!.StorageProvider.OpenFilePickerAsync(opts);
if (files is null)
{
return null;
}
else
{
List<string> filesPaths = new();
foreach (var file in files!)
{
if (file is null || file.Path is null)
{
continue;
}
// uri.LocalPath returns the correct path in Linux and Windows
// careful with file paths with whitespaces in them
// TODO: confirm behavior for MacOSX
filesPaths.Add(file.Path.LocalPath);
}
return filesPaths;
}
}
#endregion
#region GENERIC FILE EXPORT
internal static Task<string?> SelectPathForFileToBeSavedAsync(string? suggestedFileName) =>
SelectPathForFileToBeSavedAsync(new FilePickerSaveOptions() { SuggestedFileName = suggestedFileName });
private static async Task<string?> SelectPathForFileToBeSavedAsync(FilePickerSaveOptions opts)
{
var destFile = await MainWindow.Instance!.StorageProvider.SaveFilePickerAsync(opts);
if (destFile != null && destFile.Path is not null)
{
// uri.LocalPath returns the correct path in Linux and Windows
// careful with file paths with whitespaces in them
// TODO: confirm behavior for MacOSX
return destFile.Path.LocalPath;
}
else
{
return null;
}
}
#endregion
}