-
Notifications
You must be signed in to change notification settings - Fork 0
/
OptimizeBlobsImages.cs
205 lines (170 loc) · 6.78 KB
/
OptimizeBlobsImages.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
using System;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Principal;
using System.Threading;
using System.Web;
using EPiServer;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.DataAccess;
using EPiServer.Framework.Blobs;
using EPiServer.Logging;
using EPiServer.PlugIn;
using EPiServer.Scheduler;
using EPiServer.Security;
using EPiServer.ServiceLocation;
namespace Web.Business.Initialization
{
[ScheduledPlugIn(DisplayName = "OptimizeBlobsImages",
Description = "Looks up all the images on the site and compresses them to an optimal state. After the job has ran, please run \"Clear Thumbnail Properties\" to force EpiServer to refresh the images.",
IntervalLength = 300000, IntervalType = ScheduledIntervalType.Minutes)]
public class OptimizeBlobsImages : ScheduledJobBase
{
private static ILogger _log = LogManager.Instance.GetLogger("RewardImportLog");
private bool _stopSignaled;
private static readonly object TaskLock = new object();
private IBlobFactory _blobResolver;
private readonly IContentLoader _readRepo = ServiceLocator.Current.GetInstance<IContentLoader>();
private readonly IContentRepository _writeRepo = ServiceLocator.Current.GetInstance<IContentRepository>();
private readonly ImageCompressor _imageCompressor;
public OptimizeBlobsImages()
{
var parseStatus = int.TryParse(ConfigurationManager.AppSettings["QualityLevel"], out int compressionLevel);
_imageCompressor = new ImageCompressor(parseStatus ? compressionLevel : 75);
IsStoppable = true;
}
public override void Stop()
{
_stopSignaled = true;
}
public override string Execute()
{
if (!Monitor.TryEnter(TaskLock))
{
return "Job is already running.";
}
try
{
#region Set Principal Admin...
if (HttpContext.Current == null)
{
PrincipalInfo.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("Import/Update Rewards"),
new[]
{
"knowitadmin"
});
}
if (PrincipalInfo.CurrentPrincipal.Identity.Name == string.Empty)
{
_log.Log(Level.Error, "OBS! ===> Anonymous execution! ;(");
return "OBS! ===> Anonymous execution!";
}
#endregion
GetAllBlobs();
}
catch (Exception exc)
{
_log.Error(exc.Message);
_log.Error(exc.StackTrace);
return string.Format("Unknown exception caught ({0} {1})",
new object[] { exc.Message, exc.StackTrace });
}
finally
{
Monitor.Exit(TaskLock);
}
if (_stopSignaled)
{
return "Stop of job was called";
}
return $"Success: Optimized {ImageCompressor.OptimizedImagesCount} images (Saved: {ImageCompressor.TotallySavedMbs} mb)." +
$"\nSkipped {ImageCompressor.SkippedImagesCount} images.";
}
public void GetAllBlobs()
{
_blobResolver = ServiceLocator.Current.GetInstance<IBlobFactory>();
var globalAssetsRoot = EPiServer.Web.SiteDefinition.Current.GlobalAssetsRoot;
var mediaChildRefs = _readRepo.GetDescendents(globalAssetsRoot);
foreach (var child in mediaChildRefs)
{
var image = _readRepo.TryGet<ImageData>(child, out var imageData);
if (!image) continue;
var blobLink = UrlHelper.GetExternalUrlNoHttpContext(imageData.ContentLink);
if (!RemoteFileExists(blobLink))
{
continue;
}
CompressBlob(imageData);
}
_log.Error(
$"Success: Optimized {ImageCompressor.OptimizedImagesCount} images (Saved: {ImageCompressor.TotallySavedMbs} mb)." +
$"\nSkipped {ImageCompressor.SkippedImagesCount} images.");
}
public void CompressBlob(ImageData image)
{
byte[] originalImage;
using (var memoryStream = new MemoryStream())
{
image.BinaryData.OpenRead().CopyTo(memoryStream);
originalImage = memoryStream.ToArray();
}
byte[] compressedResult = {};
switch (image.MimeType)
{
case "image/jpeg":
compressedResult = _imageCompressor.MakeCompressedJpg(originalImage);
break;
case "image/png":
compressedResult = _imageCompressor.MakeCompressedPng(originalImage);
break;
default:
_log.Error("!unknown file:" + image.MimeType);
break;
}
if (ImageCompressor.OptimizedImagesCount % 50 == 0 && ImageCompressor.OptimizedImagesCount !=0)
{
OnStatusChanged($"Updated {ImageCompressor.OptimizedImagesCount}...");
}
if (compressedResult == null) { return; }
//Create the Compressed Blob.
var writableImage = image.CreateWritableClone() as ImageFile;
if (writableImage != null)
{
var blob = _blobResolver.CreateBlob(writableImage.BinaryDataContainer, Path.GetExtension(image.Name));
using (var s = blob.OpenWrite())
{
var b = new BinaryWriter(s);
b.Write(compressedResult);
b.Flush();
}
writableImage.BinaryData = blob;
_writeRepo.Save(writableImage, SaveAction.Publish, AccessLevel.NoAccess);
}
}
private bool RemoteFileExists(string url)
{
try
{
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
if (request != null)
{
request.Method = "HEAD";
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
if (response != null)
{
response.Close();
return (response.StatusCode == HttpStatusCode.OK);
}
}
return false;
}
catch
{
return false;
}
}
}
}