Exemplo n.º 1
0
        /// <summary>
        /// Handles all the basic functionality for an error (throw exception if needed and run "ErrorEncountered").
        /// </summary>
        /// <param name="args">The information about the error.</param>
        /// <param name="err">The type the error was.</param>
        /// <param name="exception">The actual exception this error is contained in.</param>
        internal void HandleError(ErrorEncounteredEventArgs args, ABSaveError err, Exception exception)
        {
            // Call the event "ErrorEncountered" - if we aren't meant to IgnoreAllErrors.
            if (!IgnoreAllErrors)
            {
                ErrorEncountered?.Invoke(args);
            }

            // If it isn't suppressed, throw an exception.
            if (!SuppressedErrors.HasFlag(err))
            {
                throw exception;
            }
        }
Exemplo n.º 2
0
 protected void FireError(string title, string message)
 {
     ErrorEncountered?.Invoke(this, new ErrorEventArgs(title, message));
 }
Exemplo n.º 3
0
        async Task StartDownloading()
        {
            while (toDownload.Count > 0)
            {
                if (IsStarted == false || IsStopped)
                {
                    if (IsStopped)
                    {
                        break;
                    }
                    await Task.Delay(300);

                    continue;
                }

                // get next work
                var work = toDownload.Dequeue();

                // go through each page
                for (int i = 0; i < work.PageCount; i++)
                {
start:
                    try
                    {
                        if (IsStarted == false || IsStopped)
                        {
                            while (true)
                            {
                                if (IsStarted)
                                {
                                    break;
                                }
                                await Task.Delay(300);

                                continue;
                            }

                            if (IsStopped)
                            {
                                break;
                            }
                        }

                        var uri = work.GetImageUri(work.OriginalImageUrl, i);

                        // determine extension
                        var extension = uri.Substring(uri.LastIndexOf('.') + 1).ToLower();
                        if (extension != "png" && extension != "gif")
                        {
                            extension = "jpg";
                        }

                        // determine download path
                        //var filename = GetValidFilename(work.Id.Value.ToString(), i, extension);  // <--- this method get's a new filename - ignoring existing ones
                        var filename = $"{work.Id.Value.ToString()}_p{i}.{extension}";
                        var fullpath = Path.Combine(Destination, filename);
                        DownloadProgress?.Report(new DownloaderProgress(work, i, fullpath, 0.0, Percentage)); // report progress

                        // only download if image doesn't exist already
                        if (File.Exists(fullpath) == false)
                        {
                            // download image
                            byte[] buffer = null;
                            using (var client = new WebClient())
                            {
                                client.Headers.Add("Referer", "http://spapi.pixiv.net/");
                                client.Headers.Add("User-Agent", "PixivIOSApp/5.8.0");
                                client.UseDefaultCredentials = true;

                                client.DownloadProgressChanged += (a, b) =>
                                {
                                    DownloadProgress?.Report(new DownloaderProgress(work, i, fullpath, b.ProgressPercentage, Percentage));
                                };

                                buffer = await client.DownloadDataTaskAsync(uri);
                            }

                            // write image
                            File.WriteAllBytes(fullpath, buffer);
                            GC.Collect();
                        }

                        DownloadedImagesCount++;
                        DownloadProgress?.Report(new DownloaderProgress(work, i, fullpath, 100.0, Percentage));
                        DownloadFinished?.Invoke(this, new Tuple <long, int>(work.Id.Value, i));
                    }
                    catch (WebException)
                    {
                        while (true)
                        {
                            // just wait and check if there is internet connection
                            await Task.Delay(2000);

                            if (MainWindow.CheckForInternetConnection())
                            {
                                break;
                            }
                            continue;
                        }
                        goto start;  // oh boi, never thought I would be using this :D
                    }
                    catch (Exception ex)
                    {
                        DownloadedImagesCount++;
                        ErrorEncountered?.Invoke(this, new Tuple <long, int, string>(work.Id.Value, i, ex.Message));
                    }
                }
            }

            IsRunning = false;
            Finished?.Invoke(this, EventArgs.Empty);
        }