Example #1
0
        /// <summary>
        /// The actual downloader
        /// </summary>
        /// <param name="fileDownloadInfo">Info of the file to download</param>
        /// <returns>Exception, null if none</returns>
        private void Download(FileDownloadInfo fileDownloadInfo)
        {
            var directoryName = Path.GetDirectoryName(fileDownloadInfo.LocalFile.ToString());
            if (!Directory.Exists(directoryName))
            {
                Directory.CreateDirectory(directoryName);
            }

            var client = new WebClient();

            try
            {
                client.DownloadFile(fileDownloadInfo.Url, fileDownloadInfo.LocalFile.ToString());
                Logger.Info("Successfully downloaded {0} to {1}", fileDownloadInfo.Url, fileDownloadInfo.LocalFile);
                return;
            }
            catch (Exception e)
            {
                if (fileDownloadInfo.Retries > 0)
                {
                    Logger.TraceException(string.Format("Error while downloading {0} to {1}.", fileDownloadInfo.Url, fileDownloadInfo.LocalFile), e);
                    throw new FileDownloadRetryException();
                } 
                else
                {
                    Logger.ErrorException(string.Format("Error while downloading {0} to {1}. Giving up.", fileDownloadInfo.Url, fileDownloadInfo.LocalFile), e);
                    throw;
                }
            }
        }
Example #2
0
        /// <summary>
        /// Add a file to download to the queue
        /// You have to subscribe to the <see cref="DownloadProgressNotification"/> event to get notified when the download is finished.
        /// </summary>
        /// <param name="uri">File to download</param>
        /// <param name="localFile">Local path to download to</param>
        /// <param name="identifier">Arbitrary identifier</param>
        /// <param name="overwriteExistingFile">Overwrite an already existing file</param>
        /// <param name="retries">Number of times to retry the download until we fail</param>
        /// <param name="priority">Priority for the WorkItem</param>
        public void AddDownload(Uri uri, FileInfo localFile, object identifier, bool overwriteExistingFile, int retries, WorkItemPriority priority)
        {
            var fileDownloadInfo = new FileDownloadInfo{ Url = uri, Identifier = identifier, LocalFile = localFile, Priority = priority, Retries = retries, OverwriteExistingFile = overwriteExistingFile };

            lock (downloadThreadPoolLockObject)
            {
                downloadThreadPool.QueueWorkItem(this.DoWork, fileDownloadInfo, priority);
            }
        }
Example #3
0
        /// <summary>
        /// Do it!
        /// </summary>
        /// <param name="fileDownloadInfo">Info of the file to download</param>
        private void DoWork(FileDownloadInfo fileDownloadInfo)
        {
            Exception resultException = null;

            try
            {
                if (fileDownloadInfo.LocalFile.Exists && !fileDownloadInfo.OverwriteExistingFile)
                {
                    Logger.Warn("File {0} already exists. Skipping.", fileDownloadInfo.LocalFile);
                    resultException = new FileDownloadExistsException();
                }
                else
                {
                    try
                    {
                        this.Download(fileDownloadInfo);
                    }
                    catch (FileDownloadRetryException)
                    {
                        lock (downloadThreadPoolLockObject)
                        {
                            downloadThreadPool.QueueWorkItem(this.DoWork, fileDownloadInfo, fileDownloadInfo.Priority);
                        }

                        return;
                    }
                    catch (Exception e)
                    {
                        resultException = e;
                    }
                }
            } 
            catch (Exception e)
            {
                resultException = e;
            }

            var eventArgs = new FileDownloadProgressEventArgs(fileDownloadInfo, resultException);
            DownloadProgressNotification(this, eventArgs);
        }
 public FileDownloadProgressEventArgs(FileDownloadInfo fileDownloadInfo, Exception e)
 {
     this.FileDownloadInfo = fileDownloadInfo;
     this.Exception = e;
 }