-
Notifications
You must be signed in to change notification settings - Fork 0
/
AWSPictureService.cs
1112 lines (1016 loc) · 42.6 KB
/
AWSPictureService.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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Nop.Services.Media;
using System;
using Nop.Core;
using Nop.Core.Data;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Media;
using Nop.Services.Configuration;
using Nop.Services.Events;
using Nop.Services.Logging;
using System.IO;
using System.Drawing;
using Nop.Services.Seo;
using ImageResizer;
using Amazon.S3;
using Amazon.S3.Model;
using Amazon;
using Amazon.Runtime;
using Amazon.S3.IO;
using Nop.Data;
using Microsoft.AspNetCore.Hosting;
using System.Threading;
namespace Nop.Services.Aws
{
public partial class AwsPictureService : PictureService, IPictureService
{
#region Const
private const int MULTIPLE_THUMB_DIRECTORIES_LENGTH = 3;
#endregion
#region Fields
private readonly IRepository<Picture> _pictureRepository;
private readonly ISettingService _settingService;
private readonly IWebHelper _webHelper;
private readonly ILogger _logger;
private readonly IEventPublisher _eventPublisher;
private readonly MediaSettings _mediaSettings;
private readonly IHostingEnvironment _hostingEnvironment;
#endregion
#region Ctr
public AwsPictureService(IRepository<Picture> pictureRepository,
IRepository<ProductPicture> productPictureRepository,
ISettingService settingService,
IWebHelper webHelper,
ILogger logger,
IDbContext dbContext,
IEventPublisher eventPublisher,
MediaSettings mediaSettings,
IDataProvider dataProvider,
IHostingEnvironment hostingEnvironment)
: base( pictureRepository,
productPictureRepository,
settingService,
webHelper,
logger,
dbContext,
eventPublisher,
mediaSettings,
dataProvider,
hostingEnvironment)
{
this._pictureRepository = pictureRepository;
this._settingService = settingService;
this._webHelper = webHelper;
this._logger = logger;
this._eventPublisher = eventPublisher;
this._mediaSettings = mediaSettings;
this._hostingEnvironment = hostingEnvironment;
}
#endregion
#region Utilities
/// <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 new virtual Size CalculateDimensions(Size originalSize, int targetSize,
ResizeType resizeType = ResizeType.LongestSide, bool ensureSizePositive = true)
{
var newSize = new Size();
switch (resizeType)
{
case ResizeType.LongestSide:
if (originalSize.Height > originalSize.Width)
{
// portrait
newSize.Width = (int)(originalSize.Width * (float)(targetSize / (float)originalSize.Height));
newSize.Height = targetSize;
}
else
{
// landscape or square
newSize.Height = (int)(originalSize.Height * (float)(targetSize / (float)originalSize.Width));
newSize.Width = targetSize;
}
break;
case ResizeType.Width:
newSize.Height = (int)(originalSize.Height * (float)(targetSize / (float)originalSize.Width));
newSize.Width = targetSize;
break;
case ResizeType.Height:
newSize.Width = (int)(originalSize.Width * (float)(targetSize / (float)originalSize.Height));
newSize.Height = targetSize;
break;
default:
throw new Exception("Not supported ResizeType");
}
if (ensureSizePositive)
{
if (newSize.Width < 1)
newSize.Width = 1;
if (newSize.Height < 1)
newSize.Height = 1;
}
return newSize;
}
/// <summary>
/// Returns the file extension from mime type.
/// </summary>
/// <param name="mimeType">Mime type</param>
/// <returns>File extension</returns>
protected new virtual string GetFileExtensionFromMimeType(string mimeType)
{
if (mimeType == null)
return null;
//also see System.Web.MimeMapping for more mime types
string[] parts = mimeType.Split('/');
string lastPart = parts[parts.Length - 1];
switch (lastPart)
{
case "pjpeg":
lastPart = "jpg";
break;
case "x-png":
lastPart = "png";
break;
case "x-icon":
lastPart = "ico";
break;
}
return lastPart;
}
/// <summary>
/// Loads a picture from file
/// </summary>
/// <param name="pictureId">Picture identifier</param>
/// <param name="mimeType">MIME type</param>
/// <returns>Picture binary</returns>
public new virtual byte[] LoadPictureFromFile(int pictureId, string mimeType)
{
if (_mediaSettings.AWSS3Enable)
{
string lastPart = GetFileExtensionFromMimeType(mimeType);
string fileName = string.Format("{0}_0.{1}", pictureId.ToString("0000000"), lastPart);
var filePath = GetPictureLocalPath(fileName);
if (!S3ExistsImage(fileName))
return new byte[0];
return S3ReadAllBytes(fileName);
}
else
{
string lastPart = GetFileExtensionFromMimeType(mimeType);
string fileName = string.Format("{0}_0.{1}", pictureId.ToString("0000000"), lastPart);
var filePath = GetPictureLocalPath(fileName);
if (!File.Exists(filePath))
return new byte[0];
return File.ReadAllBytes(filePath);
}
}
/// <summary>
/// Save picture on file system
/// </summary>
/// <param name="pictureId">Picture identifier</param>
/// <param name="pictureBinary">Picture binary</param>
/// <param name="mimeType">MIME type</param>
protected new virtual void SavePictureInFile(int pictureId, byte[] pictureBinary, string mimeType)
{
//string lastPart = GetFileExtensionFromMimeType(mimeType);
//string fileName = string.Format("{0}_0.{1}", pictureId.ToString("0000000"), lastPart);
var lastPart = GetFileExtensionFromMimeType(mimeType);
var fileName = $"{pictureId:0000000}_0.{lastPart}";
//AWS S3
if (_mediaSettings.AWSS3Enable)
S3UploadImage(fileName, pictureBinary, mimeType);
else
File.WriteAllBytes(GetPictureLocalPath(fileName), pictureBinary);
}
/// <summary>
/// Delete a picture on file system
/// </summary>
/// <param name="picture">Picture</param>
protected new virtual void DeletePictureOnFileSystem(Picture picture)
{
if (picture == null)
throw new ArgumentNullException("picture");
string lastPart = GetFileExtensionFromMimeType(picture.MimeType);
string fileName = string.Format("{0}_0.{1}", picture.Id.ToString("0000000"), lastPart);
string filePath = GetPictureLocalPath(fileName);
if (_mediaSettings.AWSS3Enable)
{
S3DeleteImage(filePath, picture.MimeType);
}
else
{
if (File.Exists(filePath))
{
File.Delete(filePath);
}
}
}
/// <summary>
/// Delete picture thumbs
/// </summary>
/// <param name="picture">Picture</param>
protected new virtual void DeletePictureThumbs(Picture picture)
{
if (_mediaSettings.AWSS3Enable)
{
// Create a client
var client = GetS3Client();
string prefix = string.Format("{0}", picture.Id.ToString("0000000"));
// List all objects
var listRequest = new ListObjectsRequest
{
BucketName = _mediaSettings.AWSS3BucketName,
Prefix = $"images/thumbs/{prefix}"
};
ListObjectsResponse listResponse;
do
{
// Get a list of objects
listResponse = client.ListObjects(listRequest);
foreach (S3Object obj in listResponse.S3Objects)
{
//delete
S3DeleteImageFromThumbs(obj.Key, picture.MimeType);
}
// Set the marker property
listRequest.Marker = listResponse.NextMarker;
} while (listResponse.IsTruncated);
}
else
{
string filter = string.Format("{0}*.*", picture.Id.ToString("0000000"));
var thumbDirectoryPath = Path.Combine(_hostingEnvironment.WebRootPath, "images\\thumbs");
//_webHelper.MapPath("~/content/images/thumbs");
string[] currentFiles = System.IO.Directory.GetFiles(thumbDirectoryPath, filter, SearchOption.AllDirectories);
foreach (string currentFileName in currentFiles)
{
var thumbFilePath = GetThumbLocalPath(currentFileName);
File.Delete(thumbFilePath);
}
}
}
/// <summary>
/// Get picture (thumb) local path
/// </summary>
/// <param name="thumbFileName">Filename</param>
/// <returns>Local picture thumb path</returns>
protected new virtual string GetThumbLocalPath(string thumbFileName)
{
if (_mediaSettings.AWSS3Enable)
{
return $"{_mediaSettings.AWSS3RootUrl}/{_mediaSettings.AWSS3BucketName}/images/thumbs/{thumbFileName}";
}
else
{
var thumbsDirectoryPath = Path.Combine(_hostingEnvironment.WebRootPath, "images\\thumbs"); ;
if (_mediaSettings.MultipleThumbDirectories)
{
//get the first two letters of the file name
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(thumbFileName);
if (fileNameWithoutExtension != null && fileNameWithoutExtension.Length > MULTIPLE_THUMB_DIRECTORIES_LENGTH)
{
var subDirectoryName = fileNameWithoutExtension.Substring(0, MULTIPLE_THUMB_DIRECTORIES_LENGTH);
thumbsDirectoryPath = Path.Combine(thumbsDirectoryPath, subDirectoryName);
if (!System.IO.Directory.Exists(thumbsDirectoryPath))
{
System.IO.Directory.CreateDirectory(thumbsDirectoryPath);
}
}
}
var thumbFilePath = Path.Combine(thumbsDirectoryPath, thumbFileName);
return thumbFilePath;
}
}
/// <summary>
/// Get picture (thumb) URL
/// </summary>
/// <param name="thumbFileName">Filename</param>
/// <param name="storeLocation">Store location URL; null to use determine the current store location automatically</param>
/// <returns>Local picture thumb path</returns>
protected new virtual string GetThumbUrl(string thumbFileName, string storeLocation = null)
{
if (_mediaSettings.AWSS3Enable)
{
var url = $"{_mediaSettings.AWSS3RootUrl}/{_mediaSettings.AWSS3BucketName}/images/thumbs/";
if (_mediaSettings.MultipleThumbDirectories)
{
//get the first two letters of the file name
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(thumbFileName);
if (fileNameWithoutExtension != null && fileNameWithoutExtension.Length > MULTIPLE_THUMB_DIRECTORIES_LENGTH)
{
var subDirectoryName = fileNameWithoutExtension.Substring(0, MULTIPLE_THUMB_DIRECTORIES_LENGTH);
url = url + subDirectoryName + "/";
}
}
url = url + thumbFileName;
return url;
}
else
{
storeLocation = !String.IsNullOrEmpty(storeLocation)
? storeLocation
: _webHelper.GetStoreLocation();
var url = storeLocation + "/images/thumbs/";
if (_mediaSettings.MultipleThumbDirectories)
{
//get the first two letters of the file name
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(thumbFileName);
if (fileNameWithoutExtension != null && fileNameWithoutExtension.Length > MULTIPLE_THUMB_DIRECTORIES_LENGTH)
{
var subDirectoryName = fileNameWithoutExtension.Substring(0, MULTIPLE_THUMB_DIRECTORIES_LENGTH);
url = url + subDirectoryName + "/";
}
}
url = url + thumbFileName;
return url;
}
}
/// <summary>
/// Get picture local path. Used when images stored on file system (not in the database)
/// </summary>
/// <param name="fileName">Filename</param>
/// <returns>Local picture path</returns>
protected new virtual string GetPictureLocalPath(string fileName)
{
if (_mediaSettings.AWSS3Enable)
{
return $"{_mediaSettings.AWSS3RootUrl}/{_mediaSettings.AWSS3BucketName}/images/{fileName}";
}
else
{
var imagesDirectoryPath = Path.Combine(_hostingEnvironment.WebRootPath, "images\\thumbs");
var filePath = Path.Combine(imagesDirectoryPath, fileName);
return filePath;
}
}
/// <summary>
/// Gets the loaded picture binary depending on picture storage settings
/// </summary>
/// <param name="picture">Picture</param>
/// <param name="fromDb">Load from database; otherwise, from file system</param>
/// <returns>Picture binary</returns>
protected new virtual byte[] LoadPictureBinary(Picture picture, bool fromDb)
{
if (picture == null)
throw new ArgumentNullException("picture");
byte[] result = null;
if (fromDb)
result = picture.PictureBinary;
else
result = LoadPictureFromFile(picture.Id, picture.MimeType);
return result;
}
#endregion
#region Getting picture local path/URL methods
/// <summary>
/// Gets the loaded picture binary depending on picture storage settings
/// </summary>
/// <param name="picture">Picture</param>
/// <returns>Picture binary</returns>
public new virtual byte[] LoadPictureBinary(Picture picture)
{
return LoadPictureBinary(picture, this.StoreInDb);
}
/// <summary>
/// Get picture SEO friendly name
/// </summary>
/// <param name="name">Name</param>
/// <returns>Result</returns>
public new virtual string GetPictureSeName(string name)
{
return SeoExtensions.GetSeName(name, true, false);
}
/// <summary>
/// Gets the default picture URL
/// </summary>
/// <param name="targetSize">The target picture size (longest side)</param>
/// <param name="defaultPictureType">Default picture type</param>
/// <param name="storeLocation">Store location URL; null to use determine the current store location automatically</param>
/// <returns>Picture URL</returns>
public new virtual string GetDefaultPictureUrl(int targetSize = 0,
PictureType defaultPictureType = PictureType.Entity,
string storeLocation = null)
{
string defaultImageFileName;
switch (defaultPictureType)
{
case PictureType.Entity:
defaultImageFileName = _settingService.GetSettingByKey("Media.DefaultImageName", "default-image.gif");
break;
case PictureType.Avatar:
defaultImageFileName = _settingService.GetSettingByKey("Media.Customer.DefaultAvatarImageName", "default-avatar.jpg");
break;
default:
defaultImageFileName = _settingService.GetSettingByKey("Media.DefaultImageName", "default-image.gif");
break;
}
string filePath = GetPictureLocalPath(defaultImageFileName);
if (_mediaSettings.AWSS3Enable)
{
if (!S3ExistsImage(defaultImageFileName))
{
return "";
}
}
else
{
if (!File.Exists(filePath))
{
return "";
}
}
if (targetSize == 0)
{
if (_mediaSettings.AWSS3Enable)
{
return $"{_mediaSettings.AWSS3RootUrl}/{_mediaSettings.AWSS3BucketName}/images/{defaultImageFileName}";
}
else
{
string url = (!String.IsNullOrEmpty(storeLocation)
? storeLocation
: _webHelper.GetStoreLocation())
+ "/images/" + defaultImageFileName;
return url;
}
}
else
{
if (_mediaSettings.AWSS3Enable)
{
string fileExtension = Path.GetExtension(filePath);
string thumbFileName = string.Format("{0}_{1}{2}",
Path.GetFileNameWithoutExtension(filePath),
targetSize,
fileExtension);
if (!S3ExistsImageOnThumbs(thumbFileName))
{
//Read from S3
var pictureBinary = S3ReadAllBytes(defaultImageFileName);
using (var stream = new MemoryStream(pictureBinary))
{
Bitmap b = null;
try
{
//try-catch to ensure that picture binary is really OK. Otherwise, we can get "Parameter is not valid" exception if binary is corrupted for some reasons
b = new Bitmap(stream);
}
catch (ArgumentException exc)
{
_logger.Error("Error generating default picture",exc);
}
var newSize = CalculateDimensions(b.Size, targetSize);
try
{
var destStream = new MemoryStream();
ImageBuilder.Current.Build(b, destStream, new ResizeSettings()
{
Width = newSize.Width,
Height = newSize.Height,
Scale = ScaleMode.Both,
Quality = _mediaSettings.DefaultImageQuality
});
var destBinary = destStream.ToArray();
S3UploadImageOnThumbs(thumbFileName, destBinary, fileExtension);
}
catch
{
}
b.Dispose();
}
}
var url = GetThumbUrl(thumbFileName, storeLocation);
return url;
}
else
{
string fileExtension = Path.GetExtension(filePath);
string thumbFileName = string.Format("{0}_{1}{2}",
Path.GetFileNameWithoutExtension(filePath),
targetSize,
fileExtension);
var thumbFilePath = GetThumbLocalPath(thumbFileName);
if (!File.Exists(thumbFilePath))
{
using (var b = new Bitmap(filePath))
{
var newSize = CalculateDimensions(b.Size, targetSize);
var destStream = new MemoryStream();
ImageBuilder.Current.Build(b, destStream, new ResizeSettings()
{
Width = newSize.Width,
Height = newSize.Height,
Scale = ScaleMode.Both,
Quality = _mediaSettings.DefaultImageQuality
});
var destBinary = destStream.ToArray();
File.WriteAllBytes(thumbFilePath, destBinary);
}
}
var url = GetThumbUrl(thumbFileName, storeLocation);
return url;
}
}
}
/// <summary>
/// Get a picture URL
/// </summary>
/// <param name="pictureId">Picture identifier</param>
/// <param name="targetSize">The target picture size (longest side)</param>
/// <param name="showDefaultPicture">A value indicating whether the default picture is shown</param>
/// <param name="storeLocation">Store location URL; null to use determine the current store location automatically</param>
/// <param name="defaultPictureType">Default picture type</param>
/// <returns>Picture URL</returns>
public new virtual string GetPictureUrl(int pictureId,
int targetSize = 0,
bool showDefaultPicture = true,
string storeLocation = null,
PictureType defaultPictureType = PictureType.Entity)
{
var picture = GetPictureById(pictureId);
return GetPictureUrl(picture, targetSize, showDefaultPicture, storeLocation, defaultPictureType);
}
/// <summary>
/// Get a picture URL(BASE) form original file store/S3
/// </summary>
/// <param name="picture">Picture instance</param>
/// <param name="targetSize">The target picture size (longest side)</param>
/// <param name="showDefaultPicture">A value indicating whether the default picture is shown</param>
/// <param name="storeLocation">Store location URL; null to use determine the current store location automatically</param>
/// <param name="defaultPictureType">Default picture type</param>
/// <returns>Picture URL</returns>
public virtual string GetPictureUrlBase(Picture picture,
int targetSize = 0,
bool showDefaultPicture = true,
string storeLocation = null,
PictureType defaultPictureType = PictureType.Entity)
{
if (picture == null)
{
if (showDefaultPicture)
return GetDefaultPictureUrl(targetSize, defaultPictureType, storeLocation);
return "";
}
string thumbFileName;
string thumbFilePath;
string seoFileName = picture.SeoFilename;
string lastPart = GetFileExtensionFromMimeType(picture.MimeType);
if (targetSize == 0)
{
thumbFileName = !string.IsNullOrEmpty(seoFileName) ?
string.Format("{0}_{1}.{2}", picture.Id.ToString("0000000"), seoFileName, lastPart) :
string.Format("{0}.{1}", picture.Id.ToString("0000000"), lastPart);
}
else
{
thumbFileName = !string.IsNullOrEmpty(seoFileName) ?
string.Format("{0}_{1}_{2}.{3}", picture.Id.ToString("0000000"), seoFileName, targetSize, lastPart) :
string.Format("{0}_{1}.{2}", picture.Id.ToString("0000000"), targetSize, lastPart);
}
thumbFilePath = GetThumbLocalPath(thumbFileName);
if (_mediaSettings.AWSS3Enable)
{
if (!S3ExistsImageOnThumbs(thumbFileName))
{
return GetResizedPictureUrl(picture, targetSize, showDefaultPicture, storeLocation, defaultPictureType, thumbFileName, thumbFilePath, true);
}
}
else
{
if (!File.Exists(thumbFilePath))
{
return GetResizedPictureUrl(picture, targetSize, showDefaultPicture, storeLocation, defaultPictureType, thumbFileName, thumbFilePath, false);
}
}
return thumbFilePath;
}
protected string GetResizedPictureUrl(Picture picture,
int targetSize,
bool showDefaultPicture,
string storeLocation,
PictureType defaultPictureType,
string thumbFileName,
string thumbFilePath,
bool s3Enabled)
{
byte[] pictureBinary = null;
if (picture != null)
{
pictureBinary = LoadPictureBinary(picture);
}
if (picture == null || pictureBinary == null || pictureBinary.Length == 0)
{
if (showDefaultPicture)
{
return GetDefaultPictureUrl(targetSize, defaultPictureType, storeLocation);
}
return "";
}
if (targetSize > 0)
{
using (var stream = new MemoryStream(pictureBinary))
{
Bitmap b = null;
try
{
//try-catch to ensure that picture binary is really OK. Otherwise, we can get "Parameter is not valid" exception if binary is corrupted for some reasons
b = new Bitmap(stream);
}
catch (ArgumentException exc)
{
_logger.Error(string.Format("Error generating picture thumb. ID={0}", picture.Id), exc);
}
if (b == null)
{
//bitmap could not be loaded for some reasons
return "";
}
var newSize = CalculateDimensions(b.Size, targetSize);
var destStream = new MemoryStream();
ImageBuilder.Current.Build(b, destStream, new ResizeSettings()
{
Width = newSize.Width,
Height = newSize.Height,
Scale = ScaleMode.Both,
Quality = _mediaSettings.DefaultImageQuality
});
byte[] destBinary = destStream.ToArray();
if (s3Enabled)
S3UploadImageOnThumbs(thumbFileName, destBinary, picture.MimeType);
else
File.WriteAllBytes(thumbFilePath, destBinary);
b.Dispose();
}
}
else
{
if (s3Enabled)
S3UploadImageOnThumbs(thumbFileName, pictureBinary, picture.MimeType);
else
File.WriteAllBytes(thumbFilePath, pictureBinary);
}
return thumbFilePath;
}
/// <summary>
/// Get Picture Url
/// </summary>
/// <param name="picture"></param>
/// <param name="targetSize"></param>
/// <param name="showDefaultPicture"></param>
/// <param name="storeLocation"></param>
/// <param name="defaultPictureType"></param>
/// <returns></returns>
public new virtual string GetPictureUrl(Picture picture,
int targetSize = 0,
bool showDefaultPicture = true,
string storeLocation = null,
PictureType defaultPictureType = PictureType.Entity)
{
string url = GetPictureUrlBase(picture, targetSize, showDefaultPicture, storeLocation, defaultPictureType);
if (_mediaSettings.EnableCdn)
{
if (_mediaSettings.AWSS3Enable)
url = url
.Replace(_mediaSettings.AWSS3BucketName+"/", "")
.Replace(_mediaSettings.AWSS3RootUrl.TrimEnd('/'), _mediaSettings.CdnBaseUrl.TrimEnd('/'));
else
url=url.Replace($"{_webHelper.GetStoreLocation()}content", _mediaSettings.CdnBaseUrl.TrimEnd('/'));
}
return url;
}
/// <summary>
/// Get a picture local path
/// </summary>
/// <param name="picture">Picture instance</param>
/// <param name="targetSize">The target picture size (longest side)</param>
/// <param name="showDefaultPicture">A value indicating whether the default picture is shown</param>
/// <returns></returns>
public new virtual string GetThumbLocalPath(Picture picture, int targetSize = 0, bool showDefaultPicture = true)
{
string url = GetPictureUrlBase(picture, targetSize, showDefaultPicture);
if (String.IsNullOrEmpty(url))
return String.Empty;
else
return GetThumbLocalPath(Path.GetFileName(url));
}
/// <summary>
/// Inserts a picture
/// </summary>
/// <param name="pictureBinary">The picture binary</param>
/// <param name="mimeType">The picture MIME type</param>
/// <param name="seoFilename">The SEO filename</param>
/// <param name="isNew">A value indicating whether the picture is new</param>
/// <param name="validateBinary">A value indicating whether to validated provided picture binary</param>
/// <returns>Picture</returns>
public new virtual Picture InsertPicture(byte[] pictureBinary, string mimeType, string seoFilename,
string altAttribute = null, string titleAttribute = null,
bool isNew = true, bool validateBinary = true)
{
mimeType = CommonHelper.EnsureNotNull(mimeType);
mimeType = CommonHelper.EnsureMaximumLength(mimeType, 20);
seoFilename = CommonHelper.EnsureMaximumLength(seoFilename, 100);
if (validateBinary)
pictureBinary = ValidatePicture(pictureBinary, mimeType);
var picture = new Picture()
{
PictureBinary = this.StoreInDb ? pictureBinary : new byte[0],
MimeType = mimeType,
SeoFilename = seoFilename,
IsNew = isNew,
};
_pictureRepository.Insert(picture);
if (!this.StoreInDb)
SavePictureInFile(picture.Id, pictureBinary, mimeType);
//event notification
_eventPublisher.EntityInserted(picture);
return picture;
}
#endregion
#region AWS Helper
/// <summary>
/// Get S3 Image Path
/// </summary>
/// <returns></returns>
public string GetS3ImagePath()
{
return $"{_mediaSettings.AWSS3BucketName}/images";
}
/// <summary>
/// Get S3 image thumb path
/// </summary>
/// <returns></returns>
public string GetS3ImageThumbPath()
{
return $"{_mediaSettings.AWSS3BucketName}/images/thumbs";
}
/// <summary>
/// Get S3 image path (local)
/// </summary>
/// <returns></returns>
public string GetS3ImageLocalPath()
{
return $"{_mediaSettings.AWSS3RootUrl}/{_mediaSettings.AWSS3BucketName}/images";
}
/// <summary>
/// Get S3 thumb image path (local)
/// </summary>
/// <returns></returns>
public string GetS3ImageThumbLocalPath()
{
return $"{_mediaSettings.AWSS3RootUrl}/{_mediaSettings.AWSS3BucketName}/images/thumbs";
}
/// <summary>
/// Check S3 image exists or not
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public bool S3ExistsImage(string fileName)
{
try
{
using (var client= GetS3Client())
{
var file = new S3FileInfo(client, GetS3ImagePath(), fileName);
return file.Exists;
}
}
catch
{
throw;
}
}
/// <summary>
/// Exists image on thumbs
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public bool S3ExistsImageOnThumbs(string fileName)
{
try
{
using (var client = GetS3Client())
{
var file = new S3FileInfo(client, GetS3ImageThumbPath(), fileName);
return file.Exists;
}
}
catch
{
return false;
}
}
/// <summary>
/// ReadAllBytes form S3
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public byte[] S3ReadAllBytes(string fileName)
{
//Stream rs;
using (var client = GetS3Client())
{
var getObjectRequest = new GetObjectRequest()
{
BucketName = GetS3ImagePath(),
Key = fileName
};
using (var getObjectResponse = client.GetObjectAsync(getObjectRequest))
{
using (var mutex = new Mutex(false))
{
using (Stream responseStream = getObjectResponse.Result.ResponseStream)
{
var bytes = S3ReadStream(responseStream);
return bytes;
}
}
}
}
}
/// <summary>
/// Read image form thumbs
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public byte[] S3ReadAllBytesFromThumbs(string fileName)
{
//Stream rs;
using (var client = GetS3Client())
{
var getObjectRequest = new GetObjectRequest()
{
BucketName = GetS3ImageThumbPath(),
Key = fileName
};
using (var getObjectResponse = client.GetObjectAsync(getObjectRequest))
{
using (Stream responseStream = getObjectResponse.Result.ResponseStream)
{
var bytes = S3ReadStream(responseStream);
return bytes;
}
}
}
}
/// <summary>
/// Read Stream From S3
/// </summary>
/// <param name="responseStream"></param>
/// <returns></returns>
public byte[] S3ReadStream(Stream responseStream)
{
byte[] buffer = new byte[16 * 1024];
using (var mutex = new Mutex(false))
{
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = responseStream.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
}
/// <summary>
/// Upload image to S3
/// </summary>
/// <param name="fileName"></param>
/// <param name="pictureBinary"></param>
/// <param name="mimeType"></param>
public void S3UploadImage(string fileName, byte[] pictureBinary, string mimeType)
{
try
{
var client = GetS3Client();
var putRequest = new PutObjectRequest
{
BucketName = GetS3ImagePath(),
Key = fileName,
InputStream = new MemoryStream(pictureBinary),
ContentType = mimeType,
};
putRequest.CannedACL = S3CannedACL.PublicRead;
putRequest.Headers.Expires = DateTime.UtcNow.AddDays(_mediaSettings.ExpiresDays);
PutObjectResponse response = client.PutObject(putRequest);
}
catch (AmazonS3Exception ex)
{
if (ex.ErrorCode != null && (ex.ErrorCode.Equals("InvalidAccessKeyId") || ex.ErrorCode.Equals("InvalidSecurity")))
{
_logger.InsertLog(Core.Domain.Logging.LogLevel.Error, ex.Message + " ## Check Crendentials", ex.StackTrace);
}
else
{
_logger.InsertLog(Core.Domain.Logging.LogLevel.Error, $"Error occurred. Message:{ex.Message} when writing an object", ex.StackTrace);
}
}