Exemplo n.º 1
0
        //public static void Test_UrlToFileName_01(string url, string ext, UrlFileNameType type = UrlFileNameType.FileName)
        //{
        //    Trace.WriteLine("url  \"{0}\"", url);
        //    Trace.WriteLine("ext  \"{0}\"", ext);
        //    Trace.WriteLine("type \"{0}\"", type);
        //    string file = Http_new.UrlToFileName(url, ext, type);
        //    Trace.WriteLine("file \"{0}\"", file);
        //}

        public static void Test_UrlToFileName_01(string url, string ext, UrlFileNameType type = UrlFileNameType.FileName)
        {
            Trace.WriteLine("url  \"{0}\"", url);
            Trace.WriteLine("ext  \"{0}\"", ext);
            Trace.WriteLine("type \"{0}\"", type);
            //string file = Http_new.UrlToFileName(url, ext, type);
            string file = zurl.UrlToFileName(new HttpRequest {
                Url = url
            }, type, ext);

            Trace.WriteLine("file \"{0}\"", file);
        }
Exemplo n.º 2
0
        public static UrlCache CreateIndexedCache(string directory, UrlFileNameType urlFileNameType = UrlFileNameType.Host | UrlFileNameType.Path | UrlFileNameType.Ext | UrlFileNameType.Query)
        {
            if (directory == null)
            {
                return(null);
            }
            UrlCache urlCache = new UrlCache(directory);

            urlCache._urlFileNameType = urlFileNameType;
            urlCache._indexedFile     = true;
            urlCache._saveRequest     = true;
            return(urlCache);
        }
Exemplo n.º 3
0
        public static UrlFileNameType GetUrlFileNameType(string urlFileNameType)
        {
            // Host, Path, Ext, Query, Content
            UrlFileNameType type = 0;

            foreach (string s in urlFileNameType.Split(','))
            {
                switch (s.Trim().ToLowerInvariant())
                {
                case "host":
                    type |= UrlFileNameType.Host;
                    break;

                case "filename":
                    type |= UrlFileNameType.FileName;
                    break;

                case "path":
                    type |= UrlFileNameType.Path;
                    break;

                case "ext":
                    type |= UrlFileNameType.Ext;
                    break;

                case "query":
                    type |= UrlFileNameType.Query;
                    break;

                case "content":
                    type |= UrlFileNameType.Content;
                    break;
                }
            }
            return(type);
        }
Exemplo n.º 4
0
 public UrlCache_v1(string cacheDirectory, UrlFileNameType urlFileNameType = UrlFileNameType.Path, GetUrlSubDirectoryDelegate_v1 getUrlSubDirectory = null)
 {
     _cacheDirectory     = cacheDirectory;
     _urlFileNameType    = urlFileNameType;
     _getUrlSubDirectory = getUrlSubDirectory;
 }
Exemplo n.º 5
0
        public static string UrlToFileName(string url, UrlFileNameType type, string ext = null, string httpRequestContent = null)
        {
            // source from Http.UrlToFileName()

            // url                                   : "http://www.site.com/toto/tata/index.php?name=search&page=2"
            // uri.Host                              : "www.site.com"
            // uri.Authority                         : "www.site.com"
            // uri.DnsSafeHost                       : "www.site.com"
            // uri.AbsoluteUri                       : "http://www.site.com/toto/tata/index.php?name=search&page=2"
            // uri.PathAndQuery                      : "/toto/tata/index.php?name=search&page=2"
            // uri.AbsolutePath                      : "/toto/tata/index.php"
            // uri.LocalPath                         : "/toto/tata/index.php"
            // uri.Query                             : "?name=search&page=2"
            // uri.Segments                          : "/, toto/, tata/, index.php"

            // url                                   : "http://www.site.com/toto/tata/"
            // uri.Host                              : "www.site.com"
            // uri.Authority                         : "www.site.com"
            // uri.DnsSafeHost                       : "www.site.com"
            // uri.AbsoluteUri                       : "http://www.site.com/toto/tata/"
            // uri.PathAndQuery                      : "/toto/tata/"
            // uri.AbsolutePath                      : "/toto/tata/"
            // uri.LocalPath                         : "/toto/tata/"
            // uri.Query                             : ""
            // uri.Segments                          : "/, toto/, tata/"

            // uri.GetComponents(UriComponents.UserInfo)
            // uri.GetLeftPart(UriPartial.Scheme)

            if (url == null)
                return null;

            string file = "";
            //bool queryOrContent = false;
            Uri uri = new Uri(url);

            if ((type & UrlFileNameType.Host) == UrlFileNameType.Host)
            {
                file = uri.Host;
            }

            if ((type & UrlFileNameType.Path) == UrlFileNameType.Path)
            {
                if (file != "")
                    file += "_";
                //file += HttpUtility.UrlDecode(uri.AbsolutePath);
                string urlPath = HttpUtility.UrlDecode(uri.AbsolutePath);
                //urlExt = zPath.GetExtension(urlPath);
                if (urlPath.StartsWith("/"))
                    urlPath = urlPath.Substring(1);
                if (urlPath.EndsWith("/"))
                    urlPath = urlPath.Substring(0, urlPath.Length - 1);

                if ((type & UrlFileNameType.Ext) != UrlFileNameType.Ext)
                {
                    // attention with zpath.PathSetExt() / => \
                    urlPath = zpath.PathSetExtension(urlPath, null);
                }
                file += urlPath;
            }
            else if ((type & UrlFileNameType.FileName) == UrlFileNameType.FileName)
            {
                if (file != "")
                    file += "_";
                //file += HttpUtility.UrlDecode(uri.Segments[uri.Segments.Length - 1]);
                //if (file.EndsWith("/"))
                //    file = file.Substring(0, file.Length - 1);
                string urlFilename = HttpUtility.UrlDecode(uri.Segments[uri.Segments.Length - 1]);
                //urlExt = zPath.GetExtension(urlFilename);
                if (urlFilename.EndsWith("/"))
                    urlFilename = urlFilename.Substring(0, urlFilename.Length - 1);
                if ((type & UrlFileNameType.Ext) != UrlFileNameType.Ext)
                {
                    // attention with zpath.PathSetExt() / => \
                    urlFilename = zpath.PathSetExtension(urlFilename, null);
                }
                file += urlFilename;
            }

            if ((type & UrlFileNameType.Query) == UrlFileNameType.Query)
            {
                if (file != "")
                    file += "_";
                file += HttpUtility.UrlDecode(uri.Query);
                //queryOrContent = true;
            }

            if ((type & UrlFileNameType.Content) == UrlFileNameType.Content)
            {
                //if (requestParameters != null && requestParameters.content != null)
                if (httpRequestContent != null)
                {
                    if (file != "")
                        file += "_";
                    //file += requestParameters.content;
                    file += httpRequestContent;
                }
                //queryOrContent = true;
            }

            //file = __rgBadFilenameChars.Replace(file, "_");
            file = zfile.ReplaceBadFilenameChars(file, "_");
            //file = file.Replace('/', '_');
            //file = file.Replace('?', '_');
            //file = file.Replace('%', '_');
            //file = file.Replace('&', '_');
            file = file.Trim('_');
            file = __rgMultiUnderline.Replace(file, "_");
            //if (ext != null)
            //    file = zpath.PathSetExt(file, ext);
            //else if (!zPath.HasExtension(file))
            //    file = zpath.PathSetExt(file, ".html");

            if (file == "")
                throw new PBException("error can't generate filename from url \"{0}\" of type {1}", url, type);

            if (ext == null)
            {
                ext = zPath.GetExtension(uri.AbsolutePath);
                // modif le 09/11/2015 appel Http.GetContentTypeFromFileExtension() pour vérifier le type d'extension
                if (Http.GetContentTypeFromFileExtension(ext) == null)
                {
                    file += ext;
                    ext = "";
                }
                ////////////// annulation modif le 24/07/2016 ajout else pour ne pas ajouter ".html" si Http.GetContentTypeFromFileExtension(ext) = null
                if (ext == "")
                    ext = ".html";
                //else if (!queryOrContent)
                //    ext = null;
            }

            if (ext != null)
            {
                //if (!queryOrContent)
                //    file = zpath.PathSetExt(file, ext);
                //else
                //    file += ext;
                if (!file.EndsWith(ext))
                    file += ext;
            }
            if (file.Length > __maxFilenameLength)
            {
                ext = zPath.GetExtension(file);
                string filename = zPath.GetFileNameWithoutExtension(file);
                int i = filename.LastIndexOf('.');
                if (i != -1 && filename.Length - i < 10)
                {
                    ext = filename.Substring(i) + ext;
                    filename = filename.Substring(0, i);
                }
                file = filename.Substring(0, filename.Length - (file.Length - __maxFilenameLength)) + ext;
            }

            return file;
        }
Exemplo n.º 6
0
        //public static string UrlToFileName(string url, UrlFileNameType type, string ext = null, HttpRequestParameters requestParameters = null)
        //{
        //    string httpRequestContent = null;
        //    if (requestParameters != null)
        //        httpRequestContent = requestParameters.content;
        //    return UrlToFileName(url, type, ext, httpRequestContent);
        //}

        public static string UrlToFileName(HttpRequest request, UrlFileNameType type, string ext = null)
        {
            return UrlToFileName(request.Url, type, ext, request.Content);
        }
Exemplo n.º 7
0
        //public static string UrlToFileName(string url, UrlFileNameType type, string ext = null, HttpRequestParameters requestParameters = null)
        //{
        //    string httpRequestContent = null;
        //    if (requestParameters != null)
        //        httpRequestContent = requestParameters.content;
        //    return UrlToFileName(url, type, ext, httpRequestContent);
        //}

        //public static string UrlToFileName(HttpRequest request, UrlFileNameType type, string ext = null)
        //{
        //    return UrlToFileName(request.Url, type, ext, request.Content);
        //}

        public static string UrlToFileName(string url, UrlFileNameType type, string ext = null, string httpRequestContent = null)
        {
            // source from Http.UrlToFileName()

            // url                                   : "http://www.site.com/toto/tata/index.php?name=search&page=2"
            // uri.Host                              : "www.site.com"
            // uri.Authority                         : "www.site.com"
            // uri.DnsSafeHost                       : "www.site.com"
            // uri.AbsoluteUri                       : "http://www.site.com/toto/tata/index.php?name=search&page=2"
            // uri.PathAndQuery                      : "/toto/tata/index.php?name=search&page=2"
            // uri.AbsolutePath                      : "/toto/tata/index.php"
            // uri.LocalPath                         : "/toto/tata/index.php"
            // uri.Query                             : "?name=search&page=2"
            // uri.Segments                          : "/, toto/, tata/, index.php"

            // url                                   : "http://www.site.com/toto/tata/"
            // uri.Host                              : "www.site.com"
            // uri.Authority                         : "www.site.com"
            // uri.DnsSafeHost                       : "www.site.com"
            // uri.AbsoluteUri                       : "http://www.site.com/toto/tata/"
            // uri.PathAndQuery                      : "/toto/tata/"
            // uri.AbsolutePath                      : "/toto/tata/"
            // uri.LocalPath                         : "/toto/tata/"
            // uri.Query                             : ""
            // uri.Segments                          : "/, toto/, tata/"

            // uri.GetComponents(UriComponents.UserInfo)
            // uri.GetLeftPart(UriPartial.Scheme)

            if (url == null)
            {
                return(null);
            }

            string file = "";
            //bool queryOrContent = false;
            Uri uri = new Uri(url);

            if ((type & UrlFileNameType.Host) == UrlFileNameType.Host)
            {
                file = uri.Host;
            }

            if ((type & UrlFileNameType.Path) == UrlFileNameType.Path)
            {
                if (file != "")
                {
                    file += "_";
                }
                //file += HttpUtility.UrlDecode(uri.AbsolutePath);
                string urlPath = HttpUtility.UrlDecode(uri.AbsolutePath);
                //urlExt = zPath.GetExtension(urlPath);
                if (urlPath.StartsWith("/"))
                {
                    urlPath = urlPath.Substring(1);
                }
                if (urlPath.EndsWith("/"))
                {
                    urlPath = urlPath.Substring(0, urlPath.Length - 1);
                }

                if ((type & UrlFileNameType.Ext) != UrlFileNameType.Ext)
                {
                    // attention with zpath.PathSetExt() / => \
                    urlPath = zpath.PathSetExtension(urlPath, null);
                }
                file += urlPath;
            }
            else if ((type & UrlFileNameType.FileName) == UrlFileNameType.FileName)
            {
                if (file != "")
                {
                    file += "_";
                }
                //file += HttpUtility.UrlDecode(uri.Segments[uri.Segments.Length - 1]);
                //if (file.EndsWith("/"))
                //    file = file.Substring(0, file.Length - 1);
                string urlFilename = HttpUtility.UrlDecode(uri.Segments[uri.Segments.Length - 1]);
                //urlExt = zPath.GetExtension(urlFilename);
                if (urlFilename.EndsWith("/"))
                {
                    urlFilename = urlFilename.Substring(0, urlFilename.Length - 1);
                }
                if ((type & UrlFileNameType.Ext) != UrlFileNameType.Ext)
                {
                    // attention with zpath.PathSetExt() / => \
                    urlFilename = zpath.PathSetExtension(urlFilename, null);
                }
                file += urlFilename;
            }

            if ((type & UrlFileNameType.Query) == UrlFileNameType.Query)
            {
                if (file != "")
                {
                    file += "_";
                }
                file += HttpUtility.UrlDecode(uri.Query);
                //queryOrContent = true;
            }

            if ((type & UrlFileNameType.Content) == UrlFileNameType.Content)
            {
                //if (requestParameters != null && requestParameters.content != null)
                if (httpRequestContent != null)
                {
                    if (file != "")
                    {
                        file += "_";
                    }
                    //file += requestParameters.content;
                    file += httpRequestContent;
                }
                //queryOrContent = true;
            }

            //file = __rgBadFilenameChars.Replace(file, "_");
            file = zfile.ReplaceBadFilenameChars(file, "_");
            //file = file.Replace('/', '_');
            //file = file.Replace('?', '_');
            //file = file.Replace('%', '_');
            //file = file.Replace('&', '_');
            file = file.Trim('_');
            file = __rgMultiUnderline.Replace(file, "_");
            //if (ext != null)
            //    file = zpath.PathSetExt(file, ext);
            //else if (!zPath.HasExtension(file))
            //    file = zpath.PathSetExt(file, ".html");

            if (file == "")
            {
                throw new PBException("error can't generate filename from url \"{0}\" of type {1}", url, type);
            }

            if (ext == null)
            {
                ext = zPath.GetExtension(uri.AbsolutePath);
                // modif le 09/11/2015 appel Http.GetContentTypeFromFileExtension() pour vérifier le type d'extension
                if (HttpTools.GetContentTypeFromFileExtension(ext) == null)
                {
                    file += ext;
                    ext   = "";
                }
                ////////////// annulation modif le 24/07/2016 ajout else pour ne pas ajouter ".html" si Http.GetContentTypeFromFileExtension(ext) = null
                if (ext == "")
                {
                    ext = ".html";
                }
                //else if (!queryOrContent)
                //    ext = null;
            }

            if (ext != null)
            {
                //if (!queryOrContent)
                //    file = zpath.PathSetExt(file, ext);
                //else
                //    file += ext;
                if (!file.EndsWith(ext))
                {
                    file += ext;
                }
            }
            if (file.Length > __maxFilenameLength)
            {
                ext = zPath.GetExtension(file);
                string filename = zPath.GetFileNameWithoutExtension(file);
                int    i        = filename.LastIndexOf('.');
                if (i != -1 && filename.Length - i < 10)
                {
                    ext      = filename.Substring(i) + ext;
                    filename = filename.Substring(0, i);
                }
                file = filename.Substring(0, filename.Length - (file.Length - __maxFilenameLength)) + ext;
            }

            return(file);
        }
Exemplo n.º 8
0
 public UrlCache_v1(string cacheDirectory, UrlFileNameType urlFileNameType = UrlFileNameType.Path, GetUrlSubDirectoryDelegate_v1 getUrlSubDirectory = null)
 {
     _cacheDirectory = cacheDirectory;
     _urlFileNameType = urlFileNameType;
     _getUrlSubDirectory = getUrlSubDirectory;
 }
Exemplo n.º 9
0
 public static string UrlToFileName(string sUrl, string sExt, UrlFileNameType type)
 {
     string sFile;
     Uri uri = new Uri(sUrl);
     if (type == UrlFileNameType.FileName)
     {
         sFile = uri.LocalPath;
         while (sFile.StartsWith("/")) sFile = sFile.Remove(0, 1);
         sFile = sFile.Replace("/", "_");
         if (sFile != "")
             sFile = uri.Host + "_" + sFile;
         else
             sFile = uri.Host;
         if (sExt != null) sFile = sFile + sExt;
     }
     else
     {
         //sFile = uri.AbsoluteUri;
         //sFile = sUrl;
         if (type == UrlFileNameType.Path)
             sFile = uri.AbsolutePath;
         else if (type == UrlFileNameType.PathAndQuery)
             sFile = uri.PathAndQuery;
         else if (type == UrlFileNameType.Query)
             sFile = uri.Query;
         else
             return null;
         int i = sFile.IndexOf("://");
         if (i != -1) sFile = cu.right(sFile, sFile.Length - i - 3);
         i = sFile.IndexOf('?');
         if (i != -1) sFile = cu.left(sFile, i);
         sFile = sFile.Replace('/', '_');
         //sFile = sFile.Replace('?', '_');
         sFile = sFile.Replace('%', '_');
         //sFile = cu.PathGetFile(sFile) + ".html";
         if (sExt != null) sFile = sFile + sExt;
     }
     sFile = Path.GetFileName(sFile);
     return sFile;
 }
Exemplo n.º 10
0
        //public static void Test_UrlToFileName_01(string url, string ext, UrlFileNameType type = UrlFileNameType.FileName)
        //{
        //    Trace.WriteLine("url  \"{0}\"", url);
        //    Trace.WriteLine("ext  \"{0}\"", ext);
        //    Trace.WriteLine("type \"{0}\"", type);
        //    string file = Http_new.UrlToFileName(url, ext, type);
        //    Trace.WriteLine("file \"{0}\"", file);
        //}

        public static void Test_UrlToFileName_01(string url, string ext, UrlFileNameType type = UrlFileNameType.FileName)
        {
            Trace.WriteLine("url  \"{0}\"", url);
            Trace.WriteLine("ext  \"{0}\"", ext);
            Trace.WriteLine("type \"{0}\"", type);
            //string file = Http_new.UrlToFileName(url, ext, type);
            string file = zurl.UrlToFileName(new HttpRequest { Url = url }, type, ext);
            Trace.WriteLine("file \"{0}\"", file);
        }