-
Notifications
You must be signed in to change notification settings - Fork 5.1k
/
DatabaseRoxyFilemanService.cs
633 lines (523 loc) · 25.8 KB
/
DatabaseRoxyFilemanService.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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Nop.Core;
using Nop.Core.Domain.Media;
using Nop.Core.Infrastructure;
using Nop.Data;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Bmp;
using SixLabors.ImageSharp.Formats.Gif;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using static SixLabors.ImageSharp.Configuration;
namespace Nop.Services.Media.RoxyFileman
{
/// <summary>
/// Database RoxyFileman service
/// </summary>
public class DatabaseRoxyFilemanService : FileRoxyFilemanService
{
#region Fields
private readonly IPictureService _pictureService;
private readonly IRepository<Picture> _pictureRepository;
#endregion
#region Ctor
public DatabaseRoxyFilemanService(IPictureService pictureService,
IRepository<Picture> pictureRepository,
IWebHostEnvironment webHostEnvironment,
IHttpContextAccessor httpContextAccessor,
INopFileProvider fileProvider,
IWebHelper webHelper,
IWorkContext workContext,
MediaSettings mediaSettings) : base(webHostEnvironment, httpContextAccessor, fileProvider, webHelper, workContext, mediaSettings)
{
_pictureService = pictureService;
_pictureRepository = pictureRepository;
}
#endregion
#region Utilities
/// <summary>
/// Get directories in the passed parent directory
/// </summary>
/// <param name="parentDirectoryPath">Path to the parent directory</param>
/// <returns>Array of the paths to the directories</returns>
protected override async Task<ArrayList> GetDirectoriesByParentDirectoryAsync(string parentDirectoryPath)
{
await CreateDirectoryAsync(parentDirectoryPath);
return await base.GetDirectoriesByParentDirectoryAsync(parentDirectoryPath);
}
/// <summary>
/// Gets picture from database by file
/// </summary>
/// <param name="filePath">File path</param>
/// <returns>Exist picture from database or null</returns>
protected virtual async Task<Picture> GetPictureByFileAsync(string filePath)
{
var sourceVirtualPath = _fileProvider.GetVirtualPath(_fileProvider.GetDirectoryName(filePath));
var fileName = _fileProvider.GetFileNameWithoutExtension(filePath);
var picture = (await _pictureService.GetPicturesAsync(sourceVirtualPath.TrimEnd('/')))
.FirstOrDefault(p => fileName.Contains(p.SeoFilename));
return picture;
}
/// <summary>
/// Create the passed directory
/// </summary>
/// <param name="directoryPath">Path to the parent directory</param>
protected virtual async Task CreateDirectoryAsync(string directoryPath)
{
_fileProvider.CreateDirectory(directoryPath);
var virtualPath = _fileProvider.GetVirtualPath(directoryPath).TrimEnd('/');
var directoryNames = (await _pictureService.GetPicturesAsync($"{virtualPath}/"))
.Where(picture => picture.VirtualPath != virtualPath)
.Select(picture => _fileProvider.GetAbsolutePath(picture.VirtualPath.TrimStart('~').Split('/')))
.Distinct();
foreach (var directory in directoryNames)
await CreateDirectoryAsync(directory);
}
/// <summary>
/// Get files in the passed directory
/// </summary>
/// <param name="directoryPath">Path to the files directory</param>
/// <param name="type">Type of the files</param>
/// <returns>List of paths to the files</returns>
protected override async Task<List<string>> GetFilesByDirectoryAsync(string directoryPath, string type)
{
//store files on disk if needed
await FlushImagesOnDiskAsync(directoryPath);
return await base.GetFilesByDirectoryAsync(directoryPath, type);
}
/// <summary>
/// Сopy the directory with the embedded files and directories
/// </summary>
/// <param name="sourcePath">Path to the source directory</param>
/// <param name="destinationPath">Path to the destination directory</param>
protected override async Task BaseCopyDirectoryAsync(string sourcePath, string destinationPath)
{
var pictures = await _pictureService.GetPicturesAsync($"{_fileProvider.GetVirtualPath(sourcePath).TrimEnd('/')}/");
var baseDestinationPathVirtualPath = _fileProvider.GetVirtualPath(destinationPath);
foreach (var picture in pictures)
{
var destinationPathVirtualPath =
$"{baseDestinationPathVirtualPath.TrimEnd('/')}{picture.VirtualPath.Replace(_fileProvider.GetVirtualPath(sourcePath), "")}";
await _pictureService.InsertPictureAsync(new RoxyFilemanFormFile(picture,
await _pictureService.GetPictureBinaryByPictureIdAsync(picture.Id),
await _pictureService.GetFileExtensionFromMimeTypeAsync(picture.MimeType)),
string.Empty,
destinationPathVirtualPath);
}
}
/// <summary>
/// Encode the image into a byte array in accordance with the specified image format
/// </summary>
/// <typeparam name="TPixel">Pixel data type</typeparam>
/// <param name="image">Image data</param>
/// <param name="imageFormat">Image format</param>
/// <param name="quality">Quality index that will be used to encode the image</param>
/// <returns>Image binary data</returns>
protected virtual async Task<byte[]> EncodeImageAsync<TPixel>(Image<TPixel> image, IImageFormat imageFormat, int? quality = null)
where TPixel : unmanaged, IPixel<TPixel>
{
await using var stream = new MemoryStream();
var imageEncoder = Default.ImageFormatsManager.FindEncoder(imageFormat);
switch (imageEncoder)
{
case JpegEncoder jpegEncoder:
jpegEncoder.Subsample = JpegSubsample.Ratio444;
jpegEncoder.Quality = quality ?? _mediaSettings.DefaultImageQuality;
await jpegEncoder.EncodeAsync(image, stream, default);
break;
case PngEncoder pngEncoder:
pngEncoder.ColorType = PngColorType.RgbWithAlpha;
await pngEncoder.EncodeAsync(image, stream, default);
break;
case BmpEncoder bmpEncoder:
bmpEncoder.BitsPerPixel = BmpBitsPerPixel.Pixel32;
await bmpEncoder.EncodeAsync(image, stream, default);
break;
case GifEncoder gifEncoder:
await gifEncoder.EncodeAsync(image, stream, default);
break;
default:
await imageEncoder.EncodeAsync(image, stream, default);
break;
}
return stream.ToArray();
}
/// <summary>
/// Calculates picture dimensions whilst maintaining aspect
/// </summary>
/// <param name="originalSize">The original picture size</param>
/// <param name="targetSize">The target picture size (longest side)</param>
/// <param name="resizeType">Resize type</param>
/// <param name="ensureSizePositive">A value indicating whether we should ensure that size values are positive</param>
/// <returns></returns>
protected virtual Size CalculateDimensions(Size originalSize, int targetSize,
ResizeType resizeType = ResizeType.LongestSide, bool ensureSizePositive = true)
{
float width, height;
switch (resizeType)
{
case ResizeType.LongestSide:
if (originalSize.Height > originalSize.Width)
{
// portrait
width = originalSize.Width * (targetSize / (float)originalSize.Height);
height = targetSize;
}
else
{
// landscape or square
width = targetSize;
height = originalSize.Height * (targetSize / (float)originalSize.Width);
}
break;
case ResizeType.Width:
width = targetSize;
height = originalSize.Height * (targetSize / (float)originalSize.Width);
break;
case ResizeType.Height:
width = originalSize.Width * (targetSize / (float)originalSize.Height);
height = targetSize;
break;
default:
throw new Exception("Not supported ResizeType");
}
if (!ensureSizePositive)
return new Size((int)Math.Round(width), (int)Math.Round(height));
if (width < 1)
width = 1;
if (height < 1)
height = 1;
//we invoke Math.Round to ensure that no white background is rendered - https://www.nopcommerce.com/boards/topic/40616/image-resizing-bug
return new Size((int)Math.Round(width), (int)Math.Round(height));
}
/// <summary>
/// Save picture by picture virtual path
/// </summary>
/// <param name="picture">Picture instance</param>
/// <param name="targetSize">The target picture size (longest side)</param>
protected virtual async Task SavePictureByVirtualPathAsync(Picture picture, int targetSize = 0)
{
if (string.IsNullOrEmpty(picture?.VirtualPath) || string.IsNullOrEmpty(picture.SeoFilename))
return;
var pictureBinary = await _pictureService.LoadPictureBinaryAsync(picture);
if (pictureBinary == null || pictureBinary.Length == 0)
return;
var seoFileName = picture.SeoFilename;
var lastPart = await _pictureService.GetFileExtensionFromMimeTypeAsync(picture.MimeType);
var thumbFileName = targetSize == 0 ? $"{seoFileName}.{lastPart}" : $"{seoFileName}_{targetSize}.{lastPart}";
var thumbsDirectoryPath = _fileProvider.GetAbsolutePath(picture.VirtualPath.TrimStart('~'));
_fileProvider.CreateDirectory(thumbsDirectoryPath);
var thumbFilePath = _fileProvider.Combine(thumbsDirectoryPath, thumbFileName);
if (picture.IsNew)
{
// delete old file if exist
_fileProvider.DeleteFile(thumbFilePath);
//we do not validate picture binary here to ensure that no exception ("Parameter is not valid") will be thrown
await _pictureService.UpdatePictureAsync(picture.Id,
pictureBinary,
picture.MimeType,
picture.SeoFilename,
picture.AltAttribute,
picture.TitleAttribute,
false,
false);
}
if (_fileProvider.FileExists(thumbFilePath))
return;
//the named mutex helps to avoid creating the same files in different threads,
//and does not decrease performance significantly, because the code is blocked only for the specific file.
//you should be very careful, mutexes cannot be used in with the await operation
//we can't use semaphore here, because it produces PlatformNotSupportedException exception on UNIX based systems
using var mutex = new Mutex(false, thumbFileName);
mutex.WaitOne();
try
{
//check, if the file was created, while we were waiting for the release of the mutex.
if (!_fileProvider.FileExists(thumbFilePath))
{
byte[] pictureBinaryResized;
if (targetSize != 0)
{
//resizing required
using var image = Image.Load<Rgba32>(pictureBinary, out var imageFormat);
var size = image.Size();
image.Mutate(imageProcess => imageProcess.Resize(new ResizeOptions
{
Mode = ResizeMode.Max,
Size = CalculateDimensions(size, targetSize)
}));
pictureBinaryResized = EncodeImageAsync(image, imageFormat).Result;
}
else
{
//create a copy of pictureBinary
pictureBinaryResized = pictureBinary.ToArray();
}
//save
_fileProvider.WriteAllBytesAsync(thumbFilePath, pictureBinaryResized).Wait();
}
}
finally
{
mutex.ReleaseMutex();
}
}
/// <summary>
/// Flush image on disk
/// </summary>
/// <param name="picture">Image to store on disk</param>
/// <param name="maxWidth">Max image width</param>
/// <param name="maxHeight">Max image height</param>
protected virtual async Task FlushImagesAsync(Picture picture, int maxWidth, int maxHeight)
{
var image = Image.Load((await _pictureService.GetPictureBinaryByPictureIdAsync(picture.Id)).BinaryData);
maxWidth = image.Width > maxWidth ? maxWidth : 0;
maxHeight = image.Height > maxHeight ? maxHeight : 0;
//save picture to folder if its not exists
await SavePictureByVirtualPathAsync(picture, maxWidth > maxHeight ? maxWidth : maxHeight);
}
#endregion
#region Methods
#region Configuration
/// <summary>
/// Initial service configuration
/// </summary>
public override async Task ConfigureAsync()
{
await base.ConfigureAsync();
foreach (var filePath in _fileProvider.GetFiles(_fileProvider.GetAbsolutePath(NopRoxyFilemanDefaults.DefaultRootDirectory.Split('/')), topDirectoryOnly: false))
{
var uniqueFileName = GetUniqueFileName(filePath, _fileProvider.GetFileNameWithoutExtension(filePath));
if (await _pictureService.GetPictureSeNameAsync(uniqueFileName) != null)
continue;
var picture = new Picture
{
IsNew = true,
SeoFilename = uniqueFileName
};
await _pictureService.InsertPictureAsync(
new RoxyFilemanFormFile(picture, new PictureBinary { BinaryData = await _fileProvider.ReadAllBytesAsync(filePath) }, _fileProvider.GetFileExtension(filePath)),
string.Empty, _fileProvider.GetVirtualPath(filePath));
}
}
#endregion
#region Directories
/// <summary>
/// Move the directory
/// </summary>
/// <param name="sourcePath">Path to the source directory</param>
/// <param name="destinationPath">Path to the destination directory</param>
/// <returns>A task that represents the completion of the operation</returns>
public override async Task MoveDirectoryAsync(string sourcePath, string destinationPath)
{
await base.MoveDirectoryAsync(sourcePath, destinationPath);
var pictures = await _pictureService.GetPicturesAsync($"{_fileProvider.GetVirtualPath(sourcePath).TrimEnd('/')}/");
var baseDestinationPathVirtualPath = _fileProvider.GetVirtualPath(destinationPath);
foreach (var picture in pictures)
{
var destinationPathVirtualPath =
$"{baseDestinationPathVirtualPath.TrimEnd('/')}/{_fileProvider.GetDirectoryNameOnly(_fileProvider.GetAbsolutePath(sourcePath.TrimStart('~').Split('/')))}";
picture.VirtualPath = destinationPathVirtualPath;
await _pictureService.UpdatePictureAsync(picture);
}
}
/// <summary>
/// Rename the directory
/// </summary>
/// <param name="sourcePath">Path to the source directory</param>
/// <param name="newName">New name of the directory</param>
/// <returns>A task that represents the completion of the operation</returns>
public override async Task RenameDirectoryAsync(string sourcePath, string newName)
{
var sourceVirtualPath = _fileProvider.GetVirtualPath(sourcePath).TrimEnd('/');
var pictures = await _pictureService.GetPicturesAsync($"{sourceVirtualPath}/");
var destinationPath =
$"{_fileProvider.GetVirtualPath(_fileProvider.GetParentDirectory(_fileProvider.GetAbsolutePath(sourcePath.Split('/')))).TrimEnd('/')}/{newName}";
foreach (var picture in pictures)
{
picture.VirtualPath = destinationPath;
await _pictureService.UpdatePictureAsync(picture);
}
await base.RenameDirectoryAsync(sourcePath, newName);
}
#endregion
#region Files
/// <summary>
/// Copy the file
/// </summary>
/// <param name="sourcePath">Path to the source file</param>
/// <param name="destinationPath">Path to the destination file</param>
/// <returns>A task that represents the completion of the operation</returns>
public override async Task CopyFileAsync(string sourcePath, string destinationPath)
{
var filePath = _fileProvider.GetAbsolutePath(sourcePath.Split('/'));
var picture = await GetPictureByFileAsync(filePath);
if (picture == null)
throw new Exception(await GetLanguageResourceAsync("E_CopyFile"));
await _pictureService.InsertPictureAsync(
new RoxyFilemanFormFile(picture, await _pictureService.GetPictureBinaryByPictureIdAsync(picture.Id), _fileProvider.GetFileExtension(filePath)),
string.Empty, _fileProvider.GetVirtualPath(destinationPath));
await GetHttpContext().Response.WriteAsync(GetSuccessResponse());
}
/// <summary>
/// Delete the file
/// </summary>
/// <param name="sourcePath">Path to the file</param>
/// <returns>A task that represents the completion of the operation</returns>
public override async Task DeleteFileAsync(string sourcePath)
{
var filePath = _fileProvider.GetAbsolutePath(sourcePath.Split('/'));
var picture = await GetPictureByFileAsync(filePath);
if (picture == null)
throw new Exception(await GetLanguageResourceAsync("E_DeletеFile"));
await _pictureService.DeletePictureAsync(picture);
await base.DeleteFileAsync(sourcePath);
}
/// <summary>
/// Move the file
/// </summary>
/// <param name="sourcePath">Path to the source file</param>
/// <param name="destinationPath">Path to the destination file</param>
/// <returns>A task that represents the completion of the operation</returns>
public override async Task MoveFileAsync(string sourcePath, string destinationPath)
{
await base.MoveFileAsync(sourcePath, destinationPath);
var filePath = _fileProvider.GetAbsolutePath(sourcePath.Split('/'));
var picture = await GetPictureByFileAsync(filePath);
if (picture == null)
throw new Exception(await GetLanguageResourceAsync("E_MoveFile"));
picture.VirtualPath = _fileProvider.GetVirtualPath(_fileProvider.GetVirtualPath(_fileProvider.GetDirectoryName(destinationPath)));
await _pictureService.UpdatePictureAsync(picture);
}
/// <summary>
/// Rename the file
/// </summary>
/// <param name="sourcePath">Path to the source file</param>
/// <param name="newName">New name of the file</param>
/// <returns>A task that represents the completion of the operation</returns>
public override async Task RenameFileAsync(string sourcePath, string newName)
{
var filePath = _fileProvider.GetAbsolutePath(_fileProvider.GetVirtualPath(sourcePath));
var picture = await GetPictureByFileAsync(filePath);
if (picture == null)
throw new Exception(await GetLanguageResourceAsync("E_RenameFile"));
picture.SeoFilename = _fileProvider.GetFileNameWithoutExtension(newName);
await _pictureService.UpdatePictureAsync(picture);
await base.DeleteFileAsync(sourcePath);
}
/// <summary>
/// Upload files to a directory on passed path
/// </summary>
/// <param name="directoryPath">Path to directory to upload files</param>
/// <returns>A task that represents the completion of the operation</returns>
public override async Task UploadFilesAsync(string directoryPath)
{
var result = GetSuccessResponse();
var hasErrors = false;
try
{
var fullPath = GetFullPath(await GetVirtualPathAsync(directoryPath));
if (!await IsPathAllowedAsync(fullPath))
throw new Exception(await GetLanguageResourceAsync("E_UploadNotAll"));
foreach (var formFile in GetHttpContext().Request.Form.Files)
{
var fileName = formFile.FileName;
if (await CanHandleFileAsync(fileName))
{
var uniqueFileName = GetUniqueFileName(fullPath, _fileProvider.GetFileName(fileName));
var destinationFile = _fileProvider.Combine(fullPath, uniqueFileName);
//A warning (SCS0018 - Path Traversal) from the "Security Code Scan" analyzer may appear at this point.
//In this case, it is not relevant. The input is not supplied by user.
if (GetFileType(new FileInfo(uniqueFileName).Extension) != "image")
{
await using var stream = new FileStream(destinationFile, FileMode.OpenOrCreate);
await formFile.CopyToAsync(stream);
}
else
{
await _pictureService.InsertPictureAsync(formFile, virtualPath: await GetVirtualPathAsync(directoryPath));
}
}
else
{
hasErrors = true;
result = GetErrorResponse(await GetLanguageResourceAsync("E_UploadNotAll"));
}
}
}
catch (Exception ex)
{
result = GetErrorResponse(ex.Message);
}
if (IsAjaxRequest())
{
if (hasErrors)
result = GetErrorResponse(await GetLanguageResourceAsync("E_UploadNotAll"));
await GetHttpContext().Response.WriteAsync(result);
}
else
await GetHttpContext().Response.WriteAsync($"<script>parent.fileUploaded({result});</script>");
}
#endregion
#region Images
/// <summary>
/// Flush all images on disk
/// </summary>
/// <param name="removeOriginal">Specifies whether to delete original images</param>
public override async Task FlushAllImagesOnDiskAsync(bool removeOriginal = true)
{
await base.FlushAllImagesOnDiskAsync(removeOriginal);
var pageIndex = 0;
const int pageSize = 400;
int.TryParse(await GetSettingAsync("MAX_IMAGE_WIDTH"), out var width);
int.TryParse(await GetSettingAsync("MAX_IMAGE_HEIGHT"), out var height);
try
{
while (true)
{
var pictures = await _pictureService.GetPicturesAsync($"~{NopRoxyFilemanDefaults.DefaultRootDirectory}/",
pageIndex, pageSize);
pageIndex++;
//all pictures flushed?
if (!pictures.Any())
break;
foreach (var picture in pictures)
await FlushImagesAsync(picture, width, height);
if (removeOriginal)
await _pictureRepository.DeleteAsync(pictures, false);
}
}
catch
{
// ignored
}
}
/// <summary>
/// Flush images on disk
/// </summary>
/// <param name="directoryPath">Directory path to flush images</param>
public override async Task FlushImagesOnDiskAsync(string directoryPath)
{
await base.FlushImagesOnDiskAsync(directoryPath);
foreach (var picture in await _pictureService.GetPicturesAsync(_fileProvider.GetVirtualPath(directoryPath)))
{
int.TryParse(await GetSettingAsync("MAX_IMAGE_WIDTH"), out var width);
int.TryParse(await GetSettingAsync("MAX_IMAGE_HEIGHT"), out var height);
await FlushImagesAsync(picture, width, height);
}
}
#endregion
#endregion
}
}