DownloadFile() public method

public DownloadFile ( string url, string path ) : void
url string
path string
return void
Beispiel #1
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)
            {
                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)
                {
                    _downloader.DownloadFile(url, file);
                }
            }

            return(new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
        }