Exemplo n.º 1
0
        private void OnHeaderRequestFinish(HttpRequester req)
        {
            if (req.IsError)
            {
                Error      = req.Error;
                IsFinished = true;
                return;
            }

            using (req)
            {
                var lastModified = req.Response.LastModified;
                // md5(savepath) to save the last modified time
                var metaKey = SavePath;
                _metaFilePath = Path.Combine(_pullerMetaFolderPath, Md5Helper.Md5String(metaKey));

                // no handle for UTC or time zone
                DateTime metaFileDateTime = DateTime.MinValue;
                if (File.Exists(_metaFilePath))
                {
                    long metaFileTicks;
                    var  metaFileTicksStr = File.ReadAllText(_metaFilePath);
                    if (!long.TryParse(metaFileTicksStr, out metaFileTicks))
                    {
                        Error      = new Exception("meta file ticks parse error");
                        IsFinished = true;
                        return;
                    }

                    metaFileDateTime = new DateTime(metaFileTicks);
                }

                _currentLastModifiedUtc = lastModified.ToUniversalTime();
                HasDownloaded           = _currentLastModifiedUtc != metaFileDateTime || !File.Exists(SavePath);

                if (HasDownloaded)
                {
                    // do download
                    _downloader = new HttpDownloader(_url, SavePath);
                    _downloader.SetFinishCallback(OnDownloadFinish);
                    _downloader.Start();
                }
                else
                {
                    // no modified, ignore!
                    IsFinished = true;
                }
            }
        }
        protected internal override void Start()
        {
            // construct the url
            var url = new StringBuilder();

            url.Append(_urlPrefix);
            if (url[url.Length - 1] != '/')
            {
                url.Append('/');
            }

            // zip file name
            var fileName = string.Format("{0}.{1}-{2}{3}", _packageNamePrefix, _checkVersionHandler.LocalVersion,
                                         _checkVersionHandler.RemoteVersion,
                                         _packageExtension);

            url.AppendFormat(fileName);

            // ���̵�ַ
            var savePath = Path.Combine(_downloadToPath, fileName);

            _savePath = savePath;
            _fullUrl  = url.ToString();

            _down = new HttpDownloader(_fullUrl, _savePath, true);
            _down.SetRequsterAsyncHook(_requesterAsyncHook);
            _down.SetFinishCallback((_) =>
            {
                if (_.Error != null)
                {
                    OnError(this, string.Format("{0} : {1}", _fullUrl, _.Error.Message));
                }

                Finish();
            });

            _down.Start();
        }