Exemple #1
0
        /// <summary>
        /// Replaces variables in paths with their values. Ex. ~/uploads/thumbs/&lt;guid>.&lt;ext>.
        /// Standard variables are &lt;ext> (the default extension for the final file type), &lt;guid>, a randomly generated GUID,
        /// &lt;filename>, the original filename without it's extension, &lt;path>, the original path and filename without extension,
        /// &lt;settings.width>, (any specified settings value except preset), &lt;width> (final width), and &lt;height> (final height).
        ///
        /// </summary>
        /// <param name="pathWithVars"></param>
        /// <param name="resolver"></param>
        /// <returns></returns>
        public static string ResolveVariablesInPath(string pathWithVars, VariableResolverCallback resolver)
        {
            string p = pathWithVars;

            while (p.IndexOf('<') > -1)
            {
                int start    = p.IndexOf('<');
                int stop     = p.IndexOf('>', start);
                int bugcheck = p.IndexOf('<', start + 1);
                if (stop < 0 || (bugcheck > -1 && bugcheck < stop) || stop == start + 1)
                {
                    throw new ImageProcessingException("Destination paths can only contain < and > in matched pairs to designate variables. Path \"" + pathWithVars + "\" has invalid syntax");
                }
                string varName = p.Substring(start + 1, stop - start - 1);
                string filter  = null;
                //Split off the filter if present.
                int filterIx = varName.IndexOf(':');
                if (filterIx > 0)
                {
                    filter  = varName.Substring(filterIx + 1);
                    varName = varName.Substring(0, filterIx);
                }
                varName = varName.ToLowerInvariant();
                string result = resolver(varName);
                if (!string.IsNullOrEmpty(filter))
                {
                    result = RemoveNonMatchingChars(result, filter);
                }
                if (result == null)
                {
                    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.");
                }
                p = p.Substring(0, start) + result + p.Substring(stop + 1);
            }
            if (p.IndexOf('>') > -1)
            {
                throw new ImageProcessingException("Orphaned '>' in template path \"" + pathWithVars + "\".");
            }
            return(p);
        }
Exemple #2
0
 /// <summary>
 /// Replaces variables in paths with their values. Ex. ~/uploads/thumbs/&lt;guid>.&lt;ext>.
 /// Standard variables are &lt;ext> (the default extension for the final file type), &lt;guid>, a randomly generated GUID, 
 /// &lt;filename>, the original filename without it's extension, &lt;path>, the original path and filename without extension, 
 /// &lt;settings.width>, (any specified settings value except preset), &lt;width> (final width), and &lt;height> (final height).
 /// 
 /// </summary>
 /// <param name="pathWithVars"></param>
 /// <param name="resolver"></param>
 /// <returns></returns>
 public static string ResolveVariablesInPath(string pathWithVars, VariableResolverCallback resolver)
 {
     string p = pathWithVars;
     while (p.IndexOf('<') > -1) {
         int start = p.IndexOf('<');
         int stop = p.IndexOf('>',start);
         int bugcheck = p.IndexOf('<',start + 1);
         if (stop < 0 || (bugcheck > -1 && bugcheck < stop) || stop == start + 1)
             throw new ImageProcessingException("Destination paths can only contain < and > in matched pairs to designate variables. Path \"" + pathWithVars + "\" has invalid syntax");
         string varName = p.Substring(start + 1,stop - start -1);
         string filter = null;
         //Split off the filter if present.
         int filterIx = varName.IndexOf(':');
         if (filterIx > 0) {
             filter = varName.Substring(filterIx + 1);
             varName = varName.Substring(0, filterIx);
         }
         varName = varName.ToLowerInvariant();
         string result = resolver(varName);
         if (!string.IsNullOrEmpty(filter)) {
             result = RemoveNonMatchingChars(result, filter);
         }
         if (result == null)
             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.");
         p = p.Substring(0,start) + result + p.Substring(stop + 1);
     }
     if (p.IndexOf('>') > -1) throw new ImageProcessingException("Orphaned '>' in template path \"" + pathWithVars + "\".");
     return p;
 }