-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHttpHandler.cs
237 lines (206 loc) · 7.36 KB
/
HttpHandler.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
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="HttpHandler.cs" company="Vizioz Limited">
// This code is Open Source cover by the attached MIT License (MIT)
// Originally developed by Vizioz Limited
// </copyright>
// <summary>
// The http handler.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace DynamicIcons
{
using System;
using System.Configuration;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using ImageProcessor;
/// <summary>
/// The http handler.
/// </summary>
public class HttpHandler : IHttpHandler
{
/// <summary>
/// Initializes a new instance of the <see cref="HttpHandler"/> class.
/// </summary>
/// <param name="isReusable">
/// The is reusable.
/// </param>
public HttpHandler(bool isReusable)
{
this.IsReusable = isReusable;
}
/// <summary>
/// Initializes a new instance of the <see cref="HttpHandler"/> class.
/// </summary>
public HttpHandler()
{
// TEST
}
/// <summary>
/// Gets or sets a value indicating whether is reusable.
/// </summary>
public bool IsReusable { get; set; }
/// <summary>
/// The process request.
/// </summary>
/// <param name="context">
/// The context.
/// </param>
public void ProcessRequest(HttpContext context)
{
int quality = 80;
byte[] photoBytes;
var iconType = SupportedIconTypes(context);
if (iconType == DynamicIconType.Unsupported)
{
return;
}
var appSettingsReader = new AppSettingsReader();
string dynamicIcon;
try
{
dynamicIcon = (string)appSettingsReader.GetValue("dynamicIcon", typeof(string));
}
catch (Exception)
{
return;
}
quality = GetQuality(context, appSettingsReader, dynamicIcon, out photoBytes, quality);
// This is in preparation for future Icon formats to be supported.
switch (iconType)
{
case DynamicIconType.AppleTouchIcon:
GetAppleTouchIcon(context, photoBytes, quality);
break;
}
}
/// <summary>
/// The get quality.
/// </summary>
/// <param name="context">
/// The context.
/// </param>
/// <param name="appSettingsReader">
/// The app settings reader.
/// </param>
/// <param name="dynamicIcon">
/// The dynamic icon.
/// </param>
/// <param name="photoBytes">
/// The photo bytes.
/// </param>
/// <param name="quality">
/// The quality.
/// </param>
/// <returns>
/// The <see cref="int"/>.
/// </returns>
private static int GetQuality(
HttpContext context,
AppSettingsReader appSettingsReader,
string dynamicIcon,
out byte[] photoBytes,
int quality)
{
string dynamicIconQuality;
try
{
dynamicIconQuality = (string)appSettingsReader.GetValue("dynamicIconQuality", typeof(string));
}
catch (Exception)
{
dynamicIconQuality = "80";
}
var dynamicIconPath = context.Server.MapPath(dynamicIcon);
photoBytes = File.ReadAllBytes(dynamicIconPath);
if (dynamicIconQuality.Trim() == string.Empty)
{
return quality;
}
int result;
int.TryParse(dynamicIconQuality, out result);
if (result > 0)
{
quality = result;
}
return quality;
}
/// <summary>
/// The get apple touch icon.
/// </summary>
/// <param name="context">
/// The context.
/// </param>
/// <param name="photoBytes">
/// The photo bytes.
/// </param>
/// <param name="quality">
/// The quality.
/// </param>
private static void GetAppleTouchIcon(HttpContext context, byte[] photoBytes, int quality)
{
var format = ImageFormat.Png;
var imageName = context.Request.Url.Segments.Last().ToLower().Substring(17).Replace("-precomposed", string.Empty);
string width = string.Empty;
string height = string.Empty;
if (imageName.Contains("x"))
{
width = imageName.Substring(0, imageName.IndexOf("x", StringComparison.Ordinal));
height = imageName.Substring(
imageName.IndexOf("x", StringComparison.Ordinal) + 1,
imageName.IndexOf(".", StringComparison.Ordinal) - (imageName.IndexOf("x", StringComparison.Ordinal) + 1));
}
int widthInt, heightInt;
var size = new Size();
if (int.TryParse(width, out widthInt) && int.TryParse(height, out heightInt))
{
size.Width = widthInt;
size.Height = heightInt;
}
else
{
size.Width = 156;
size.Height = 156;
}
using (var inStream = new MemoryStream(photoBytes))
{
using (var outStream = new MemoryStream())
{
// Initialize the ImageFactory using the overload to preserve EXIF metadata.
using (var imageFactory = new ImageFactory(true))
{
// Load, resize, set the format and quality and save an image.
imageFactory.Load(inStream).Resize(size).Format(format).Quality(quality).Save(outStream);
}
context.Response.Clear();
context.Response.ContentType = "image/png";
string headerValue = string.Format("attachment; filename={0}", context.Request.Url.Segments.Last());
context.Response.AppendHeader("Content-Disposition", headerValue);
outStream.WriteTo(context.Response.OutputStream);
context.Response.End();
}
}
}
/// <summary>
/// Method to check if the current request is a supported icon type.
/// </summary>
/// <param name="context">
/// The current request context.
/// </param>
/// <returns>
/// The icon type
/// </returns>
private static DynamicIconType SupportedIconTypes(HttpContext context)
{
// As additional formats are added they will need to be added to this statement.
if (context.Request.Url.Segments.Last().ToLower().Contains("apple-touch-icon"))
{
return DynamicIconType.AppleTouchIcon;
}
return DynamicIconType.Unsupported;
}
}
}