Ejemplo n.º 1
0
        public bool DownloadImage()
        {
            try {
                ImageInfo Image = new ImageInfo(DownloadInfo);

                switch (Image.Status)
                {
                case DownloadStatus.FileAlreadyExists:
                    return(true);

                case DownloadStatus.ReadyToDownload:
                    // Download file.
                    using (DownloadClient = new Controls.ExtendedWebClient()) {
                        DownloadClient.Proxy = WebRequest.GetSystemWebProxy();
                        DownloadClient.Headers.Add("User-Agent: " + Program.UserAgent);
                        DownloadClient.Method = "GET";

                        DownloadClient.DownloadFile(Image.FileUrl, Image.DownloadPath);
                    }

                    return(true);

                default:
                    return(false);
                }
            }
            catch (PoolOrPostWasDeletedException) {
                System.Media.SystemSounds.Hand.Play();
                ErrorLog.ReportCustomException("The image was deleted.", "ImageDownloader.cs");
                return(false);
            }
            catch (ApiReturnedNullOrEmptyException) {
                System.Media.SystemSounds.Hand.Play();
                ErrorLog.ReportCustomException("The api returned null or empty.", "ImageDownloader.cs");
                return(false);
            }
            catch (NoFilesToDownloadException) {
                System.Media.SystemSounds.Hand.Play();
                ErrorLog.ReportCustomException("No image is available to download.", "ImageDownloader.cs");
                return(false);
            }
            catch (ImageWasNullAfterBypassingException) {
                ErrorLog.ReportCustomException("The image was null after attempting to bypassing the e621 global blacklist.", "ImageDownloader.cs");
                return(false);
            }
            catch (ThreadAbortException) {
                if (DownloadClient != null && DownloadClient.IsBusy)
                {
                    DownloadClient.CancelAsync();
                }
                return(false);
            }
            catch (ObjectDisposedException) {
                return(false);
            }
            catch (WebException WebE) {
                System.Media.SystemSounds.Hand.Play();
                ErrorLog.ReportWebException(WebE, DownloadInfo.ImageUrl, "ImageDownloader.cs");
                return(false);
            }
            catch (Exception ex) {
                System.Media.SystemSounds.Hand.Play();
                if (DownloadClient != null && DownloadClient.IsBusy)
                {
                    DownloadClient.CancelAsync();
                }
                ErrorLog.ReportException(ex, "ImageDownloader.cs");
                return(false);
            }
        }
Ejemplo n.º 2
0
        public async void DownloadImage()
        {
            try {
                this.Invoke((Action) delegate {
                    lbInfo.Text = "Downloading image id " + DownloadInfo.PostId;
                    status.Text = "Waiting for JSON parse...";
                });

                ImageInfo Image = new ImageInfo(DownloadInfo);
                CurrentStatus = Image.Status;

                if (Image.Status == DownloadStatus.ReadyToDownload)
                {
                    this.Invoke((Action) delegate {
                        status.Text            = "Downloading image...";
                        pbDownloadStatus.Value = 0;
                        pbDownloadStatus.Style = ProgressBarStyle.Blocks;
                        pbDownloadStatus.State = aphrodite.Controls.ProgressBarState.Normal;
                    });

                    // Download file.
                    using (DownloadClient = new Controls.ExtendedWebClient()) {
                        DownloadClient.DownloadProgressChanged += (s, e) => {
                            this.Invoke((Action) delegate {
                                pbDownloadStatus.Value = e.ProgressPercentage;
                                pbDownloadStatus.Value++;
                                pbDownloadStatus.Value--;
                                lbPercentage.Text = e.ProgressPercentage.ToString() + "%";
                            });
                        };
                        DownloadClient.DownloadFileCompleted += (s, e) => {
                            this.Invoke((Action) delegate {
                                pbDownloadStatus.Value = 100;
                                lbPercentage.Text      = "Done";
                            });
                        };

                        DownloadClient.Proxy = WebRequest.GetSystemWebProxy();
                        DownloadClient.Headers.Add("User-Agent: " + Program.UserAgent);
                        DownloadClient.Method = "GET";

                        await DownloadClient.DownloadFileTaskAsync(Image.FileUrl, Image.DownloadPath);
                    }
                }

                this.Invoke((Action) delegate {
                    Program.Log(LogAction.WriteToLog, "The image " + DownloadInfo.PostId + " was downloaded. (frmImageDownloader.cs)");
                });
                CurrentStatus = DownloadStatus.Finished;
            }
            catch (PoolOrPostWasDeletedException) {
                this.Invoke((Action) delegate {
                    Program.Log(LogAction.WriteToLog, "The image " + DownloadInfo.PostId + " was deleted. (frmImageDownloader.cs)");
                });
                CurrentStatus = DownloadStatus.PostOrPoolWasDeleted;
            }
            catch (ApiReturnedNullOrEmptyException) {
                this.Invoke((Action) delegate {
                    Program.Log(LogAction.WriteToLog, "The image " + DownloadInfo.PostId + " API Returned null or empty. (frmImageDownloader.cs)");
                });
                CurrentStatus = DownloadStatus.ApiReturnedNullOrEmpty;
            }
            catch (NoFilesToDownloadException) {
                this.Invoke((Action) delegate {
                    Program.Log(LogAction.WriteToLog, "No files are available for image " + DownloadInfo.PostId + ". (frmImageDownloader.cs)");
                });
                CurrentStatus = DownloadStatus.NothingToDownload;
            }
            catch (ImageWasNullAfterBypassingException) {
                this.Invoke((Action) delegate {
                    Program.Log(LogAction.WriteToLog, "The image " + DownloadInfo.PostId + " was still null after trying to bypass e621's hard filter. (frmImageDownloader.cs)");
                });
                CurrentStatus = DownloadStatus.FileWasNullAfterBypassingBlacklist;
            }
            catch (ThreadAbortException) {
                this.Invoke((Action) delegate {
                    Program.Log(LogAction.WriteToLog, "The download thread was aborted. (frmImageDownloader.cs)");
                });
                if (DownloadClient != null && DownloadClient.IsBusy)
                {
                    DownloadClient.CancelAsync();
                }
                CurrentStatus = DownloadStatus.Aborted;
            }
            catch (ObjectDisposedException) {
                this.Invoke((Action) delegate {
                    Program.Log(LogAction.WriteToLog, "Seems like the form got disposed. (frmImageDownloader.cs)");
                });
                CurrentStatus = DownloadStatus.FormWasDisposed;
            }
            catch (WebException WebE) {
                this.Invoke((Action) delegate {
                    Program.Log(LogAction.WriteToLog, "A WebException occured. (frmImageDownloader.cs");
                    status.Text            = "A WebException has occured";
                    pbDownloadStatus.State = aphrodite.Controls.ProgressBarState.Error;
                });
                ErrorLog.ReportWebException(WebE, DownloadInfo.ImageUrl, "frmImageDownloader.cs");
                CurrentStatus = DownloadStatus.Errored;
            }
            catch (Exception ex) {
                this.Invoke((Action) delegate {
                    Program.Log(LogAction.WriteToLog, "An Exception occured. (frmImageDownloader.cs");
                    status.Text            = "An Exception has occured";
                    pbDownloadStatus.State = aphrodite.Controls.ProgressBarState.Error;
                });
                if (DownloadClient != null && DownloadClient.IsBusy)
                {
                    DownloadClient.CancelAsync();
                }
                ErrorLog.ReportException(ex, "frmImageDownloader.cs");
                CurrentStatus = DownloadStatus.Errored;
            }
            finally {
                if (CurrentStatus != DownloadStatus.FormWasDisposed)
                {
                    this.Invoke((Action) delegate {
                        AfterDownload();
                    });
                }
            }
        }