예제 #1
0
        private byte[] Download(ImageViewState imageViewState, Stream responseStream)
        {
            using (var ms = new MemoryStream())
            {
                var buffer = new byte[81920];
                var len    = 0;

                for (; (len = responseStream.Read(buffer, 0, buffer.Length)) > 0;)
                {
                    ms.Write(buffer, 0, len);
                    imageViewState.Position += len;

                    try
                    {
                        this.OnDownloading(new TaskProgressingEventArgs(imageViewState));
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                }

                return(ms.ToArray());
            }
        }
 public TaskImageDownloadEventArgs(DownloadTask task, ImageViewState view, GalleryImagePath image, byte[] data, int index, int retryCount)
 {
     this.Task       = task;
     this.View       = view;
     this.Image      = image;
     this.Data       = data;
     this.Index      = index;
     this.RetryCount = retryCount;
 }
예제 #3
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);
                    }
                }
            }
        }
예제 #4
0
 public TaskProgressingEventArgs(ImageViewState viewState)
 {
     this.ViewState = viewState;
 }
예제 #5
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");
        }