-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPhysicalFileAccess.cs
380 lines (270 loc) · 16.6 KB
/
PhysicalFileAccess.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
using System.Text;
using Infra.Core.FileAccess.Abstractions;
using Infra.Core.FileAccess.Enums;
using Infra.Core.FileAccess.Models;
using Infra.Core.FileAccess.Validators;
using Infra.FileAccess.Physical.Configuration;
using Infra.FileAccess.Physical.Configuration.Validators;
using Ionic.Zip;
using Ionic.Zlib;
using Microsoft.Extensions.Options;
namespace Infra.FileAccess.Physical;
public class PhysicalFileAccess : IFileAccess
{
private readonly Settings settings;
private readonly PathValidator pathValidator;
public PhysicalFileAccess(IOptions<Settings> settings)
{
this.settings = SettingsValidator.TryValidate(settings.Value, out var validationException) ? settings.Value : throw validationException;
pathValidator = new PathValidator(this.settings.Roots);
}
#region Sync Method
#region Directory
public void CreateDirectory(string directoryPath)
=> Directory.CreateDirectory(GetVerifiedPath(directoryPath));
public bool DirectoryExists(string directoryPath)
=> Directory.Exists(GetVerifiedPath(directoryPath));
public string[] GetFiles(string directoryPath, string searchPattern = "", SearchOption searchOption = default)
=> Directory.GetFiles(GetVerifiedPath(directoryPath), searchPattern, searchOption);
public void DeleteDirectory(string directoryPath, bool recursive = true)
=> Directory.Delete(GetVerifiedPath(directoryPath), recursive);
public string[] GetSubDirectories(string directoryPath, string searchPattern = "", SearchOption searchOption = default)
=> Directory.GetDirectories(GetVerifiedPath(directoryPath), searchPattern, searchOption);
public void DirectoryCompress(string directoryPath, string zipFilePath, int compressionLevel = 6)
{
directoryPath = GetVerifiedPath(directoryPath);
zipFilePath = GetVerifiedPath(zipFilePath);
using var zip = new ZipFile();
zip.UseZip64WhenSaving = Zip64Option.AsNecessary;
zip.CompressionLevel = Enum.Parse<CompressionLevel>($"{compressionLevel}");
zip.AddDirectory(directoryPath);
zip.Save(zipFilePath);
}
public void DirectorySplitCompress(string directoryPath, string zipFilePath, ZipDataUnit zipDataUnit, int segmentSize, int compressionLevel = 6)
{
directoryPath = GetVerifiedPath(directoryPath);
zipFilePath = GetVerifiedPath(zipFilePath);
using var zip = new ZipFile();
zip.MaxOutputSegmentSize = segmentSize * (int)zipDataUnit;
zip.UseZip64WhenSaving = Zip64Option.AsNecessary;
zip.BufferSize = 1024;
zip.CaseSensitiveRetrieval = true;
zip.CompressionLevel = Enum.Parse<CompressionLevel>($"{compressionLevel}");
zip.AddItem(directoryPath, string.Empty);
zip.Save(zipFilePath);
}
public string GetParentPath(string directoryPath)
=> Directory.GetParent(GetVerifiedPath(directoryPath))?.FullName;
public string GetCurrentDirectoryName(string directoryPath)
=> new DirectoryInfo(GetVerifiedPath(directoryPath)).Name;
#endregion
public bool FileExists(string filePath)
=> File.Exists(GetVerifiedPath(filePath));
public void SaveFile(string filePath, string content)
=> SaveFile(filePath, content, Encoding.UTF8);
public void SaveFile(string filePath, string content, Encoding encoding)
=> SaveFile(filePath, encoding.GetBytes(content));
public void SaveFile(string filePath, byte[] bytes)
=> File.WriteAllBytes(GetVerifiedPath(filePath), bytes);
public void DeleteFile(string filePath)
=> File.Delete(GetVerifiedPath(filePath));
public long GetFileSize(string filePath)
=> new FileInfo(GetVerifiedPath(filePath)).Length;
public string ReadTextFile(string filePath)
=> ReadTextFile(filePath, Encoding.UTF8);
public string ReadTextFile(string filePath, Encoding encoding)
{
var fileBytes = ReadFile(filePath);
return encoding.GetString(fileBytes);
}
public byte[] ReadFile(string filePath)
=> File.ReadAllBytes(GetVerifiedPath(filePath));
public void MoveFile(string sourceFilePath, string destFilePath, bool overwrite = true)
=> File.Move(GetVerifiedPath(sourceFilePath), GetVerifiedPath(destFilePath), overwrite);
public void CopyFile(string sourceFilePath, string destFilePath, bool overwrite = true)
=> File.Copy(GetVerifiedPath(sourceFilePath), GetVerifiedPath(destFilePath), overwrite);
public void AppendAllLines(string filePath, IEnumerable<string> contents)
=> AppendAllLines(filePath, contents, Encoding.UTF8);
public void AppendAllLines(string filePath, IEnumerable<string> contents, Encoding encoding)
{
filePath = GetVerifiedPath(filePath);
File.AppendAllLines(filePath, contents, encoding);
}
public string[] ReadAllLines(string filePath)
=> ReadAllLines(filePath, Encoding.UTF8);
public string[] ReadAllLines(string filePath, Encoding encoding)
{
filePath = GetVerifiedPath(filePath);
return File.ReadAllLines(filePath, encoding);
}
public void AppendAllText(string filePath, string content)
=> AppendAllText(filePath, content, Encoding.UTF8);
public void AppendAllText(string filePath, string content, Encoding encoding)
{
filePath = GetVerifiedPath(filePath);
File.AppendAllText(filePath, content, encoding);
}
public void CompressFiles(Dictionary<string, string> files, string zipFilePath, int compressionLevel = 6)
=> SaveFile(zipFilePath, CompressFiles(files, compressionLevel));
public void CompressFiles(Dictionary<string, byte[]> files, string zipFilePath, int compressionLevel = 6)
=> SaveFile(zipFilePath, CompressFiles(files, compressionLevel));
public byte[] CompressFiles(Dictionary<string, string> files, int compressionLevel = 6)
{
using var ms = new MemoryStream();
using (var zip = new ZipFile())
{
zip.UseZip64WhenSaving = Zip64Option.AsNecessary;
zip.CompressionLevel = Enum.Parse<CompressionLevel>($"{compressionLevel}");
foreach (var (compressName, path) in files)
{
var fileBytes = ReadFile(path);
zip.AddEntry(compressName, fileBytes);
}
zip.Save(ms);
}
return ms.ToArray();
}
public byte[] CompressFiles(Dictionary<string, byte[]> files, int compressionLevel = 6)
{
using var ms = new MemoryStream();
using (var zip = new ZipFile())
{
zip.UseZip64WhenSaving = Zip64Option.AsNecessary;
zip.CompressionLevel = Enum.Parse<CompressionLevel>($"{compressionLevel}");
foreach (var (compressName, fileBytes) in files)
{
zip.AddEntry(compressName, fileBytes);
}
zip.Save(ms);
}
return ms.ToArray();
}
#endregion
#region Async Method
#region Directory
public Task CreateDirectoryAsync(string directoryPath, Action<ProgressInfo> progressCallBack = null, CancellationToken cancellationToken = default)
=> throw new NotSupportedException();
public Task<bool> DirectoryExistsAsync(string directoryPath, Action<ProgressInfo> progressCallBack = null, CancellationToken cancellationToken = default)
=> throw new NotSupportedException();
public Task<string[]> GetFilesAsync(string directoryPath, string searchPattern = "", SearchOption searchOption = default, Action<ProgressInfo> progressCallBack = null, CancellationToken cancellationToken = default)
=> throw new NotSupportedException();
public Task DeleteDirectoryAsync(string directoryPath, bool recursive = true, Action<ProgressInfo> progressCallBack = null, CancellationToken cancellationToken = default)
=> throw new NotSupportedException();
public Task<string[]> GetSubDirectoriesAsync(string directoryPath, string searchPattern = "", SearchOption searchOption = default, Action<ProgressInfo> progressCallBack = null, CancellationToken cancellationToken = default)
=> throw new NotSupportedException();
public Task DirectoryCompressAsync(string directoryPath, string zipFilePath, int compressionLevel = 6, Action<ProgressInfo> progressCallBack = null, CancellationToken cancellationToken = default)
{
directoryPath = GetVerifiedPath(directoryPath);
zipFilePath = GetVerifiedPath(zipFilePath);
using var zip = new ZipFile();
zip.UseZip64WhenSaving = Zip64Option.AsNecessary;
zip.CompressionLevel = Enum.Parse<CompressionLevel>($"{compressionLevel}");
zip.AddDirectory(directoryPath);
zip.Save(zipFilePath);
return Task.CompletedTask;
}
public Task DirectorySplitCompressAsync(string directoryPath, string zipFilePath, ZipDataUnit zipDataUnit, int segmentSize, int compressionLevel = 6, Action<ProgressInfo> progressCallBack = null, CancellationToken cancellationToken = default)
{
directoryPath = GetVerifiedPath(directoryPath);
zipFilePath = GetVerifiedPath(zipFilePath);
using var zip = new ZipFile();
zip.MaxOutputSegmentSize = segmentSize * (int)zipDataUnit;
zip.UseZip64WhenSaving = Zip64Option.AsNecessary;
zip.BufferSize = 1024;
zip.CaseSensitiveRetrieval = true;
zip.CompressionLevel = Enum.Parse<CompressionLevel>($"{compressionLevel}");
zip.AddItem(directoryPath, string.Empty);
zip.Save(zipFilePath);
return Task.CompletedTask;
}
public Task<string> GetParentPathAsync(string directoryPath, Action<ProgressInfo> progressCallBack = null, CancellationToken cancellationToken = default)
=> throw new NotSupportedException();
public Task<string> GetCurrentDirectoryNameAsync(string directoryPath, Action<ProgressInfo> progressCallBack = null, CancellationToken cancellationToken = default)
=> throw new NotSupportedException();
#endregion
public Task<bool> FileExistsAsync(string filePath, Action<ProgressInfo> progressCallBack = null, CancellationToken cancellationToken = default)
=> throw new NotSupportedException();
public async Task SaveFileAsync(string filePath, string content, Action<ProgressInfo> progressCallBack = null, CancellationToken cancellationToken = default)
=> await SaveFileAsync(filePath, content, Encoding.UTF8, progressCallBack, cancellationToken);
public async Task SaveFileAsync(string filePath, string content, Encoding encoding, Action<ProgressInfo> progressCallBack = null, CancellationToken cancellationToken = default)
=> await SaveFileAsync(filePath, encoding.GetBytes(content), progressCallBack, cancellationToken);
public async Task SaveFileAsync(string filePath, byte[] bytes, Action<ProgressInfo> progressCallBack = null, CancellationToken cancellationToken = default)
=> await File.WriteAllBytesAsync(GetVerifiedPath(filePath), bytes, cancellationToken);
public Task DeleteFileAsync(string filePath, Action<ProgressInfo> progressCallBack = null, CancellationToken cancellationToken = default)
=> throw new NotSupportedException();
public Task<long> GetFileSizeAsync(string filePath, Action<ProgressInfo> progressCallBack = null, CancellationToken cancellationToken = default)
=> throw new NotSupportedException();
public async Task<string> ReadTextFileAsync(string filePath, Action<ProgressInfo> progressCallBack = null, CancellationToken cancellationToken = default)
=> await ReadTextFileAsync(filePath, Encoding.UTF8, progressCallBack, cancellationToken);
public async Task<string> ReadTextFileAsync(string filePath, Encoding encoding, Action<ProgressInfo> progressCallBack = null, CancellationToken cancellationToken = default)
{
var fileBytes = await ReadFileAsync(filePath, progressCallBack, cancellationToken);
return encoding.GetString(fileBytes);
}
public async Task<byte[]> ReadFileAsync(string filePath, Action<ProgressInfo> progressCallBack = null, CancellationToken cancellationToken = default)
=> await File.ReadAllBytesAsync(GetVerifiedPath(filePath), cancellationToken);
public Task MoveFileAsync(string sourceFilePath, string destFilePath, bool overwrite = true, Action<ProgressInfo> progressCallBack = null, CancellationToken cancellationToken = default)
=> throw new NotSupportedException();
public Task CopyFileAsync(string sourceFilePath, string destFilePath, bool overwrite = true, Action<ProgressInfo> progressCallBack = null, CancellationToken cancellationToken = default)
=> throw new NotSupportedException();
public async Task AppendAllLinesAsync(string filePath, IEnumerable<string> contents, Action<ProgressInfo> progressCallBack = null, CancellationToken cancellationToken = default)
=> await AppendAllLinesAsync(filePath, contents, Encoding.UTF8, progressCallBack, cancellationToken);
public async Task AppendAllLinesAsync(string filePath, IEnumerable<string> contents, Encoding encoding, Action<ProgressInfo> progressCallBack = null, CancellationToken cancellationToken = default)
{
filePath = GetVerifiedPath(filePath);
await File.AppendAllLinesAsync(filePath, contents, encoding, cancellationToken);
}
public async Task<string[]> ReadAllLinesAsync(string filePath, Action<ProgressInfo> progressCallBack = null, CancellationToken cancellationToken = default)
=> await ReadAllLinesAsync(filePath, Encoding.UTF8, progressCallBack, cancellationToken);
public async Task<string[]> ReadAllLinesAsync(string filePath, Encoding encoding, Action<ProgressInfo> progressCallBack = null, CancellationToken cancellationToken = default)
{
filePath = GetVerifiedPath(filePath);
return await File.ReadAllLinesAsync(filePath, encoding, cancellationToken);
}
public async Task AppendAllTextAsync(string filePath, string content, Action<ProgressInfo> progressCallBack = null, CancellationToken cancellationToken = default)
=> await AppendAllTextAsync(filePath, content, Encoding.UTF8, progressCallBack, cancellationToken);
public async Task AppendAllTextAsync(string filePath, string content, Encoding encoding, Action<ProgressInfo> progressCallBack = null, CancellationToken cancellationToken = default)
{
filePath = GetVerifiedPath(filePath);
await File.AppendAllTextAsync(filePath, content, encoding, cancellationToken);
}
public async Task CompressFilesAsync(Dictionary<string, string> files, string zipFilePath, int compressionLevel = 6, Action<ProgressInfo> progressCallBack = null, CancellationToken cancellationToken = default)
=> await SaveFileAsync(zipFilePath, await CompressFilesAsync(files, compressionLevel, progressCallBack, cancellationToken), progressCallBack, cancellationToken);
public async Task CompressFilesAsync(Dictionary<string, byte[]> files, string zipFilePath, int compressionLevel = 6, Action<ProgressInfo> progressCallBack = null, CancellationToken cancellationToken = default)
=> await SaveFileAsync(zipFilePath, await CompressFilesAsync(files, compressionLevel, progressCallBack, cancellationToken), progressCallBack, cancellationToken);
public async Task<byte[]> CompressFilesAsync(Dictionary<string, string> files, int compressionLevel = 6, Action<ProgressInfo> progressCallBack = null, CancellationToken cancellationToken = default)
{
await using var ms = new MemoryStream();
using (var zip = new ZipFile())
{
zip.UseZip64WhenSaving = Zip64Option.AsNecessary;
zip.CompressionLevel = Enum.Parse<CompressionLevel>($"{compressionLevel}");
foreach (var (compressName, path) in files)
{
var fileBytes = await ReadFileAsync(path, progressCallBack, cancellationToken);
zip.AddEntry(compressName, fileBytes);
}
zip.Save(ms);
}
return ms.ToArray();
}
public async Task<byte[]> CompressFilesAsync(Dictionary<string, byte[]> files, int compressionLevel = 6, Action<ProgressInfo> progressCallBack = null, CancellationToken cancellationToken = default)
{
await using var ms = new MemoryStream();
using (var zip = new ZipFile())
{
zip.UseZip64WhenSaving = Zip64Option.AsNecessary;
zip.CompressionLevel = Enum.Parse<CompressionLevel>($"{compressionLevel}");
foreach (var (compressName, fileBytes) in files)
{
zip.AddEntry(compressName, fileBytes);
}
zip.Save(ms);
}
return ms.ToArray();
}
#endregion
#region Private Method
private string GetVerifiedPath(string path) => pathValidator.GetValidPath(path);
#endregion
}