Esempio n. 1
0
        public static FileInfo GetFileInfoFromUrl(string url)
        {
            var invalid = Path.GetInvalidPathChars().ToList();

            foreach (var c in invalid)
            {
                if (url.Contains(c))
                {
                    url = url.Replace(c.ToString(), "_");
                }
            }

            url = url.Replace("?", "_");
            url = url.Replace(":", "-");

            var absPath = "";

            if (new FileInfo(url).Extension == "")
            {
                if (!url.EndsWith("/"))
                {
                    url = url + "/";
                }

                absPath = URIHelper.ConvertToAbsPath($"{baseDir}{url.ToLower()}{htmlFileName}");
            }
            else
            {
                absPath = URIHelper.ConvertToAbsPath($"{baseDir}{url.ToLower()}");
            }

            var fileInfo = new FileInfo(absPath);

            return(fileInfo);
        }
Esempio n. 2
0
        public static string ReadUrl(string url, bool enableCaching = false, long cacheDurationInSeconds = 86400)
        {
            if (url == null)
            {
                return("");
            }

            var absUrl = URIHelper.ConvertToAbsUrl(url).ToLower();

            var    data    = "";
            string absPath = URIHelper.ConvertToAbsPath(URIHelper.ConvertAbsUrlToTilda(url));

            using (FileStream fs = File.Open(absPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                using (BufferedStream bs = new BufferedStream(fs))
                    using (StreamReader sr = new StreamReader(bs))
                    {
                        data = sr.ReadToEnd();
                    }

            if (enableCaching)
            {
                ContextHelper.SaveToCache(absUrl, data, DateTime.Now.AddSeconds(cacheDurationInSeconds));
            }

            return(data);
        }
Esempio n. 3
0
        public Return Validate()
        {
            var returnOnj = BaseMapper.GenerateReturn();

            MediaTypeHandler = MediaTypeHandler.Trim();

            if (!File.Exists(URIHelper.ConvertToAbsPath(URIHelper.ConvertAbsUrlToTilda(MediaTypeHandler))))
            {
                var ex = new Exception("Media Type MediaTypeHandler is invalid", new Exception("File (" + MediaTypeHandler + ") does not exist"));
                returnOnj.Error = ErrorHelper.CreateError(ex);
            }

            return(returnOnj);
        }
Esempio n. 4
0
        public string GetMobileTemplate()
        {
            if (string.IsNullOrEmpty(this.MobileTemplate))
            {
                return(this.PathToFile);
            }

            if (File.Exists(URIHelper.ConvertToAbsPath(this.MobileTemplate)))
            {
                return(this.MobileTemplate);
            }

            return(this.PathToFile);
        }
Esempio n. 5
0
        public static Return ClearAllCache()
        {
            if (string.IsNullOrEmpty(baseDir))
            {
                return(new Return());
            }

            //FileCacheHelper.DeleteGenerateNav();
            //FileCacheHelper.DeleteSettings();
            //FileCacheHelper.DeleteHTMLCache();

            var directoryInfo = new DirectoryInfo(URIHelper.ConvertToAbsPath(baseDir));

            var subDirectories = directoryInfo.GetDirectories();
            var allFiles       = directoryInfo.GetFiles();

            foreach (var file in allFiles)
            {
                try
                {
                    File.Delete(file.FullName);
                }
                catch (Exception ex)
                {
                    ErrorHelper.LogException(ex);
                }
            }

            foreach (var directory in subDirectories)
            {
                try
                {
                    Directory.Delete(directory.FullName, true);
                }
                catch (Exception ex)
                {
                    ErrorHelper.LogException(ex);
                }
            }

            ContextHelper.ClearAllMemoryCache();

            return(new Return());
        }