예제 #1
0
        async Task DownloadImages()
        {
            if (DownloadedImages.Count >= LoadedWork.PageCount)
            {
                return;
            }

            var lworkid = LoadedWork.Id;

            using (await locker.LockAsync())
            {
                try
                {
                    if (lworkid != LoadedWork.Id)
                    {
                        return;
                    }
                    SetProgressBar(true);

                    for (int i = 0; i < LoadedWork.PageCount; i++)
                    {
                        // if page is not yet downloaded, download it
                        ImageSource dimg = null;

                        var page = DownloadedImages.Find(x => x.Page == i + 1);
                        if (page == null)
                        {
                            // start downloading it - befor and after download - check if work has been switched
                            if (isClosing || lworkid != LoadedWork.Id)
                            {
                                break;
                            }
                            dimg = await Task.Run(() => PixivWork.GetImage(LoadedWork.GetImageUri(LoadedWork.OriginalImageUrl, i)));

                            if (isClosing || lworkid != LoadedWork.Id)
                            {
                                break;
                            }
                        }
                        else
                        {
                            dimg = page.ImageData;
                        }

                        // cache image and update status
                        var dwImage = new DownloadedImageData(i + 1, dimg);
                        DownloadedImages.Add(dwImage);
                        CacheDownloads(lworkid.Value, dwImage);
                        SetPageStatus();
                        ImageDownloaded?.Invoke(this, dimg);
                    }
                }
                finally
                {
                    isClosing = false;
                    SetProgressBar(false);
                }
            }
        }
예제 #2
0
        public async Task Handle(ImageDownloaded message)
        {
            _logger.LogInformation(
                "Image {imageId} is available for case number {caseNumber}",
                message.ImageId,
                message.CaseNumber);

            var currentImage = Data.Images.Single(x => x.Id == message.ImageId);

            currentImage.ImageTicket = message.ImageTicket;

            await _bus.Send(new ImageReady
            {
                CaseNumber  = Data.CaseNumber,
                ImageId     = currentImage.Id,
                ImageTicket = currentImage.ImageTicket
            });

            await VerifyIfAllImagesDownloaded();
        }
예제 #3
0
파일: Downloader.cs 프로젝트: elpikel/WIT
 public Downloader(string url, OnPageDownloaded onDownloadedPage, ImageDownloaded onImageDownloaded)
 {
     _url = url;
     DownloadedPage += onDownloadedPage;
     OnDownloadedImage += onImageDownloaded;
 }
        private async Task DownloadImageAsync(Image image)
        {
            try
            {
                if (IsInvalid || _token.IsCancellationRequested)
                {
                    return;
                }

                await WaitAsync(_token).ConfigureAwait(true);

                String formatedSavePath = null;
                if (SaveToDisk)
                {
                    formatedSavePath = GetFormatedFilePath(image);

                    if (String.IsNullOrEmpty(formatedSavePath))
                    {
                        Log.Add(new LogMessage(Globals.Localization.FormatFileNameError,
                                               MessageType.Warning, new[] { image.id.ToString() }));
                        ImageDownloaded?.Invoke(image);
                        ImageSaved?.Invoke(image);
                        return;
                    }

                    if (File.Exists(formatedSavePath) && !Globals.ExistFileRewrite.GetValue())
                    {
                        ImageDownloaded?.Invoke(image);
                        ImageSaved?.Invoke(image);
                        return;
                    }
                }

                Byte[] imageByte = await GetImageAsync(image).ConfigureAwait(true);

                if (_token.IsCancellationRequested || imageByte == null)
                {
                    return;
                }

                ImageDownloaded?.Invoke(image);

                if (SaveToDisk)
                {
                    try
                    {
                        await SaveImageAsync(image, imageByte, formatedSavePath).ConfigureAwait(true);
                    }
                    catch (Exception e)
                    {
                        Log.Add(new LogMessage(e.Message, MessageType.CriticalWarning));
                        IsInvalid = true;
                        MessageForm.GetDialogResultOnException(e, null, null, MessageBoxButtons.OK, new[] { Globals.Localization.Accept });
                    }
                }
            }
            catch (Exception e)
            {
                Log.Add(new LogMessage($"{e.Message}{LocalizationBase.NewLine}{e.StackTrace}", MessageType.CriticalWarning));
                throw;
            }
        }