Ejemplo n.º 1
0
        public virtual void Download(string url, string destFolder)
        {
            this.canceled = false;

            string destFileName = Path.GetFileName(url);

            destFolder         = destFolder.Replace("file:///", "").Replace("file://", "");
            this.downloadingTo = Path.Combine(destFolder, destFileName);

            DirectoriesManager.Create(destFolder);

            if (!File.Exists(downloadingTo))
            {
                using (FileStream fs = File.Create(downloadingTo))
                {
                    fs.Dispose();
                    fs.Close();
                }
            }

            byte[] buffer = new byte[DownloadBlockSize];

            var gotCanceled = false;

            using (FileStream fs = File.Open(downloadingTo, FileMode.Append, FileAccess.Write,
                                             FileShare.Write | FileShare.Delete))
            {
                var currentRetries = 0;

                while (currentRetries < MaxDownloadRetries)
                {
                    DownloadData data = null;

                    try
                    {
                        data = DownloadData.Create(url, destFolder, this.proxy, Credentials);
                        var totalDownloaded = data.StartPoint;
                        int readCount;

                        try
                        {
                            while ((int)(readCount = data.DownloadStream.Read(buffer, 0, DownloadBlockSize)) > 0)
                            {
                                if (canceled)
                                {
                                    gotCanceled = true;
                                    data.Close();
                                    break;
                                }

                                totalDownloaded += readCount;

                                SaveToFile(buffer, readCount, fs);

                                if (data.IsProgressKnown)
                                {
                                    RaiseProgressChanged(totalDownloaded, data.FileSize);
                                }

                                if (canceled)
                                {
                                    gotCanceled = true;
                                    data.Close();
                                    break;
                                }
                            }

                            currentRetries = MaxDownloadRetries;
                        }
                        catch
                        {
                            currentRetries++;

                            if (currentRetries >= MaxDownloadRetries)
                            {
                                fs.Dispose();
                                fs.Close();

                                throw new WebException($"All retries have been tried for {url}.");
                            }

                            Thread.Sleep(DelayForRetryMilliseconds + (DelayForRetryMilliseconds * currentRetries));
                        }
                    }
                    catch (WebException webException)
                    {
                        throw new WebException($"The URL {url} generated an exception.", webException);
                    }
                    catch (UriFormatException e)
                    {
                        throw new ArgumentException(
                                  string.Format(
                                      "Could not parse the URL \"{0}\" - it's either malformed or is an unknown protocol.",
                                      url), e);
                    }
                    finally
                    {
                        if (data != null)
                        {
                            data.Close();
                        }
                    }
                }

                fs.Dispose();
                fs.Close();
            }

            if (!gotCanceled)
            {
                OnDownloadComplete();
            }
        }
Ejemplo n.º 2
0
        public static DownloadData Create(string url, string destFolder, IWebProxy proxy, NetworkCredential credentials)
        {
            DownloadData downloadData = new DownloadData();

            downloadData.proxy = proxy;

            long urlSize = downloadData.GetFileSize(url, credentials);

            downloadData.size = urlSize;

            WebRequest req = downloadData.GetRequest(url, credentials);

            try
            {
                downloadData.response = (WebResponse)req.GetResponse();
            }
            catch (Exception e)
            {
                throw new ArgumentException(String.Format(
                                                "Error downloading \"{0}\": {1}", url, e.Message), e);
            }

            ValidateResponse(downloadData.response, url);

            String fileName = System.IO.Path.GetFileName(downloadData.response.ResponseUri.ToString());

            String downloadTo = Path.Combine(destFolder, fileName);

            if (!downloadData.IsProgressKnown && File.Exists(downloadTo))
            {
                File.Delete(downloadTo);
            }

            if (downloadData.IsProgressKnown && File.Exists(downloadTo))
            {
                if (!(downloadData.Response is HttpWebResponse))
                {
                    File.Delete(downloadTo);
                }
                else
                {
                    downloadData.start = new FileInfo(downloadTo).Length;

                    if (downloadData.start > urlSize)
                    {
                        File.Delete(downloadTo);
                    }
                    else if (downloadData.start < urlSize)
                    {
                        downloadData.response.Close();
                        req = downloadData.GetRequest(url, credentials);
                        ((HttpWebRequest)req).AddRange((int)downloadData.start);
                        downloadData.response = req.GetResponse();

                        if (((HttpWebResponse)downloadData.Response).StatusCode != HttpStatusCode.PartialContent)
                        {
                            File.Delete(downloadTo);
                            downloadData.start = 0;
                        }
                    }
                }
            }
            return(downloadData);
        }