Example #1
0
        private byte[] Download(GalleryAgent agent, ImageViewState imageViewState, GalleryImagePath imagePath, GalleryParameterValues values)
        {
            using (var source = new CancellationTokenSource())
            {
                var cancelSources = this.CancelSources;

                try
                {
                    lock (cancelSources)
                    {
                        this.ThrowIfCancelRequested();
                        cancelSources.Add(source);
                    }

                    var downloadRequest = agent.CreateImageRequest(imageViewState.View, imagePath, values);

                    using (var response = agent.Explorer.Request(downloadRequest, source))
                    {
                        if (response.StatusCode == HttpStatusCode.ServiceUnavailable)
                        {
                            throw new HttpStatusCodeException(response.StatusCode);
                        }

                        imageViewState.Length   = response.ContentLength;
                        imageViewState.Position = 0L;

                        using (var responseStream = response.ReadAsStream())
                        {
                            return(this.Download(imageViewState, responseStream));
                        }
                    }
                }
                finally
                {
                    lock (cancelSources)
                    {
                        cancelSources.Remove(source);
                    }
                }
            }
        }
Example #2
0
        private string Download(int index, ImageViewState viewState, GalleryParameterValues values)
        {
            var agent     = this.Agent;
            var imageView = viewState.View;
            var imagePath = agent.GetGalleryImagePath(imageView, values);

            if (imagePath.ImageUrl == null)
            {
                return("RequestNotCreate");
            }
            else
            {
                var config     = DoujinshiDownloader.Instance.Config.Values.Network;
                var retryCount = config.RetryCount;
                var fileName   = imageView.FileName ?? new Uri(imagePath.ImageUrl).GetFileName();

                for (var k = 0; k < retryCount + 1; k++)
                {
                    try
                    {
                        this.ThrowIfCancelRequested();

                        var bytes = this.Download(agent, viewState, imagePath, values);
                        agent.Validate(imageView, imagePath, values, bytes);
                        this.OnImageDownload(new TaskImageDownloadEventArgs(this, viewState, imagePath, bytes, index, k));

                        var pair = this.ConvertForWrite(fileName, bytes);
                        fileName = pair.Item1;
                        bytes    = pair.Item2;

                        lock (this.DownloadFile)
                        {
                            this.DownloadFile.Write(fileName, bytes);
                        }

                        return(null);
                    }
                    catch (ImageRequestCreateException e)
                    {
                        return(e.Message);
                    }
                    catch (HttpStatusCodeException e)
                    {
                        if (e.Code == HttpStatusCode.ServiceUnavailable)
                        {
                            Thread.Sleep(config.ServiceUnavailableSleep);
                        }
                    }
                    catch (TaskCancelingException)
                    {
                        throw;
                    }
                    catch (Exception e)
                    {
                        if (this.CancelRequested == true)
                        {
                            throw;
                        }

                        Console.WriteLine(e);

                        if (k + 1 == retryCount)
                        {
                            return("Network");
                        }
                        else
                        {
                            if (string.IsNullOrWhiteSpace(imagePath.ReloadUrl) == false)
                            {
                                imagePath = agent.ReloadImagePath(imageView, imagePath, values);
                            }
                        }
                    }
                }
            }

            return("Unknown");
        }