Ejemplo n.º 1
0
        /// <summary>
        /// Redownload files with given relative filenames.
        /// </summary>
        /// <returns>RunWorkerCompletedEventArgs. True if the download is succeeded, otherwise false.</returns>
        public static RunWorkerCompletedEventArgs RedownloadFile(ExtendedWebClient _webClient, string relativeFilename, string destinationFullfilename, Func <int, bool> progress_callback)
        {
            bool      continueDownload = true;
            Exception Myex             = null;
            Uri       currenturl       = null;
            DownloadProgressChangedEventHandler ooooo = null;

            if (progress_callback != null)
            {
                ooooo = new DownloadProgressChangedEventHandler(delegate(object sender, DownloadProgressChangedEventArgs e)
                {
                    if (progress_callback.Invoke(e.ProgressPercentage))
                    {
                        continueDownload = false;
                        _webClient.CancelAsync();
                    }
                });
            }
            if (ooooo != null)
            {
                _webClient.DownloadProgressChanged += ooooo;
            }
            try
            {
                HttpStatusCode lastCode;
                var            _pso22fileurl = new PSO2FileUrl(Leayal.UriHelper.URLConcat(DefaultValues.Web.MainDownloadLink, relativeFilename), Leayal.UriHelper.URLConcat(DefaultValues.Web.OldDownloadLink, relativeFilename));
                currenturl = _pso22fileurl.MainUrl;
                lastCode   = HttpStatusCode.ServiceUnavailable;
                try
                {
                    _webClient.AutoUserAgent = true;
                    _webClient.DownloadFile(currenturl, destinationFullfilename);
                    _webClient.AutoUserAgent = false;
                }
                catch (WebException webEx)
                {
                    if (webEx.Response != null)
                    {
                        HttpWebResponse rep = webEx.Response as HttpWebResponse;
                        lastCode = rep.StatusCode;
                    }
                    else
                    {
                        throw webEx;
                    }
                }
                if (lastCode == HttpStatusCode.NotFound)
                {
                    currenturl = _pso22fileurl.GetTheOtherOne(currenturl.OriginalString);
                    try
                    {
                        _webClient.AutoUserAgent = true;
                        _webClient.DownloadFile(currenturl, destinationFullfilename);
                        _webClient.AutoUserAgent = false;
                    }
                    catch (WebException webEx)
                    {
                        if (webEx.Response != null)
                        {
                            HttpWebResponse rep = webEx.Response as HttpWebResponse;
                            if (rep.StatusCode != HttpStatusCode.NotFound)
                            {
                                throw webEx;
                            }
                        }
                        else
                        {
                            throw webEx;
                        }
                    }
                }
            }
            catch (Exception ex) { Myex = ex; }
            if (ooooo != null)
            {
                _webClient.DownloadProgressChanged -= ooooo;
            }
            return(new RunWorkerCompletedEventArgs(null, Myex, !continueDownload));
        }