-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageMaxSizeHandler.cs
125 lines (106 loc) · 5.37 KB
/
ImageMaxSizeHandler.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
using DM.Core.Components.Imaging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.IO;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.PropertyEditors.ValueConverters;
using Umbraco.Cms.Core.Services;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Formats.Gif;
using Microsoft.Extensions.Configuration;
namespace DM.Core.Components.Imaging
{
public class ImageMaxSizeHandler : INotificationHandler<MediaSavedNotification>
{
private readonly MediaFileManager _mediaFileSystem;
private readonly IMediaService _mediaService;
private readonly IContentTypeBaseServiceProvider _contentTypeBaseServiceProvider;
private readonly ImageMaxSizeConfig _imagingConfig;
private readonly ContentSettings _contentSettings;
public ImageMaxSizeHandler(IMediaService mediaService, MediaFileManager mediaFileSystem, IOptions<ContentSettings> contentSettings, IContentTypeBaseServiceProvider contentTypeBaseServiceProvider, IConfiguration configuration)
{
_imagingConfig = ImageMaxSizeConfig.GetConfigInstance(configuration);
_mediaFileSystem = mediaFileSystem;
_mediaService = mediaService;
_contentTypeBaseServiceProvider = contentTypeBaseServiceProvider;
_contentSettings = contentSettings.Value;
}
public void Handle(MediaSavedNotification notification)
{
string[] supportedTypes = _contentSettings.Imaging.ImageFileTypes;
int maxWidth = _imagingConfig.MaxWidth;
int maxHeight = _imagingConfig.MaxHeight;
if(maxWidth == 0 && maxHeight == 0)
{
maxWidth = 1920;
}
foreach (IMedia media in notification.SavedEntities)
{
string UmbracoFileAlias = _contentSettings.Imaging.AutoFillImageProperties?.FirstOrDefault().Alias; //This could be hardcoded to "umbracoFile" but probably better this way
if (media.ContentType.Alias == "Image" && media.HasProperty(UmbracoFileAlias))
{
string path = null;
try
{
var umbracoFile = JsonConvert.DeserializeObject<Umbraco.Cms.Core.PropertyEditors.ValueConverters.ImageCropperValue>(media.GetValue<string>(UmbracoFileAlias));
path = umbracoFile.Src;
}
catch
{
path = media.GetValue<string>(UmbracoFileAlias); //Do we still need this?
}
if (path != null)
{
string extension = Path.GetExtension(path).ToLower();
if (!string.IsNullOrEmpty(extension)) //Check to see if it has a path
{
extension = extension.Substring(1);
if (supportedTypes.Contains(extension))
{
// Resize the image to 1920px wide, height is driven by the
// aspect ratio of the image.
string fullPath = _mediaFileSystem.FileSystem.GetFullPath(path);
if (_mediaFileSystem.FileSystem.FileExists(path))
{
MemoryStream outStream = new MemoryStream();
using (var imgStream = _mediaFileSystem.FileSystem.OpenFile(path))
{
IImageFormat format;
using (Image image = Image.Load(imgStream, out format))
{
//Check to see if the image is too big and then resize
if ((maxWidth != 0 && image.Width > maxWidth) || (maxHeight != 0 && image.Height > maxHeight))
{
//Bigger than allowed, so resize
image.Mutate(x => x.Resize(new Size(maxWidth, maxHeight)));
image.Save(outStream, format);
}
}
}
if (outStream.Length > 0)
{
//We have an updated image file
_mediaFileSystem.FileSystem.AddFile(path, outStream, true);
_mediaService.Save(media); //This updates the correct image size
}
}
}
}
}
}
}
}
}
}