示例#1
0
        private CacheMetaData CacheFile(HttpWebResponse resp, string fileName)
        {
            var etag = resp.Headers[HttpResponseHeader.ETag];

            string md5;

            if (etag != null)
            {
                md5 = etag.Split(':')[0].Substring(1);
            }
            else
            {
                md5 = "0";
            }

            CacheMetaData meta = new CacheMetaData(resp.ContentLength, md5);

            _metaData[fileName] = meta;

            using (var sw = File.AppendText(Path.Combine(CachePath, "cache.meta")))
            {
                sw.WriteLine(string.Format("{0} {1} {2}", fileName, resp.ContentLength, md5.ToUpper()));
            }

            return(meta);
        }
示例#2
0
        public void DownloadFile(string url, string path)
        {
            Directory.CreateDirectory(Path.GetDirectoryName(path));

            //using (var client = new HttpClient())
            //{
            //    var msg = client.GetAsync(url).Result;

            //    using (Stream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
            //    {
            //        //CacheMetaData.AddToCache(resp, path);
            //        //CopyToStream(stream, fs, resp.ContentLength);

            //        msg.Content.CopyToAsync(fs).Wait();
            //    }
            //}

            HttpWebRequest request = WebRequest.CreateHttp(url);

            using (HttpWebResponse resp = (HttpWebResponse)request.GetResponseAsync().Result)
                using (Stream stream = resp.GetResponseStream())
                    using (Stream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
                    {
                        CacheMetaData.AddToCache(resp, path);
                        CopyToStream(stream, fs, resp.ContentLength);
                    }
        }
示例#3
0
        public CacheMetaData GetMetaData(string url, string file)
        {
            try
            {
                //HttpClient client = new HttpClient();

                //HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Head, url);

                //HttpResponseMessage response = client.SendAsync(request).Result;

                //var headers = response.Headers;

                HttpWebRequest request = WebRequest.CreateHttp(url);
                request.Method = "HEAD";

                using (HttpWebResponse resp = (HttpWebResponse)request.GetResponseAsync().Result)
                {
                    return(CacheMetaData.AddToCache(resp, file));
                }
            }
            catch
            {
                return(null);
            }
        }
示例#4
0
        public static CacheMetaData AddToCache(HttpWebResponse resp, string file)
        {
            string        md5  = resp.Headers[HttpResponseHeader.ETag].Split(':')[0].Substring(1);
            CacheMetaData meta = new CacheMetaData(resp.ContentLength, md5.ToByteArray());

            meta.Save(file);
            return(meta);
        }
示例#5
0
        public Stream OpenFile(string name, string url, bool isData)
        {
            if (!Enabled)
            {
                return(null);
            }

            if (isData && !CacheData)
            {
                return(null);
            }

            string file = Path.Combine(_cachePath, name);

            Logger.WriteLine("CDNCache: Opening file {0}", file);

            FileInfo fi = new FileInfo(file);

            if (!fi.Exists)
            {
                _downloader.DownloadFile(url, file);
            }

            if (Validate && !_validatedData.Contains(file))
            {
                CacheMetaData meta = CacheMetaData.Load(file) ?? _downloader.GetMetaData(url, file);

                if (meta == null)
                {
                    throw new Exception(string.Format("unable to validate file {0}", file));
                }

                bool sizeOk, md5Ok;

                using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    sizeOk = fs.Length == meta.Size;
                    md5Ok  = _md5.ComputeHash(fs).EqualsTo(meta.MD5);
                }

                if (sizeOk && md5Ok)
                {
                    _validatedData.Add(file);
                }
                else
                {
                    _downloader.DownloadFile(url, file);
                }
            }

            return(new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
        }
示例#6
0
        public CacheMetaData GetMetaData(string url, string file)
        {
            try
            {
                HttpWebRequest request = WebRequest.CreateHttp(url);
                request.Method = "HEAD";

                using (HttpWebResponse resp = (HttpWebResponse)request.GetResponseAsync().Result)
                {
                    return(CacheMetaData.AddToCache(resp, file));
                }
            }
            catch
            {
                return(null);
            }
        }
示例#7
0
        private CacheMetaData CacheFile(HttpWebResponse resp, string fileName)
        {
            var lastModifiedStr = resp.Headers[HttpResponseHeader.LastModified];

            DateTime lastModified = lastModifiedStr != null?DateTime.Parse(lastModifiedStr) : DateTime.MinValue;

            CacheMetaData meta = new CacheMetaData(resp.ContentLength, lastModified);

            _metaData[fileName] = meta;

            using (var sw = File.AppendText(Path.Combine(CachePath, "cache.meta")))
            {
                sw.WriteLine($"{fileName} {resp.ContentLength} {lastModified}");
            }

            return(meta);
        }
示例#8
0
        public CDNCache()
        {
            if (Enabled)
            {
                string metaFile = Path.Combine(CachePath, "cache.meta");

                _metaData = new Dictionary <string, CacheMetaData>(StringComparer.OrdinalIgnoreCase);

                if (File.Exists(metaFile))
                {
                    var lines = File.ReadLines(metaFile);

                    foreach (var line in lines)
                    {
                        string[] tokens = line.Split(' ');
                        _metaData[tokens[0]] = new CacheMetaData(Convert.ToInt64(tokens[1]), tokens[2]);
                    }
                }
            }
        }
示例#9
0
        private CDNCache(CASCConfig config)
        {
            if (Enabled)
            {
                _config = config;

                string metaFile = Path.Combine(CachePath, "cache.meta");

                _metaData = new Dictionary <string, CacheMetaData>(StringComparer.OrdinalIgnoreCase);

                if (File.Exists(metaFile))
                {
                    var lines = File.ReadLines(metaFile);

                    foreach (var line in lines)
                    {
                        string[] tokens = line.Split(new[] { ' ' }, 3);
                        _metaData[tokens[0]] = new CacheMetaData(Convert.ToInt64(tokens[1]), DateTime.Parse(tokens[2]));
                    }
                }
            }
        }