-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileManagerController.cs
551 lines (454 loc) · 18.4 KB
/
FileManagerController.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using CloudFileManager.Web.Helpers;
using Kendo.Mvc.UI;
using Microsoft.AspNetCore.Mvc;
using System.Net;
namespace CloudFileManager.Web.Controllers;
public class FileManagerController : Controller
{
private readonly IAmazonS3 s3Client;
private const string BucketName = "bkt-for-deployment";
private const string SessionDirectory = "Dir";
public FileManagerController(IConfiguration config)
{
var credentials = new Amazon.Runtime.BasicAWSCredentials(config["AWS_ACCESS_KEY_ID"], config["AWS_SECRET_ACCESS_KEY"]);
s3Client = new AmazonS3Client(credentials, RegionEndpoint.USEast1);
}
// This is for creating a new folder in the s3 bucket
public virtual async Task<ActionResult> CreateDirectory(string target, FileManagerEntry entry)
{
try
{
FileManagerEntry newEntry;
// If the path is empty, we're creating a new folder
if (string.IsNullOrEmpty(entry.Path))
{
//'New Folder/' Created
newEntry = await S3CreateNewDirectoryAsync(target, entry);
}
else // Otherwise, we're copying an existing item
{
newEntry = await S3CopyItemAsync(target, entry);
}
UpdateSessionDir();
return Json(newEntry);
}
catch (Exception e)
{
throw new Exception($"CreateDirectory Exception. {e.Message}");
}
}
// Return a list of files and folders at the desired path
public virtual async Task<JsonResult> Read(string target)
{
try
{
var sessionDir = await S3ListContentsAsync(target);
HttpContext.Session.SetObjectAsJson(SessionDirectory, sessionDir);
return Json(sessionDir);
}
catch (Exception e)
{
throw new Exception($"Read Exception. {e.Message}");
}
}
// rename a folder path or a filename
public virtual async Task<ActionResult> Update(string target, FileManagerEntry entry)
{
try
{
FileManagerEntry updatedEntry = null;
if (entry.IsDirectory) // rename a folder
{
updatedEntry = await S3RenameDirectoryAsync(target, entry);
}
else
{
updatedEntry = await S3RenameFileAsync(target, entry);
}
return Json(updatedEntry);
}
catch (Exception e)
{
throw new Exception($"Update Exception. {e.Message}");
}
}
// Deletes item at the desired path
public virtual async Task<ActionResult> Destroy(FileManagerEntry entry)
{
try
{
var sessionDir = HttpContext.Session.GetObjectFromJson<ICollection<FileManagerEntry>>(SessionDirectory);
var currentEntry = sessionDir.FirstOrDefault(x => x.Path == entry.Path);
await S3DeleteAsync(entry);
sessionDir.Remove(currentEntry);
HttpContext.Session.SetObjectAsJson(SessionDirectory, sessionDir);
return Json(Array.Empty<object>());
}
catch (Exception e)
{
throw new Exception($"Destroy Exception. {e.Message}");
}
}
// Uploads actual file data
[AcceptVerbs("POST")]
public virtual async Task<ActionResult> Upload(string path, IFormFile file)
{
var sessionDir = HttpContext.Session.GetObjectFromJson<ICollection<FileManagerEntry>>(SessionDirectory);
var newEntry = new FileManagerEntry();
var newPath = path ?? "";
try
{
// IMPORTANT!
// We are saving a temporary file because AWS SDK doesn't support uploading from stream.
var tempFilePath = Path.Combine(Path.GetTempPath(), file.FileName);
await using var fileStream = System.IO.File.OpenWrite(tempFilePath);
await file.CopyToAsync(fileStream);
fileStream.Close();
// Take the temp file and upload it to the s3 bucket
var request = new PutObjectRequest
{
BucketName = BucketName,
Key = newPath + file.FileName,
FilePath = tempFilePath,
};
var response = await s3Client.PutObjectAsync(request);
if (response.HttpStatusCode == HttpStatusCode.OK)
{
newEntry.Name = file.FileName;
newEntry.Path = Path.Combine(newPath, file.FileName);
newEntry.Modified = DateTime.Now;
newEntry.ModifiedUtc = DateTime.Now;
newEntry.Created = DateTime.Now;
newEntry.CreatedUtc = DateTime.UtcNow;
newEntry.Size = file.Length;
newEntry.Extension = Path.GetExtension(file.FileName);
sessionDir.Add(newEntry);
return Json(sessionDir);
}
}
catch (Exception e)
{
throw new Exception($"Upload Error. ${e.Message}");
}
return Forbid();
}
#region S3 Methods
private async Task<List<FileManagerEntry>> S3ListContentsAsync(string directory)
{
var entries = new List<FileManagerEntry>();
var request = new ListObjectsV2Request
{
BucketName = BucketName,
Prefix = directory ?? "",
Delimiter = "/"
};
ListObjectsV2Response response;
do
{
response = await s3Client.ListObjectsV2Async(request);
// List folders
foreach (var commonPrefix in response.CommonPrefixes)
{
var lastFolderName = commonPrefix;
if (commonPrefix.Contains('/'))
{
var folders = commonPrefix.Split('/');
if (folders.Length is var count)
{
lastFolderName = count >= 2 ? folders[^2] : folders[^1];
}
}
// Add folder to list for the FileManager
entries.Add(new FileManagerEntry
{
Name = lastFolderName,
Path = commonPrefix,
IsDirectory = true,
HasDirectories = await CheckForSubdirectories(commonPrefix)
});
}
// List files
foreach (var s3Object in response.S3Objects)
{
var name = Path.GetFileNameWithoutExtension(s3Object.Key);
//if folder has a name, add item. Otherwise skip
if (name == "")
continue;
var entry = new FileManagerEntry
{
Name = name,
Path = s3Object.Key,
Extension = Path.GetExtension(s3Object.Key),
IsDirectory = false,
HasDirectories = false,
Created = s3Object.LastModified,
CreatedUtc = DateTime.SpecifyKind(s3Object.LastModified, DateTimeKind.Utc),
Modified = s3Object.LastModified,
ModifiedUtc = DateTime.SpecifyKind(s3Object.LastModified, DateTimeKind.Utc),
Size = s3Object.Size
};
// If the item is a directory, update related properties
if (s3Object.Key.Last() == '/')
{
entry.IsDirectory = true;
}
// Add file to the list for FileManager
entries.Add(entry);
}
// for do-while continuation
request.ContinuationToken = response.NextContinuationToken;
} while (response.IsTruncated);
return entries;
}
private async Task<FileManagerEntry> S3RenameDirectoryAsync(string target, FileManagerEntry entry)
{
var newPath = "";
// Get a list of the current folder's objects
var originalContentsResponse = await s3Client.ListObjectsV2Async(new ListObjectsV2Request
{
BucketName = BucketName,
Prefix = entry.Path
});
// Iterate over the items and copy them into the new destination
originalContentsResponse.S3Objects.ForEach(async (s3Object) =>
{
// copy of the Name so we do not modify the original fileManagerEntry object
var comparer = entry.Name;
// IMPORTANT:
// The trailing slash is required for directories in S3
if (entry.IsDirectory && entry.Name.Last() != '/') comparer = entry.Name + "/";
// Simpler approach to create the new path by replacing the old name with the new name in the full key
newPath = s3Object.Key.Replace(entry.Path, comparer);
await s3Client.CopyObjectAsync(new CopyObjectRequest
{
SourceBucket = BucketName,
SourceKey = s3Object.Key,
DestinationBucket = BucketName,
DestinationKey = newPath
});
});
// Cleanup phase - Delete original folder and all its contents
//await s3Client.DeleteObjectAsync(BucketName, entry.Path);
await S3DeleteAsync(entry);
// Phase 3. Renaming FileManager data source
// We have finished updating the S3 bucket, now we need to update the FileManagerEntry object with the new folder name and return it.
entry.Path = entry.Path.Replace(entry.Path, entry.Name);
// TODO This is a workaround to keep the same pattern of using trailing slashed in FileManager to have the same appearance as S3
// This is not required, but needs ot be handled carefully.
if (entry.IsDirectory && entry.Name.Last() != '/') entry.Path += "/";
return entry;
}
private async Task<FileManagerEntry> S3RenameFileAsync(string target, FileManagerEntry entry)
{
var directory = Path.GetDirectoryName(entry.Path);
var ext = entry.Extension ?? "";
//Concat extension
var newPath = NormalizePath(Path.Combine(directory, entry.Name + ext));
// Phase 1. Copy the object to a new key
var copyRequest = new CopyObjectRequest
{
SourceBucket = BucketName,
SourceKey = entry.Path,
DestinationBucket = BucketName,
DestinationKey = newPath
};
var copyResponse = await s3Client.CopyObjectAsync(copyRequest);
if (copyResponse.HttpStatusCode != HttpStatusCode.OK)
{
throw new Exception("Error copying object in Update method.");
}
//// Phase 2. Delete the original object
var deleteRequest = new DeleteObjectRequest
{
BucketName = BucketName,
Key = entry.Path
};
var deleteResponse = await s3Client.DeleteObjectAsync(deleteRequest);
//delete original file object after rename successfully with 204
if (deleteResponse.HttpStatusCode != HttpStatusCode.NoContent)
{
throw new AmazonS3Exception("Error deleting original object in Update method.");
}
// Phase 3. Renaming FileManager data source
var sessionDir = HttpContext.Session.GetObjectFromJson<ICollection<FileManagerEntry>>(SessionDirectory);
var currentEntry = sessionDir.FirstOrDefault(x => x.Path == entry.Path);
currentEntry.Name = entry.Name;
currentEntry.Path = newPath;
currentEntry.Extension = entry.Extension ?? "";
HttpContext.Session.SetObjectAsJson(SessionDirectory, sessionDir);
return currentEntry;
}
private async Task<FileManagerEntry> S3CreateNewDirectoryAsync(string target, FileManagerEntry entry)
{
// TODO This is a workaround to keep the same pattern of using trailing slashed in FileManager to have the same appearance as S3
// This is not required, but needs ot be handled carefully.
if (entry.IsDirectory && entry.Name.Last() != '/') entry.Name += "/";
var newPath = Path.Combine(target ?? "", entry.Name);
// Warning: If creating a new folder when the "New Folder" exists, it will overwrite The developer will need to rename for each initialization
var putRequest = new PutObjectRequest
{
BucketName = BucketName,
Key = newPath,
};
var response = await s3Client.PutObjectAsync(putRequest);
if (response.HttpStatusCode != HttpStatusCode.OK)
{
throw new Exception("Error creating directory... HttpStatusCode != HttpStatusCode.OK");
}
return new FileManagerEntry
{
Name = entry.Name,
Path = newPath,
IsDirectory = true,
HasDirectories = false,
Created = DateTime.Now,
CreatedUtc = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc),
Modified = DateTime.Now,
ModifiedUtc = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc)
};
}
private async Task<FileManagerEntry> S3CopyItemAsync(string target, FileManagerEntry entry)
{
var newPath = "";
var directory = Path.GetDirectoryName(entry.Path);
var ext = entry.Extension ?? "";
//Concat extension
newPath = NormalizePath(Path.Combine(target, entry.Name + ext));
FileManagerEntry entryToReturn = null;
if (entry.IsDirectory) // If it is a directory
{
// Get a list of the current folder's objects
var originalContentsResponse = await s3Client.ListObjectsV2Async(new ListObjectsV2Request
{
BucketName = BucketName,
Prefix = entry.Path
});
// Iterate over the items and copy them into the new destination
originalContentsResponse.S3Objects.ForEach(async (s3Object) =>
{
newPath = NormalizePath(Path.Combine(target, s3Object.Key));
await s3Client.CopyObjectAsync(new CopyObjectRequest
{
SourceBucket = BucketName,
SourceKey = s3Object.Key,
DestinationBucket = BucketName,
DestinationKey = newPath
});
});
//pass item which is copied
entryToReturn = new FileManagerEntry
{
Name = entry.Name,
Path = newPath,
Extension = entry.Extension,
IsDirectory = false,
HasDirectories = false,
Created = DateTime.Now,
CreatedUtc = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc),
Modified = DateTime.Now,
ModifiedUtc = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc),
Size = entry.Size
};
}
else // If it is a file
{
var request = new CopyObjectRequest
{
SourceBucket = BucketName,
SourceKey = entry.Path, //key
DestinationBucket = BucketName,
DestinationKey = newPath, //where it is being saved
};
var response = await s3Client.CopyObjectAsync(request);
if (response.HttpStatusCode != HttpStatusCode.OK)
{
throw new Exception("Error copying object... HttpStatusCode != HttpStatusCode.OK");
}
entryToReturn = new FileManagerEntry
{
Name = entry.Name,
Path = newPath,
Extension = entry.Extension,
IsDirectory = false,
HasDirectories = false,
Created = DateTime.Now,
CreatedUtc = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc),
Modified = DateTime.Now,
ModifiedUtc = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc),
Size = entry.Size
};
}
return entryToReturn;
}
private async Task S3DeleteAsync(FileManagerEntry entry)
{
var request = new ListObjectsV2Request
{
BucketName = BucketName,
Prefix = entry.Path
};
try
{
ListObjectsV2Response response;
do
{
response = await s3Client.ListObjectsV2Async(request);
response.S3Objects.ForEach(async (s3Object) =>
{
try
{
await s3Client.DeleteObjectAsync(BucketName, s3Object.Key);
}
catch (Exception e)
{
throw new Exception($"Could not delete {s3Object.Key}. Exception {e.Message}");
}
});
// for do-while continuation
request.ContinuationToken = response.NextContinuationToken;
}
while (response.IsTruncated);
}
catch (AmazonS3Exception ex)
{
Console.WriteLine($"Error deleting objects: {ex.Message}");
throw new Exception("File Not Found");
}
}
#endregion
#region General Helpers
private void UpdateSessionDir()
{
var sessionDir = HttpContext.Session.GetObjectFromJson<ICollection<FileManagerEntry>>(SessionDirectory).ToList();
foreach (var item in sessionDir.Where(d => d.IsDirectory))
{
item.HasDirectories = HasSubDirectories(item);
}
HttpContext.Session.SetObjectAsJson(SessionDirectory, sessionDir);
}
private bool HasSubDirectories(FileManagerEntry entry)
{
var sessionDir = HttpContext.Session.GetObjectFromJson<ICollection<FileManagerEntry>>(SessionDirectory)
.Where(d => d.IsDirectory && d.Path != entry.Path).ToList();
return sessionDir.Any(item => item.IsDirectory && item.Path.Contains(entry.Path));
}
private async Task<bool> CheckForSubdirectories(string keyPrefix)
{
var response = await s3Client.ListObjectsV2Async(new ListObjectsV2Request
{
BucketName = BucketName,
Prefix = keyPrefix,
Delimiter = "/"
});
return response.CommonPrefixes.Count > 0;
}
protected virtual string NormalizePath(string path)
{
var newString = path.Replace('\\', '/');
return newString;
}
#endregion
}