ImageResizer  3.4.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events
Public Member Functions | Static Public Member Functions | Properties | List of all members
ImageResizer.Util.PathUtils Class Reference

A set of utility methods for manipulating virtual paths More...

Public Member Functions

delegate string VariableResolverCallback (string variableName)
 A method that resolves variable names to values for the ResolveVariablesInPath method More...
 

Static Public Member Functions

static string SetExtension (string path, string newExtension)
 Should be called SetFullExtension. Sets the file extension of the specified path to the specified value, returning the result. If an extension has multiple parts, it will replace all of them. Leading dots will be stripped from 'newExtension' and re-addd as required. The querystring and fragment is maintained as-is. Semicolon syntax not supported. More...
 
static string RemoveFullExtension (string path)
 Removes all extension segments from the filename or URL, leaving the querystring intact. I.e, image.jpg.bmp.tiff?hi would be image?hi The querystring and fragment is maintained as-is. Semicolon syntax not supported. More...
 
static string RemoveExtension (string path)
 Removes the extension from the filename or URL, leaving the querystring intact, where the extension is only the last extension segment. I.e, image.jpg.bmp.tiff?hi would be image.jpg.bmp?hi after this call. The querystring and fragment is maintained as-is. Semicolon syntax not supported. More...
 
static string AddExtension (string path, string newExtension)
 Adds the specified extension to path, returning the result. Multiple calls will result in "path.ext.ext.ext.ext". The querystring and fragment is maintained as-is. Semicolon syntax not supported. More...
 
static string GetFullExtension (string path)
 Will return the full extension, like ".jpg.ashx", not just the last bit. Excludes extensions containing spaces or slashes. The querystring and fragment is ignored. Semicolon syntax not supported. More...
 
static string GetExtension (string path)
 Grabs the last segment of the filename extension. Returns an empty string if there is no extension, or if the extension contains a space. Includes the leading '.' The querystring and fragment is ignored. Semicolon syntax not supported. More...
 
static string ResolveAppRelative (string virtualPath)
 Resolves app-relative paths to virtual paths. Does nothing with virtual or relative paths. More...
 
static string ResolveAppRelativeAssumeAppRelative (string virtualPath)
 Turns relative paths into domain-relative paths. Turns app-relative paths into domain relative paths. More...
 
static string AddQueryString (string virtualPath, string querystring)
 Joins the path and querystring. If the path already contains a querystring, they are 'append joined' with the correct character. Fragment is maintained as-is. Does not support the semicolon syntax. More...
 
static string RemoveQueryString (string path)
 Removes the query string from the specifed path. If the path is only a querystring, an empty string is returned. Does not support the semicolon syntax. Fragment is maintained as-is. More...
 
static string MergeOverwriteQueryString (string path, NameValueCollection newQuerystring)
 Overwrites exisisting querystring values in 'path' with the values in 'newQuerystring'. Does not support the semicolon syntax. More...
 
static string MergeQueryString (string path, NameValueCollection newQuerystring)
 Adds the querystring values in 'newQuerystring' to the querystring in Path, but does not overwrite values. Does not support the semicolon syntax. More...
 
static string BuildQueryString (NameValueCollection QueryString)
 Returns a string querystring in the form "?key=value&key=value". Keys and values are UrlEncoded as they should be. More...
 
static string BuildQueryString (NameValueCollection QueryString, bool urlEncode)
 Returns a string querystring in the form "?key=value&key=value". Keys and values are UrlEncoded if urlEncode=true. More...
 
static string BuildSemicolonQueryString (NameValueCollection QueryString, bool urlEncode)
 Returns a string querystring in the form ";key=value;key=value". Keys and values are UrlEncoded if urlEncode=true. More...
 
static string BuildQueryString (NameValueCollection QueryString, bool urlEncode, bool skipNullValues=true, char firstSeparator= '?', char laterSeparators= '&', char equals= '=')
 Build a customized querystring from a NameValueCollection More...
 
static NameValueCollection ParseQueryStringFriendly (string path)
 Like ParseQueryString, but permits the leading '?' to be omitted. Does not support the semicolon syntax. More...
 
static NameValueCollection ParseQueryStringFriendlyAllowSemicolons (string path)
 Like ParseQueryString, but permits the leading '?' to be omitted, and semicolons can be substituted for '&' More...
 
static NameValueCollection ParseQueryString (string path)
 Parses the querystring from the given path into a NameValueCollection. accepts "file?key=value" and "?key=value&key2=value2" formats. (no path is required) UrlDecodes keys and values. More...
 
static NameValueCollection ParseQueryString (string path, bool allowSemicolons)
 Parses the querystring from the given path into a NameValueCollection. accepts "file?key=value" and "?key=value&key2=value2" formats. (no path is required) UrlDecodes keys and values. Does not enforce correct syntax, I.E. '?key=value?key2=value2' is allowed. However, '&key=value?key2=value' will only get key2 parsed. When allowSemicolons is true, semicolon paths like ';key=value;key2=value2' are allowed, as are hybrid paths: ';key=value?key2=value2&key3=value3'. More...
 
static NameValueCollection ParseQueryString (string path, bool allowSemicolons, out string beforeQuery, out string fragment)
 Parses the querystring from the given path into a NameValueCollection. accepts "file?key=value" and "?key=value&key2=value2" formats. (no path is required) UrlDecodes keys and values. Does not enforce correct syntax, I.E. '?key=value?key2=value2' is allowed. However, '&key=value?key2=value' will only get key2 parsed. When allowSemicolons is true, semicolon paths like ';key=value;key2=value2' are allowed, as are hybrid paths: ';key=value?key2=value2&key3=value3'. More...
 
static NameValueCollection ParseQueryOnly (string query, bool allowSemicolons=true, bool urlDecode=true)
 Parses a querystring into a name/value collection. The given string cannot include path or fragment information - it must be just the querystring. More...
 
static string ToBase64U (byte[] data)
 Converts aribtrary bytes to a URL-safe version of base64 (no = padding, with - instead of + and _ instead of /) More...
 
static byte[] FromBase64UToBytes (string data)
 Converts a URL-safe version of base64 to a byte array. (no = padding, with - instead of + and _ instead of /) More...
 
static string ToBase64U (string data)
 Converts aribtrary strings to a URL-safe version of base64 (no = padding, with - instead of + and _ instead of /) More...
 
static string FromBase64UToString (string data)
 Converts a URL-safe version of base64 to a string. 64U is (no = padding, with - instead of + and _ instead of /) More...
 
static string MapPathIfAppRelative (string path)
 Returns the physcial mapped path for the specified virtual path if it starts with ~, otherwise retuns the original path. More...
 
static string GenerateImageUrl (string path, NameValueCollection query)
 Generates a resized image url for the specified physical or virtual path. Not CloudFront compatible. Does not support remote URLs, use RemoteReader for that. If you're running in IIS classic, add ".ashx" to the 'path' parameter. More...
 
static string GuessVirtualPath (string path)
 Attempts to guess the virtual path from physical path. Will be thrwarted if the path is mapped as a virtual folder inside the application. If the path is a non-physical path, it will be returned as is. Returns null if the physical path isn't a subfolder of the application's physical path. More...
 
static string ResolveVariablesInPath (string pathWithVars, VariableResolverCallback resolver)
 Replaces variables in paths with their values. Ex. ~/uploads/thumbs/<guid>.<ext>. Standard variables are <ext> (the default extension for the final file type), <guid>, a randomly generated GUID, <filename>, the original filename without it's extension, <path>, the original path and filename without extension, <settings.width>, (any specified settings value except preset), <width> (final width), and <height> (final height). More...
 
static string RemoveNonMatchingChars (string text, string chars)
 Allows a string to be filtered using the specified whitelisting expression (regex style). More...
 
static NameValueCollection FilterQueryString (ResizeSettings query, params string[] keepKeys)
 Returns a new collection containing only the specified keys from the old one More...
 

Properties

static string AppVirtualPath [get]
 Returns HostingEnvironment.ApplicationVirtualPath or "/", if asp.net is not running More...
 
static string AppPhysicalPath [get]
 If not running in ASP.NET, returns the folder containing the DLL. More...
 

Detailed Description

A set of utility methods for manipulating virtual paths

Definition at line 17 of file PathUtils.cs.

Member Function Documentation

static string ImageResizer.Util.PathUtils.AddExtension ( string  path,
string  newExtension 
)
inlinestatic

Adds the specified extension to path, returning the result. Multiple calls will result in "path.ext.ext.ext.ext". The querystring and fragment is maintained as-is. Semicolon syntax not supported.

Parameters
path
newExtension
Returns

Definition at line 104 of file PathUtils.cs.

104  {
105  int query = path.IndexOfAny(QueryOrFragment);
106  if (query < 0) query = path.Length;
107  return path.Substring(0, query) + "." + newExtension.TrimStart('.') + path.Substring(query);
108  }
static string ImageResizer.Util.PathUtils.AddQueryString ( string  virtualPath,
string  querystring 
)
inlinestatic

Joins the path and querystring. If the path already contains a querystring, they are 'append joined' with the correct character. Fragment is maintained as-is. Does not support the semicolon syntax.

Parameters
virtualPath
querystring
Returns

Definition at line 178 of file PathUtils.cs.

178  {
179  //Separate the fragment if it's present, and restore it later
180  int fragment = virtualPath.IndexOf('#');
181  string suffix = "";
182  if (fragment < 0) fragment = virtualPath.Length;
183  else {
184  suffix = virtualPath.Substring(fragment);
185  virtualPath = virtualPath.Substring(0, fragment);
186  }
187 
188  if (virtualPath.IndexOf('?') > -1) virtualPath = virtualPath.TrimEnd('&') + '&';
189  else virtualPath += '?';
190 
191  return virtualPath + querystring.TrimStart('&', '?') + suffix;
192  }
static string ImageResizer.Util.PathUtils.BuildQueryString ( NameValueCollection  QueryString)
inlinestatic

Returns a string querystring in the form "?key=value&amp;key=value". Keys and values are UrlEncoded as they should be.

Parameters
QueryString
Returns

Definition at line 249 of file PathUtils.cs.

249  {
250  return BuildQueryString(QueryString, true);
251  }
static string BuildQueryString(NameValueCollection QueryString)
Returns a string querystring in the form &quot;?key=value&amp;amp;key=value&quot;. Keys and values are UrlEncoded a...
Definition: PathUtils.cs:249
static string ImageResizer.Util.PathUtils.BuildQueryString ( NameValueCollection  QueryString,
bool  urlEncode 
)
inlinestatic

Returns a string querystring in the form "?key=value&amp;key=value". Keys and values are UrlEncoded if urlEncode=true.

Parameters
QueryString
urlEncode
Returns

Definition at line 259 of file PathUtils.cs.

259  {
260  return BuildQueryString(QueryString, urlEncode, true, '?', '&', '=');
261  }
static string BuildQueryString(NameValueCollection QueryString)
Returns a string querystring in the form &quot;?key=value&amp;amp;key=value&quot;. Keys and values are UrlEncoded a...
Definition: PathUtils.cs:249
static string ImageResizer.Util.PathUtils.BuildQueryString ( NameValueCollection  QueryString,
bool  urlEncode,
bool  skipNullValues = true,
char  firstSeparator = '?',
char  laterSeparators = '&',
char  equals = '=' 
)
inlinestatic

Build a customized querystring from a NameValueCollection

Parameters
QueryString
urlEncodeTrue to URL encode all values
skipNullValuesIf true, null-valued keys will be skipped
firstSeparatorUsually ? or ;
laterSeparatorsUsually & or ;
equalsAlways =
Returns

Definition at line 284 of file PathUtils.cs.

284  {
285  if (QueryString.Count < 1) return ""; //No keys? return empty string.
286 
287  StringBuilder path = new StringBuilder();
288  path.Append(firstSeparator);
289  foreach (string key in QueryString.Keys) {
290  if (key == null) continue; //Skip null keys
291  string value = QueryString[key];
292  if (skipNullValues && value == null) continue; //Optionally skip null values
293  path.Append(urlEncode ? HttpUtility.UrlEncode(key) : key);
294  path.Append(equals);
295  path.Append(urlEncode ? HttpUtility.UrlEncode(value) : value);
296  path.Append(laterSeparators);
297  }
298  if (path[path.Length - 1] == laterSeparators) path.Remove(path.Length - 1, 1);
299 
300  return path.ToString();
301  }
static string ImageResizer.Util.PathUtils.BuildSemicolonQueryString ( NameValueCollection  QueryString,
bool  urlEncode 
)
inlinestatic

Returns a string querystring in the form ";key=value;key=value". Keys and values are UrlEncoded if urlEncode=true.

Parameters
QueryString
urlEncode
Returns

Definition at line 271 of file PathUtils.cs.

271  {
272  return BuildQueryString(QueryString, urlEncode, true, ';', ';', '=');
273  }
static string BuildQueryString(NameValueCollection QueryString)
Returns a string querystring in the form &quot;?key=value&amp;amp;key=value&quot;. Keys and values are UrlEncoded a...
Definition: PathUtils.cs:249
static NameValueCollection ImageResizer.Util.PathUtils.FilterQueryString ( ResizeSettings  query,
params string[]  keepKeys 
)
inlinestatic

Returns a new collection containing only the specified keys from the old one

Parameters
query
keepKeys
Returns

Definition at line 583 of file PathUtils.cs.

583  {
584  return NameValueCollectionExtensions.Keep(query, keepKeys);
585  }
static byte [] ImageResizer.Util.PathUtils.FromBase64UToBytes ( string  data)
inlinestatic

Converts a URL-safe version of base64 to a byte array. (no = padding, with - instead of + and _ instead of /)

Parameters
data
Returns

Definition at line 445 of file PathUtils.cs.

445  {
446  data = data.PadRight(data.Length + ((4 - data.Length % 4) % 4), '='); //if there is 1 leftover octet, add ==, if 2, add =. 3 octects = 4 chars.
447  return Convert.FromBase64String(data.Replace('-', '+').Replace('_', '/'));
448  }
static string ImageResizer.Util.PathUtils.FromBase64UToString ( string  data)
inlinestatic

Converts a URL-safe version of base64 to a string. 64U is (no = padding, with - instead of + and _ instead of /)

Parameters
data
Returns

Definition at line 462 of file PathUtils.cs.

462  {
463  return UTF8Encoding.UTF8.GetString(FromBase64UToBytes(data));
464  }
static byte[] FromBase64UToBytes(string data)
Converts a URL-safe version of base64 to a byte array. (no = padding, with - instead of + and _ inste...
Definition: PathUtils.cs:445
static string ImageResizer.Util.PathUtils.GenerateImageUrl ( string  path,
NameValueCollection  query 
)
inlinestatic

Generates a resized image url for the specified physical or virtual path. Not CloudFront compatible. Does not support remote URLs, use RemoteReader for that. If you're running in IIS classic, add ".ashx" to the 'path' parameter.

Parameters
path
query
Returns

Definition at line 483 of file PathUtils.cs.

483  {
484  return ResolveAppRelative(GuessVirtualPath(path)) + BuildQueryString(query);
485  }
static string GuessVirtualPath(string path)
Attempts to guess the virtual path from physical path. Will be thrwarted if the path is mapped as a v...
Definition: PathUtils.cs:494
static string BuildQueryString(NameValueCollection QueryString)
Returns a string querystring in the form &quot;?key=value&amp;amp;key=value&quot;. Keys and values are UrlEncoded a...
Definition: PathUtils.cs:249
static string ResolveAppRelative(string virtualPath)
Resolves app-relative paths to virtual paths. Does nothing with virtual or relative paths...
Definition: PathUtils.cs:150
static string ImageResizer.Util.PathUtils.GetExtension ( string  path)
inlinestatic

Grabs the last segment of the filename extension. Returns an empty string if there is no extension, or if the extension contains a space. Includes the leading '.' The querystring and fragment is ignored. Semicolon syntax not supported.

Parameters
path
Returns

Definition at line 134 of file PathUtils.cs.

134  {
135  int query = path.IndexOfAny(QueryOrFragment);
136  if (query < 0) query = path.Length;
137  //Finds the first character that could possibly be part of the extension (before the query)
138  int firstPossibleExtensionChar = path.LastIndexOfAny(SpaceOrSlash, query - 1) + 1;
139  int extensionStarts = path.LastIndexOf('.', query -1, query - firstPossibleExtensionChar );
140  if (extensionStarts < 0) extensionStarts = query;
141 
142  return path.Substring(extensionStarts, query - extensionStarts);
143 
144  }
static string ImageResizer.Util.PathUtils.GetFullExtension ( string  path)
inlinestatic

Will return the full extension, like ".jpg.ashx", not just the last bit. Excludes extensions containing spaces or slashes. The querystring and fragment is ignored. Semicolon syntax not supported.

Parameters
path
Returns

Definition at line 116 of file PathUtils.cs.

116  {
117  int query = path.IndexOfAny(QueryOrFragment);
118  if (query < 0) query = path.Length;
119  //Finds the first character that could possibly be part of the extension (before the query)
120  int firstPossibleExtensionChar = path.LastIndexOfAny(SpaceOrSlash, query - 1) + 1;
121  int extensionStarts = path.IndexOf('.', firstPossibleExtensionChar, query - firstPossibleExtensionChar);
122  if (extensionStarts < 0) extensionStarts = query;
123 
124  return path.Substring(extensionStarts, query - extensionStarts);
125 
126  }
static string ImageResizer.Util.PathUtils.GuessVirtualPath ( string  path)
inlinestatic

Attempts to guess the virtual path from physical path. Will be thrwarted if the path is mapped as a virtual folder inside the application. If the path is a non-physical path, it will be returned as is. Returns null if the physical path isn't a subfolder of the application's physical path.

Parameters
path
Returns

Definition at line 494 of file PathUtils.cs.

494  {
495  //If there's no windows slashes, it's probably virtual.
496  if (path.IndexOf('\\') < 0)
497  return path;
498 
499  string appPath = HostingEnvironment.ApplicationPhysicalPath;
500  //Otherwise, try to get the full path so we can compare it.
501  if (!Path.IsPathRooted(path)) path = Path.Combine(appPath, path);
502  path = Path.GetFullPath(path);
503  //If it's a match, strip it and convert it to a virtual path.
504  if (path.StartsWith(appPath, StringComparison.OrdinalIgnoreCase)) {
505  //Convert to app-relative path missing the ~/
506  path = path.Remove(0, appPath.Length).TrimStart(new char[] { '/', '\\', '~' }).Replace('\\', '/');
507  return PathUtils.ResolveAppRelativeAssumeAppRelative(path);
508 
509  }
510  return null;
511  }
static string ImageResizer.Util.PathUtils.MapPathIfAppRelative ( string  path)
inlinestatic

Returns the physcial mapped path for the specified virtual path if it starts with ~, otherwise retuns the original path.

Parameters
path
Returns

Definition at line 470 of file PathUtils.cs.

470  {
471  return (path.StartsWith("~", StringComparison.OrdinalIgnoreCase)) ? HostingEnvironment.MapPath(path) : path;
472  }
static string ImageResizer.Util.PathUtils.MergeOverwriteQueryString ( string  path,
NameValueCollection  newQuerystring 
)
inlinestatic

Overwrites exisisting querystring values in 'path' with the values in 'newQuerystring'. Does not support the semicolon syntax.

Parameters
path
newQuerystring
Returns

Definition at line 220 of file PathUtils.cs.

220  {
221  NameValueCollection oldQuery = ParseQueryString(path);
222  //Overwrite old with new
223  foreach (string key in newQuerystring.Keys)
224  if (key != null) oldQuery[key] = newQuerystring[key];
225 
226  return AddQueryString(RemoveQueryString(path), BuildQueryString(oldQuery));
227  }
static string RemoveQueryString(string path)
Removes the query string from the specifed path. If the path is only a querystring, an empty string is returned. Does not support the semicolon syntax. Fragment is maintained as-is.
Definition: PathUtils.cs:199
static string AddQueryString(string virtualPath, string querystring)
Joins the path and querystring. If the path already contains a querystring, they are &#39;append joined&#39; ...
Definition: PathUtils.cs:178
static NameValueCollection ParseQueryString(string path)
Parses the querystring from the given path into a NameValueCollection. accepts &quot;file?key=value&quot; and &quot;?key=value&amp;amp;key2=value2&quot; formats. (no path is required) UrlDecodes keys and values.
Definition: PathUtils.cs:344
static string BuildQueryString(NameValueCollection QueryString)
Returns a string querystring in the form &quot;?key=value&amp;amp;key=value&quot;. Keys and values are UrlEncoded a...
Definition: PathUtils.cs:249
static string ImageResizer.Util.PathUtils.MergeQueryString ( string  path,
NameValueCollection  newQuerystring 
)
inlinestatic

Adds the querystring values in 'newQuerystring' to the querystring in Path, but does not overwrite values. Does not support the semicolon syntax.

Parameters
path
newQuerystring
Returns

Definition at line 234 of file PathUtils.cs.

234  {
235  NameValueCollection oldQuery = ParseQueryString(path);
236  //Overwrite new with old
237  foreach (string key in oldQuery.Keys)
238  if (key != null) newQuerystring[key] = oldQuery[key];
239 
240  return AddQueryString(RemoveQueryString(path), BuildQueryString(newQuerystring));
241  }
static string RemoveQueryString(string path)
Removes the query string from the specifed path. If the path is only a querystring, an empty string is returned. Does not support the semicolon syntax. Fragment is maintained as-is.
Definition: PathUtils.cs:199
static string AddQueryString(string virtualPath, string querystring)
Joins the path and querystring. If the path already contains a querystring, they are &#39;append joined&#39; ...
Definition: PathUtils.cs:178
static NameValueCollection ParseQueryString(string path)
Parses the querystring from the given path into a NameValueCollection. accepts &quot;file?key=value&quot; and &quot;?key=value&amp;amp;key2=value2&quot; formats. (no path is required) UrlDecodes keys and values.
Definition: PathUtils.cs:344
static string BuildQueryString(NameValueCollection QueryString)
Returns a string querystring in the form &quot;?key=value&amp;amp;key=value&quot;. Keys and values are UrlEncoded a...
Definition: PathUtils.cs:249
static NameValueCollection ImageResizer.Util.PathUtils.ParseQueryOnly ( string  query,
bool  allowSemicolons = true,
bool  urlDecode = true 
)
inlinestatic

Parses a querystring into a name/value collection. The given string cannot include path or fragment information - it must be just the querystring.

Parameters
query
allowSemicolons
urlDecode
Returns

Definition at line 408 of file PathUtils.cs.

408  {
409  string[] pairs = query.Split(allowSemicolons ? new char[] { '?', '&', ';' } : new char[] { '?', '&' }, StringSplitOptions.RemoveEmptyEntries);
410  NameValueCollection c = new NameValueCollection();
411  foreach (string s in pairs) {
412  string[] namevalue = s.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
413  if (namevalue.Length > 2) {
414  //Handle &key=value=value and &key===value=value -> key : "value=value"
415  string value = s.Substring(s.IndexOf('=')).TrimStart('=');
416  c[urlDecode ? HttpUtility.UrlDecode(namevalue[0]) : namevalue[0]] =
417  urlDecode ? HttpUtility.UrlDecode(value) : value;
418  } else if (namevalue.Length == 2) {
419  //Handle key=value (normal)
420  c[urlDecode ? HttpUtility.UrlDecode(namevalue[0]) : namevalue[0]] =
421  urlDecode ? HttpUtility.UrlDecode(namevalue[1]) : namevalue[1];
422  } else if (namevalue.Length == 1){
423  //Hanlde &key=&key2= or &key&key2 -> key: "", key2: ""
424  //Setting a null value would be confusing, as that is how we determine
425  //whether a certain paramater exists
426  c[urlDecode ? HttpUtility.UrlDecode(namevalue[0]) : namevalue[0]] = "";
427  }
428  }
429  return c;
430  }
static NameValueCollection ImageResizer.Util.PathUtils.ParseQueryString ( string  path)
inlinestatic

Parses the querystring from the given path into a NameValueCollection. accepts "file?key=value" and "?key=value&amp;key2=value2" formats. (no path is required) UrlDecodes keys and values.

Parameters
path
Returns

Definition at line 344 of file PathUtils.cs.

344  {
345  return ParseQueryString(path, false);
346  }
static NameValueCollection ParseQueryString(string path)
Parses the querystring from the given path into a NameValueCollection. accepts &quot;file?key=value&quot; and &quot;?key=value&amp;amp;key2=value2&quot; formats. (no path is required) UrlDecodes keys and values.
Definition: PathUtils.cs:344
static NameValueCollection ImageResizer.Util.PathUtils.ParseQueryString ( string  path,
bool  allowSemicolons 
)
inlinestatic

Parses the querystring from the given path into a NameValueCollection. accepts "file?key=value" and "?key=value&amp;key2=value2" formats. (no path is required) UrlDecodes keys and values. Does not enforce correct syntax, I.E. '?key=value?key2=value2' is allowed. However, '&key=value?key2=value' will only get key2 parsed. When allowSemicolons is true, semicolon paths like ';key=value;key2=value2' are allowed, as are hybrid paths: ';key=value?key2=value2&key3=value3'.

Parameters
path
allowSemicolons
Returns

Definition at line 357 of file PathUtils.cs.

357  {
358  string s, f;
359  return ParseQueryString(path, allowSemicolons, out s,out f);
360  }
static NameValueCollection ParseQueryString(string path)
Parses the querystring from the given path into a NameValueCollection. accepts &quot;file?key=value&quot; and &quot;?key=value&amp;amp;key2=value2&quot; formats. (no path is required) UrlDecodes keys and values.
Definition: PathUtils.cs:344
static NameValueCollection ImageResizer.Util.PathUtils.ParseQueryString ( string  path,
bool  allowSemicolons,
out string  beforeQuery,
out string  fragment 
)
inlinestatic

Parses the querystring from the given path into a NameValueCollection. accepts "file?key=value" and "?key=value&amp;key2=value2" formats. (no path is required) UrlDecodes keys and values. Does not enforce correct syntax, I.E. '?key=value?key2=value2' is allowed. However, '&key=value?key2=value' will only get key2 parsed. When allowSemicolons is true, semicolon paths like ';key=value;key2=value2' are allowed, as are hybrid paths: ';key=value?key2=value2&key3=value3'.

Does NOT parse fragments correctly.

Parameters
path
allowSemicolons
beforeQueryReturns the portion of the 'path' before the querystring. May include the scheme, server, port, path and path info, depending upon what 'path' contained.
Returns

Definition at line 374 of file PathUtils.cs.

374  {
375  //Separate the fragment if it's present, and restore it later
376  int frag = path.IndexOf('#');
377  if (frag < 0) fragment = "";
378  else {
379  fragment = path.Substring(frag);
380  path = path.Substring(0, frag);
381  }
382 
383 
384  int firstdelimiter = path.IndexOf('?');
385  if (allowSemicolons) {
386  //Use the index if whichever is first, and preset
387  int firstsemicolon = path.IndexOf(';');
388  if (firstdelimiter < 0 || (firstsemicolon >= 0 && firstsemicolon < firstdelimiter)) firstdelimiter = firstsemicolon;
389  }
390  if (firstdelimiter < 0 || firstdelimiter >= path.Length) {
391  //No query string detected
392  beforeQuery = path;
393  return new NameValueCollection();
394  } else {
395  beforeQuery = path.Substring(0, firstdelimiter);
396  }
397 
398  return ParseQueryOnly(path.Substring(firstdelimiter, path.Length - firstdelimiter),allowSemicolons);
399  }
static NameValueCollection ParseQueryOnly(string query, bool allowSemicolons=true, bool urlDecode=true)
Parses a querystring into a name/value collection. The given string cannot include path or fragment i...
Definition: PathUtils.cs:408
static NameValueCollection ImageResizer.Util.PathUtils.ParseQueryStringFriendly ( string  path)
inlinestatic

Like ParseQueryString, but permits the leading '?' to be omitted. Does not support the semicolon syntax.

Parameters
path
Returns

Definition at line 312 of file PathUtils.cs.

312  {
313  if (path.IndexOf('?') < 0 && path.IndexOf('=') >= 0) path = '?' + path;
314  return ParseQueryString(path,false);
315  }
static NameValueCollection ParseQueryString(string path)
Parses the querystring from the given path into a NameValueCollection. accepts &quot;file?key=value&quot; and &quot;?key=value&amp;amp;key2=value2&quot; formats. (no path is required) UrlDecodes keys and values.
Definition: PathUtils.cs:344
static NameValueCollection ImageResizer.Util.PathUtils.ParseQueryStringFriendlyAllowSemicolons ( string  path)
inlinestatic

Like ParseQueryString, but permits the leading '?' to be omitted, and semicolons can be substituted for '&'

Parameters
path
Returns

Definition at line 322 of file PathUtils.cs.

322  {
323  //Delete the fragment before we start
324  int fragment = path.IndexOf('#');
325  if (fragment > -1) path = path.Substring(0, fragment);
326 
327  int eq = path.IndexOf('=');
328  int quest = path.IndexOf('?');
329  int semi = path.IndexOf(';');
330  if (eq >= 0 && //We have equals, which indicates a query
331  quest < 0 && //But no question mark!
332  (eq < semi || semi < 0)) //An no semicolon, or else a semicolon *after* the first equals.
333  path = '?' + path;
334  return ParseQueryString(path,true);
335  }
static NameValueCollection ParseQueryString(string path)
Parses the querystring from the given path into a NameValueCollection. accepts &quot;file?key=value&quot; and &quot;?key=value&amp;amp;key2=value2&quot; formats. (no path is required) UrlDecodes keys and values.
Definition: PathUtils.cs:344
static string ImageResizer.Util.PathUtils.RemoveExtension ( string  path)
inlinestatic

Removes the extension from the filename or URL, leaving the querystring intact, where the extension is only the last extension segment. I.e, image.jpg.bmp.tiff?hi would be image.jpg.bmp?hi after this call. The querystring and fragment is maintained as-is. Semicolon syntax not supported.

Parameters
path
Returns

Definition at line 85 of file PathUtils.cs.

85  {
86  int query = path.IndexOfAny(QueryOrFragment);
87  if (query < 0) query = path.Length;
88  //Finds the first character that could possibly be part of the extension (before the query)
89  int firstPossibleExtensionChar = path.LastIndexOfAny(SpaceOrSlash, query - 1) + 1;
90  int extensionStarts = path.LastIndexOf('.', query - 1, query - firstPossibleExtensionChar);
91  if (extensionStarts < 0) extensionStarts = query;
92 
93  return path.Substring(0, extensionStarts) + path.Substring(query);
94 
95  }
static string ImageResizer.Util.PathUtils.RemoveFullExtension ( string  path)
inlinestatic

Removes all extension segments from the filename or URL, leaving the querystring intact. I.e, image.jpg.bmp.tiff?hi would be image?hi The querystring and fragment is maintained as-is. Semicolon syntax not supported.

Parameters
path
Returns

Definition at line 66 of file PathUtils.cs.

66  {
67  int query = path.IndexOfAny(QueryOrFragment);
68  if (query < 0) query = path.Length;
69  //Finds the first character that could possibly be part of the extension (before the query)
70  int firstPossibleExtensionChar = path.LastIndexOfAny(SpaceOrSlash, query - 1) + 1;
71  int extensionStarts = path.IndexOf('.', firstPossibleExtensionChar, query - firstPossibleExtensionChar);
72  if (extensionStarts < 0) extensionStarts = query;
73 
74  return path.Substring(0, extensionStarts) + path.Substring(query);
75 
76  }
static string ImageResizer.Util.PathUtils.RemoveNonMatchingChars ( string  text,
string  chars 
)
inlinestatic

Allows a string to be filtered using the specified whitelisting expression (regex style).

I.e, "hi YOU 3", "a-z3" will produce "hi3".

Parameters
text
chars
Returns

Definition at line 560 of file PathUtils.cs.

560  {
561  StringBuilder sb = new StringBuilder();
562  Regex r = new Regex("[" + Regex.Escape(chars) + "]", RegexOptions.Compiled);
563  for (int i = 0; i < text.Length; i++) {
564  string chr = text.Substring(i,1);
565  if (r.IsMatch(chr)) sb.Append(chr);
566  }
567  return sb.ToString();
568  }
static string ImageResizer.Util.PathUtils.RemoveQueryString ( string  path)
inlinestatic

Removes the query string from the specifed path. If the path is only a querystring, an empty string is returned. Does not support the semicolon syntax. Fragment is maintained as-is.

Parameters
path
Returns

Definition at line 199 of file PathUtils.cs.

199  {
200  //Separate the fragment if it's present, and restore it later
201  int fragment = path.IndexOf('#');
202  string suffix = "";
203  if (fragment < 0) fragment = path.Length;
204  else {
205  suffix = path.Substring(fragment);
206  path = path.Substring(0, fragment);
207  }
208 
209  int delimiter = path.IndexOf('?');
210  return delimiter > -1 ? path.Substring(0, delimiter) + suffix : path;
211  }
static string ImageResizer.Util.PathUtils.ResolveAppRelative ( string  virtualPath)
inlinestatic

Resolves app-relative paths to virtual paths. Does nothing with virtual or relative paths.

Parameters
virtualPath
Returns

Definition at line 150 of file PathUtils.cs.

Referenced by ImageResizer.ImageBuilder.GetStreamFromSource().

150  {
151  //resolve tilde
152  if (virtualPath.StartsWith("~", StringComparison.OrdinalIgnoreCase)) return HostingEnvironment.ApplicationVirtualPath.TrimEnd('/') + '/' + virtualPath.TrimStart('~', '/');
153  return virtualPath;
154  }
static string ImageResizer.Util.PathUtils.ResolveAppRelativeAssumeAppRelative ( string  virtualPath)
inlinestatic

Turns relative paths into domain-relative paths. Turns app-relative paths into domain relative paths.

Parameters
virtualPath
Returns

Definition at line 162 of file PathUtils.cs.

162  {
163 
164  if (virtualPath.StartsWith("~")) return HostingEnvironment.ApplicationVirtualPath.TrimEnd('/') + "/" + virtualPath.TrimStart('~', '/');
165  if (!virtualPath.StartsWith("/")) return HostingEnvironment.ApplicationVirtualPath.TrimEnd('/') + "/" + virtualPath;
166  return virtualPath;
167  }
static string ImageResizer.Util.PathUtils.ResolveVariablesInPath ( string  pathWithVars,
VariableResolverCallback  resolver 
)
inlinestatic

Replaces variables in paths with their values. Ex. ~/uploads/thumbs/<guid>.<ext>. Standard variables are <ext> (the default extension for the final file type), <guid>, a randomly generated GUID, <filename>, the original filename without it's extension, <path>, the original path and filename without extension, <settings.width>, (any specified settings value except preset), <width> (final width), and <height> (final height).

Parameters
pathWithVars
resolver
Returns

Definition at line 524 of file PathUtils.cs.

524  {
525  string p = pathWithVars;
526  while (p.IndexOf('<') > -1) {
527  int start = p.IndexOf('<');
528  int stop = p.IndexOf('>',start);
529  int bugcheck = p.IndexOf('<',start + 1);
530  if (stop < 0 || (bugcheck > -1 && bugcheck < stop) || stop == start + 1)
531  throw new ImageProcessingException("Destination paths can only contain < and > in matched pairs to designate variables. Path \"" + pathWithVars + "\" has invalid syntax");
532  string varName = p.Substring(start + 1,stop - start -1);
533  string filter = null;
534  //Split off the filter if present.
535  int filterIx = varName.IndexOf(':');
536  if (filterIx > 0) {
537  filter = varName.Substring(filterIx + 1);
538  varName = varName.Substring(0, filterIx);
539  }
540  varName = varName.ToLowerInvariant();
541  string result = resolver(varName);
542  if (!string.IsNullOrEmpty(filter)) {
543  result = RemoveNonMatchingChars(result, filter);
544  }
545  if (result == null)
546  throw new ImageProcessingException("Invalid variable name \"" + varName + "\" in templated path \"" + pathWithVars + "\". The variable name may be mispelled, or the variable may not be available with the pipeline you are using.");
547  p = p.Substring(0,start) + result + p.Substring(stop + 1);
548  }
549  if (p.IndexOf('>') > -1) throw new ImageProcessingException("Orphaned '>' in template path \"" + pathWithVars + "\".");
550  return p;
551  }
static string RemoveNonMatchingChars(string text, string chars)
Allows a string to be filtered using the specified whitelisting expression (regex style)...
Definition: PathUtils.cs:560
static string ImageResizer.Util.PathUtils.SetExtension ( string  path,
string  newExtension 
)
inlinestatic

Should be called SetFullExtension. Sets the file extension of the specified path to the specified value, returning the result. If an extension has multiple parts, it will replace all of them. Leading dots will be stripped from 'newExtension' and re-addd as required. The querystring and fragment is maintained as-is. Semicolon syntax not supported.

Parameters
path
newExtension
Returns

Definition at line 48 of file PathUtils.cs.

48  {
49  int query = path.IndexOfAny(QueryOrFragment);
50  if (query < 0) query = path.Length;
51  //Finds the first character that could possibly be part of the extension (before the query)
52  int firstPossibleExtensionChar = path.LastIndexOfAny(SpaceOrSlash, query - 1) + 1;
53  int extensionStarts = path.IndexOf('.', firstPossibleExtensionChar, query - firstPossibleExtensionChar);
54  if (extensionStarts < 0) extensionStarts = query;
55 
56  return path.Substring(0, extensionStarts) + (!string.IsNullOrEmpty(newExtension) ? "." + newExtension.TrimStart('.') : "") + path.Substring(query);
57 
58  }
static string ImageResizer.Util.PathUtils.ToBase64U ( byte[]  data)
inlinestatic

Converts aribtrary bytes to a URL-safe version of base64 (no = padding, with - instead of + and _ instead of /)

Parameters
data
Returns

Definition at line 437 of file PathUtils.cs.

437  {
438  return Convert.ToBase64String(data).Replace("=", String.Empty).Replace('+', '-').Replace('/', '_');
439  }
static string ImageResizer.Util.PathUtils.ToBase64U ( string  data)
inlinestatic

Converts aribtrary strings to a URL-safe version of base64 (no = padding, with - instead of + and _ instead of /)

Parameters
data
Returns

Definition at line 454 of file PathUtils.cs.

454  {
455  return ToBase64U(UTF8Encoding.UTF8.GetBytes(data));
456  }
static string ToBase64U(byte[] data)
Converts aribtrary bytes to a URL-safe version of base64 (no = padding, with - instead of + and _ ins...
Definition: PathUtils.cs:437
delegate string ImageResizer.Util.PathUtils.VariableResolverCallback ( string  variableName)

A method that resolves variable names to values for the ResolveVariablesInPath method

Parameters
variableName
Returns

Property Documentation

string ImageResizer.Util.PathUtils.AppPhysicalPath
staticget

If not running in ASP.NET, returns the folder containing the DLL.

Definition at line 30 of file PathUtils.cs.

string ImageResizer.Util.PathUtils.AppVirtualPath
staticget

Returns HostingEnvironment.ApplicationVirtualPath or "/", if asp.net is not running

Definition at line 22 of file PathUtils.cs.


The documentation for this class was generated from the following file: