This repository has been archived by the owner on Nov 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 104
/
Video.cs
349 lines (319 loc) · 16.5 KB
/
Video.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
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.WebPages;
using Microsoft.Internal.Web.Utils;
using Resources;
namespace Microsoft.Web.Helpers
{
public static class Video
{
private const string FlashCab =
"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab";
private const string FlashClassId = "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000";
private const string FlashMimeType = "application/x-shockwave-flash";
private const string MediaPlayerClassId = "clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6";
private const string MediaPlayerMimeType = "application/x-mplayer2";
private const string OleMimeType = "application/x-oleobject";
private const string SilverlightMimeType = "application/x-silverlight-2";
// These attributes can't be specified using anonymous objects (either because they are available as separate arguments or because
// they don't make sense in the context of the helper).
private static readonly string[] _globalBlacklist = new[] { "width", "height", "type", "data", "classid", "codebase" };
private static readonly string[] _mediaPlayerBlacklist = new[] { "autoStart", "playCount", "uiMode", "stretchToFit", "enableContextMenu", "mute", "volume", "baseURL" };
private static readonly string[] _silverlightBlacklist = new[] { "background", "initparams", "minruntimeversion", "autoUpgrade" };
private static readonly string[] _flashBlacklist = new[] { "play", "loop", "menu", "bgColor", "quality", "scale", "wmode", "base" };
private static VirtualPathUtilityWrapper _pathUtility = new VirtualPathUtilityWrapper();
#if CODE_COVERAGE
[ExcludeFromCodeCoverage]
#endif
private static HttpContextBase HttpContext
{
get
{
var httpContext = System.Web.HttpContext.Current;
return httpContext == null ? null : new HttpContextWrapper(httpContext);
}
}
// see: http://kb2.adobe.com/cps/127/tn_12701.html
#if CODE_COVERAGE
[ExcludeFromCodeCoverage]
#endif
[SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "10#",
Justification = "string parameter passed to flash in object tag")]
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1305:FieldNamesMustNotUseHungarianNotation", Target = "bgColor",
Justification = "This method is public and the parameter name cannot be changed")]
public static HelperResult Flash(string path, string width = null, string height = null,
bool play = true, bool loop = true, bool menu = true, string bgColor = null,
string quality = null, string scale = null, string windowMode = null, string baseUrl = null,
string version = null, object options = null, object htmlAttributes = null, string embedName = null)
{
return Flash(HttpContext, _pathUtility, path, width, height, play, loop, menu, bgColor,
quality, scale, windowMode, baseUrl, version, options, htmlAttributes, embedName);
}
// see: http://msdn.microsoft.com/en-us/library/aa392321
#if CODE_COVERAGE
[ExcludeFromCodeCoverage]
#endif
[SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "10#",
Justification = "string parameter passed to media player in object tag")]
public static HelperResult MediaPlayer(string path, string width = null, string height = null,
bool autoStart = true, int playCount = 1, string uiMode = null, bool stretchToFit = false,
bool enableContextMenu = true, bool mute = false, int volume = -1, string baseUrl = null,
object options = null, object htmlAttributes = null, string embedName = null)
{
return MediaPlayer(HttpContext, _pathUtility, path, width, height, autoStart, playCount, uiMode, stretchToFit,
enableContextMenu, mute, volume, baseUrl, options, htmlAttributes, embedName);
}
// should users really use Silverlight.js?
// see: http://msdn.microsoft.com/en-us/library/cc838259(v=VS.95).aspx
#if CODE_COVERAGE
[ExcludeFromCodeCoverage]
#endif
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1305:FieldNamesMustNotUseHungarianNotation", Target = "bgColor",
Justification = "This method is public and the parameter name cannot be changed")]
public static HelperResult Silverlight(string path, string width, string height,
string bgColor = null, string initParameters = null, string minimumVersion = null, bool autoUpgrade = true,
object options = null, object htmlAttributes = null)
{
return Silverlight(HttpContext, _pathUtility, path, width, height, bgColor, initParameters, minimumVersion, autoUpgrade,
options, htmlAttributes);
}
internal static HelperResult Flash(HttpContextBase context, VirtualPathUtilityBase pathUtility, string path,
string width = null, string height = null, bool play = true, bool loop = true, bool menu = true,
string backgroundColor = null, string quality = null, string scale = null, string windowMode = null,
string baseUrl = null, string version = null, object options = null, object htmlAttributes = null, string embedName = null)
{
var parameters = ObjectToDictionary(options, "options", _flashBlacklist);
if (!play)
{
parameters["play"] = false;
}
if (!loop)
{
parameters["loop"] = false;
}
if (!menu)
{
parameters["menu"] = false;
}
if (!String.IsNullOrEmpty(backgroundColor))
{
parameters["bgColor"] = backgroundColor;
}
if (!String.IsNullOrEmpty(quality))
{
parameters["quality"] = quality;
}
if (!String.IsNullOrEmpty(scale))
{
parameters["scale"] = scale;
}
if (!String.IsNullOrEmpty(windowMode))
{
parameters["wmode"] = windowMode;
}
if (!String.IsNullOrEmpty(baseUrl))
{
parameters["base"] = baseUrl;
}
string cab = FlashCab;
if (!String.IsNullOrEmpty(version))
{
cab += "#version=" + version.Replace('.', ',');
}
return GetHtml(context, pathUtility, path, width, height,
OleMimeType, null, FlashClassId, cab, "movie", FlashMimeType, parameters, htmlAttributes, embedName);
}
internal static HelperResult MediaPlayer(HttpContextBase context, VirtualPathUtilityBase pathUtility, string path, string width = null, string height = null,
bool autoStart = true, int playCount = 1, string uiMode = null, bool stretchToFit = false,
bool enableContextMenu = true, bool mute = false, int volume = -1, string baseUrl = null,
object options = null, object htmlAttributes = null, string embedName = null)
{
var parameters = ObjectToDictionary(options, "options", _mediaPlayerBlacklist);
if (!autoStart)
{
parameters["autoStart"] = false;
}
if (playCount != 1)
{
parameters["playCount"] = playCount;
}
if (!String.IsNullOrEmpty(uiMode))
{
parameters["uiMode"] = uiMode;
}
if (stretchToFit)
{
parameters["stretchToFit"] = true;
}
if (!enableContextMenu)
{
parameters["enableContextMenu"] = false;
}
if (mute)
{
parameters["mute"] = true;
}
if (volume >= 0)
{
parameters["volume"] = Math.Min(volume, 100);
}
if (!String.IsNullOrEmpty(baseUrl))
{
parameters["baseURL"] = baseUrl;
}
return GetHtml(context, pathUtility, path, width, height,
null, null, MediaPlayerClassId, null, "URL", MediaPlayerMimeType, parameters, htmlAttributes, embedName);
}
internal static HelperResult Silverlight(HttpContextBase context, VirtualPathUtilityBase pathUtility, string path, string width, string height,
string backgroundColor = null, string initParameters = null, string minimumVersion = null, bool autoUpgrade = true,
object options = null, object htmlAttributes = null)
{
if (String.IsNullOrEmpty(width))
{
throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "width");
}
if (String.IsNullOrEmpty(height))
{
throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "height");
}
var parameters = ObjectToDictionary(options, "options", _silverlightBlacklist);
if (!String.IsNullOrEmpty(backgroundColor))
{
parameters["background"] = backgroundColor;
}
if (!String.IsNullOrEmpty(initParameters))
{
parameters["initparams"] = initParameters;
}
if (!String.IsNullOrEmpty(minimumVersion))
{
parameters["minruntimeversion"] = minimumVersion;
}
if (!autoUpgrade)
{
parameters["autoUpgrade"] = autoUpgrade;
}
return GetHtml(context, pathUtility, path, width, height,
SilverlightMimeType, "data:" + SilverlightMimeType + ",", // ',' required for Opera support
null, null, "source", null, parameters, htmlAttributes, null,
tw =>
{
tw.WriteLine("<a href=\"http://go.microsoft.com/fwlink/?LinkID=149156\" style=\"text-decoration:none\">");
tw.WriteLine("<img src=\"http://go.microsoft.com/fwlink?LinkId=108181\" alt=\"Get Microsoft Silverlight\" style=\"border-style:none\"/>");
tw.WriteLine("</a>");
});
}
private static IDictionary<string, object> ObjectToDictionary(object o, string argName, string[] blackList)
{
var dictionary = new RouteValueDictionary(o);
foreach (var key in dictionary.Keys)
{
if (blackList.Contains(key, StringComparer.OrdinalIgnoreCase))
{
throw new ArgumentException(String.Format(CultureInfo.CurrentUICulture,
HelpersToolkitResources.Video_PropertyCannotBeSet, key), argName);
}
}
return dictionary;
}
private static HelperResult GetHtml(HttpContextBase context, VirtualPathUtilityBase pathUtility,
string path, string width, string height, string objectType, string objectDataType,
string objectClassId, string objectCodeBase, string pathParamName, string embedContentType,
IDictionary<string, object> parameters = null, object htmlAttributes = null, string embedName = null,
Action<TextWriter> plugin = null)
{
path = ValidatePath(context, pathUtility, path);
var objectAttr = ObjectToDictionary(htmlAttributes, "htmlAttributes", _globalBlacklist);
objectAttr["width"] = width;
objectAttr["height"] = height;
objectAttr["type"] = objectType;
objectAttr["data"] = objectDataType;
objectAttr["classid"] = objectClassId;
objectAttr["codebase"] = objectCodeBase;
return new HelperResult(tw =>
{
tw.Write("<object ");
foreach (var a in objectAttr.OrderBy(a => a.Key, StringComparer.OrdinalIgnoreCase))
{
var value = (a.Value == null) ? null : a.Value.ToString();
WriteIfNotNullOrEmpty(tw, a.Key, value);
}
tw.WriteLine(">");
// object parameters
if (!String.IsNullOrEmpty(pathParamName))
{
tw.WriteLine("<param name=\"{0}\" value=\"{1}\" />",
HttpUtility.HtmlAttributeEncode(pathParamName),
HttpUtility.HtmlAttributeEncode(HttpUtility.UrlPathEncode(path)));
}
if (parameters != null)
{
foreach (var p in parameters)
{
tw.WriteLine("<param name=\"{0}\" value=\"{1}\" />",
HttpUtility.HtmlAttributeEncode(p.Key),
HttpUtility.HtmlAttributeEncode(p.Value.ToString()));
}
}
if (!String.IsNullOrEmpty(embedContentType))
{
tw.Write("<embed src=\"{0}\" ", HttpUtility.HtmlAttributeEncode(HttpUtility.UrlPathEncode(path)));
WriteIfNotNullOrEmpty(tw, "width", width);
WriteIfNotNullOrEmpty(tw, "height", height);
WriteIfNotNullOrEmpty(tw, "name", embedName);
WriteIfNotNullOrEmpty(tw, "type", embedContentType);
if (parameters != null)
{
foreach (var p in parameters)
{
tw.Write("{0}=\"{1}\" ", HttpUtility.HtmlEncode(p.Key), HttpUtility.HtmlAttributeEncode(p.Value.ToString()));
}
}
tw.WriteLine("/>");
}
if (plugin != null)
{
plugin(tw);
}
tw.WriteLine("</object>");
});
}
private static string ValidatePath(HttpContextBase context, VirtualPathUtilityBase pathUtility, string path)
{
if (String.IsNullOrEmpty(path))
{
throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "path");
}
string originalPath = path;
if (!path.StartsWith("http", StringComparison.OrdinalIgnoreCase))
{
// resolve relative paths
path = pathUtility.Combine(context.Request.AppRelativeCurrentExecutionFilePath, path);
// resolve to app absolute - SL doesn't support app relative
path = pathUtility.ToAbsolute(path);
if (!File.Exists(context.Server.MapPath(path)))
{
throw new InvalidOperationException(String.Format(CultureInfo.CurrentUICulture,
HelpersToolkitResources.Video_FileDoesNotExist, originalPath));
}
}
return path;
}
private static void WriteIfNotNullOrEmpty(TextWriter tw, string key, string value)
{
Debug.Assert(!String.IsNullOrEmpty(key));
if (!String.IsNullOrEmpty(value))
{
tw.Write("{0}=\"{1}\" ", HttpUtility.HtmlEncode(key), HttpUtility.HtmlAttributeEncode(value));
}
}
}
}