/
BinaryFileManager.cs
341 lines (318 loc) · 14.8 KB
/
BinaryFileManager.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
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using System.Web;
using Sdl.Web.Common;
using Sdl.Web.Common.Configuration;
using Sdl.Web.Common.Logging;
using Tridion.ContentDelivery.DynamicContent;
using Tridion.ContentDelivery.Meta;
using Image = System.Drawing.Image; // TODO: Shouldn't use System.Drawing namespace in a web application.
namespace Sdl.Web.Tridion.Statics
{
/// <summary>
/// Ensures a Binary file is cached on the file-system from the Tridion Broker DB
/// </summary>
public class BinaryFileManager
{
private static readonly BinaryFileManager _instance = new BinaryFileManager();
#region Inner classes
internal class Dimensions
{
internal int Width;
internal int Height;
internal bool NoStretch;
/// <summary>
/// Returns a string that represents the current object.
/// </summary>
/// <returns>
/// A string that represents the current object.
/// </returns>
public override string ToString()
{
return string.Format("(W={0}, H={1}, NoStretch={2})", Width, Height, NoStretch);
}
}
#endregion
/// <summary>
/// Gets the singleton BinaryFileManager instance.
/// </summary>
internal static BinaryFileManager Instance
{
get
{
return _instance;
}
}
private static BinaryMeta GetBinaryMeta(string urlPath, int publicationId)
{
BinaryMetaFactory binaryMetaFactory = new BinaryMetaFactory();
return binaryMetaFactory.GetMetaByUrl(publicationId, urlPath);
}
private static DateTime GetBinaryLastPublishDate(string urlPath, int publicationId)
{
BinaryMeta binaryMeta = GetBinaryMeta(urlPath, publicationId);
if (binaryMeta == null || !binaryMeta.IsComponent)
{
return DateTime.MinValue;
}
ComponentMetaFactory componentMetaFactory = new ComponentMetaFactory(publicationId);
IComponentMeta componentMeta = componentMetaFactory.GetMeta(binaryMeta.Id);
return componentMeta.LastPublicationDate;
}
/// <summary>
/// Gets the cached local file for a given URL path.
/// </summary>
/// <param name="urlPath">The URL path.</param>
/// <param name="localization">The Localization.</param>
/// <returns>The path to the local file.</returns>
internal string GetCachedFile(string urlPath, Localization localization)
{
string localFilePath = GetFilePathFromUrl(urlPath, localization);
using (new Tracer(urlPath, localization, localFilePath))
{
Dimensions dimensions;
urlPath = StripDimensions(urlPath, out dimensions);
int publicationId = Convert.ToInt32(localization.LocalizationId);
if (File.Exists(localFilePath))
{
DateTime lastPublishedDate = SiteConfiguration.CacheProvider.GetOrAdd(
urlPath,
CacheRegions.BinaryPublishDate,
() => GetBinaryLastPublishDate(urlPath, publicationId)
);
if (localization.LastRefresh.CompareTo(lastPublishedDate) < 0)
{
//File has been modified since last application start but we don't care
Log.Debug(
"Binary with URL '{0}' is modified, but only since last application restart, so no action required",
urlPath);
return localFilePath;
}
FileInfo fi = new FileInfo(localFilePath);
if (fi.Length > 0)
{
DateTime fileModifiedDate = File.GetLastWriteTime(localFilePath);
if (fileModifiedDate.CompareTo(lastPublishedDate) >= 0)
{
Log.Debug("Binary with URL '{0}' is still up to date, no action required", urlPath);
return localFilePath;
}
}
}
// Binary does not exist or cached binary is out-of-date
BinaryMeta binaryMeta = GetBinaryMeta(urlPath, publicationId);
if (binaryMeta == null)
{
throw new DxaItemNotFoundException(urlPath, localization.LocalizationId);
}
BinaryFactory binaryFactory = new BinaryFactory();
BinaryData binaryData = binaryFactory.GetBinary(publicationId, binaryMeta.Id, binaryMeta.VariantId);
WriteBinaryToFile(binaryData.Bytes, localFilePath, dimensions);
return localFilePath;
}
}
private static string GetFilePathFromUrl(string urlPath, Localization loc)
{
// TODO: not nice to use HttpContext at this level
HttpContext httpContext = HttpContext.Current;
return (httpContext == null) ?
string.Format("{0}/{1}", SiteConfiguration.GetLocalStaticsFolder(loc.LocalizationId), Uri.UnescapeDataString(urlPath)) :
httpContext.Server.MapPath("~/" + SiteConfiguration.GetLocalStaticsFolder(loc.LocalizationId) + urlPath);
}
/// <summary>
/// Perform actual write of binary content to file
/// </summary>
/// <param name="binary">The binary to store</param>
/// <param name="physicalPath">String the file path to write to</param>
/// <param name="dimensions">Dimensions of file</param>
/// <returns>True is binary was written to disk, false otherwise</returns>
private static bool WriteBinaryToFile(byte[] binary, String physicalPath, Dimensions dimensions)
{
using (new Tracer(binary, physicalPath, dimensions))
{
bool result = true;
try
{
if (!File.Exists(physicalPath))
{
FileInfo fileInfo = new FileInfo(physicalPath);
if (fileInfo.Directory != null && !fileInfo.Directory.Exists)
{
fileInfo.Directory.Create();
}
}
byte[] buffer = binary;
if (dimensions != null && (dimensions.Width > 0 || dimensions.Height > 0))
{
buffer = ResizeImage(buffer, dimensions, GetImageFormat(physicalPath));
}
lock (NamedLocker.GetLock(physicalPath))
{
using (FileStream fileStream = new FileStream(physicalPath, FileMode.Create, FileAccess.ReadWrite, FileShare.Read))
{
fileStream.Write(buffer, 0, buffer.Length);
}
}
}
catch (IOException)
{
// file probabaly accessed by a different thread in a different process, locking failed
Log.Warn("Cannot write to {0}. This can happen sporadically, let the next thread handle this.", physicalPath);
result = false;
}
return result;
}
}
private static void CleanupLocalFile(string physicalPath)
{
using (new Tracer(physicalPath))
{
try
{
// file got unpublished
File.Delete(physicalPath);
NamedLocker.RemoveLock(physicalPath);
}
catch (IOException)
{
// file probabaly accessed by a different thread in a different process
Log.Warn("Cannot delete '{0}'. This can happen sporadically, let the next thread handle this.", physicalPath);
}
}
}
private static byte[] ResizeImage(byte[] imageData, Dimensions dimensions, ImageFormat imageFormat)
{
using (new Tracer(imageData.Length, dimensions, imageFormat))
{
Image original = Image.FromStream(new MemoryStream(imageData));
//Defaults for crop position, width and target size
int cropX = 0, cropY = 0;
int sourceW = original.Width, sourceH = original.Height;
int targetW = original.Width, targetH = original.Height;
//Most complex case is if a height AND width is specified
if (dimensions.Width > 0 && dimensions.Height > 0)
{
if (dimensions.NoStretch)
{
//If we don't want to stretch, then we crop
float originalAspect = (float)original.Width / (float)original.Height;
float targetAspect = (float)dimensions.Width / (float)dimensions.Height;
if (targetAspect < originalAspect)
{
//Crop the width - ensuring that we do not stretch if the requested height is bigger than the original
targetH = dimensions.Height > original.Height ? original.Height : dimensions.Height;
targetW = (int)Math.Ceiling(targetH * targetAspect);
cropX = (int)Math.Ceiling((original.Width - (original.Height * targetAspect)) / 2);
sourceW = sourceW - 2 * cropX;
}
else
{
//Crop the height - ensuring that we do not stretch if the requested width is bigger than the original
targetW = dimensions.Width > original.Width ? original.Width : dimensions.Width;
targetH = (int)Math.Ceiling(targetW / targetAspect);
cropY = (int)Math.Ceiling((original.Height - (original.Width / targetAspect)) / 2);
sourceH = sourceH - 2 * cropY;
}
}
else
{
//We stretch to fit the dimensions
targetH = dimensions.Height;
targetW = dimensions.Width;
}
}
//If we simply have a certain width or height, its simple: We just use that and derive the other
//dimension from the original image aspect ratio. We also check if the target size is bigger than
//the original, and if we allow stretching.
else if (dimensions.Width > 0)
{
targetW = (dimensions.NoStretch && dimensions.Width > original.Width) ? original.Width : dimensions.Width;
targetH = (int)(original.Height * ((float)targetW / (float)original.Width));
}
else
{
targetH = (dimensions.NoStretch && dimensions.Height > original.Height) ? original.Height : dimensions.Height;
targetW = (int)(original.Width * ((float)targetH / (float)original.Height));
}
if (targetW == original.Width && targetH == original.Height)
{
//No resize required
return imageData;
}
Image imgPhoto;
using (MemoryStream memoryStream = new MemoryStream(imageData))
{
imgPhoto = Image.FromStream(memoryStream);
}
// Create a new blank canvas. The resized image will be drawn on this canvas.
Bitmap bmPhoto = new Bitmap(targetW, targetH, PixelFormat.Format24bppRgb);
Bitmap bmOriginal = new Bitmap(original);
bmPhoto.SetResolution(72, 72);
bmPhoto.MakeTransparent(bmOriginal.GetPixel(0, 0));
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;
grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, targetW, targetH), cropX, cropY, sourceW, sourceH, GraphicsUnit.Pixel);
// Save out to memory and then to a file. We dispose of all objects to make sure the files don't stay locked.
using (MemoryStream memoryStream = new MemoryStream())
{
bmPhoto.Save(memoryStream, imageFormat);
original.Dispose();
imgPhoto.Dispose();
bmPhoto.Dispose();
grPhoto.Dispose();
return memoryStream.GetBuffer();
}
}
}
private static ImageFormat GetImageFormat(string path)
{
switch (Path.GetExtension(path).ToLower())
{
case ".jpg":
case ".jpeg":
return ImageFormat.Jpeg;
case ".gif":
return ImageFormat.Gif;
//case ".png":
// use png as default
default:
return ImageFormat.Png;
}
}
private static string StripDimensions(string path, out Dimensions dimensions)
{
dimensions = new Dimensions();
Regex re = new Regex(@"_(w(\d+))?(_h(\d+))?(_n)?\.");
if (re.IsMatch(path))
{
Match match = re.Match(path);
string dim = match.Groups[2].ToString();
if (!String.IsNullOrEmpty(dim))
{
dimensions.Width = Convert.ToInt32(dim);
}
dim = match.Groups[4].ToString();
if (!String.IsNullOrEmpty(dim))
{
dimensions.Height = Convert.ToInt32(dim);
}
if(!String.IsNullOrEmpty(match.Groups[5].ToString()))
{
dimensions.NoStretch = true;
}
return re.Replace(path, ".");
}
// TSI-417: unescape and only escape spaces
path = WebUtility.UrlDecode(path);
path = path.Replace(" ", "%20");
return path;
}
}
}