// 3/10/2020 I wish that we could both be there
        public async void Download()
        {
            if (DownloadContent.IsUgoira)
            {
                DownloadGif();
                return;
            }

            string path = null;

            try
            {
                await using var memory = await PixivIO.Download(DownloadContent.GetDownloadUrl(), new Progress <double>(d => Progress = d), cancellationTokenSource.Token);

                if (DownloadContent.FromSpotlight)
                {
                    path = IsFromManga
                        ? Path.Combine(Directory.CreateDirectory(AppContext.DownloadPathProvider.GetSpotlightPath(DownloadContent.SpotlightTitle)).FullName, DownloadContent.Id, AppContext.FileNameFormatter.FormatManga(DownloadContent, MangaIndex))
                        : Path.Combine(Directory.CreateDirectory(AppContext.DownloadPathProvider.GetSpotlightPath(DownloadContent.SpotlightTitle)).FullName, AppContext.FileNameFormatter.Format(DownloadContent));
                }
                else if (IsFromManga)
                {
                    path = Path.Combine(Directory.CreateDirectory(AppContext.DownloadPathProvider.GetMangaPath(DownloadContent.Id)).FullName, AppContext.FileNameFormatter.FormatManga(DownloadContent, MangaIndex));
                }
                else
                {
                    path = Path.Combine(Directory.CreateDirectory(AppContext.DownloadPathProvider.GetIllustrationPath()).FullName, AppContext.FileNameFormatter.Format(DownloadContent));
                }
                if (cancellationTokenSource.IsCancellationRequested)
                {
                    return;
                }
                await using var fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
                memory.WriteTo(fileStream);
                Application.Current.Invoke(() => DownloadFinished?.Invoke(this));
            }
            catch (TaskCanceledException)
            {
                if (path != null && File.Exists(path))
                {
                    File.Delete(path);
                }
            }
            catch (Exception e)
            {
                if (!retried)
                {
                    Restart();
                    retried = true;
                }
                else
                {
                    HandleError(e, path);
                }
            }
        }
Beispiel #2
0
        private async void LoadOrigin()
        {
            LoadingOrigin = true;
            var progress = new Progress <double>(p => Dispatcher.Invoke(() => LoadingIndicator = p));

            await using var mem = await PixivIO.Download(Illust.GetDownloadUrl(), progress, cancellationTokenSource.Token);

            ImgSource     = InternalIO.CreateBitmapImageFromStream(mem);
            LoadingOrigin = false;
            ((BlurEffect)ContentImage.Effect).Radius = 0;
        }
        private async void DownloadGif()
        {
            var downloadPath = GetPath();

            try
            {
                var metadata = await HttpClientFactory.AppApiService().GetUgoiraMetadata(DownloadContent.Id);

                var ugoiraUrl = metadata.UgoiraMetadataInfo.ZipUrls.Medium;
                ugoiraUrl = !ugoiraUrl.EndsWith("ugoira1920x1080.zip")
                    ? Regex.Replace(ugoiraUrl, "ugoira(\\d+)x(\\d+).zip", "ugoira1920x1080.zip")
                    : ugoiraUrl;
                var delay = metadata.UgoiraMetadataInfo.Frames.Select(f => f.Delay / 10).ToArray();
                if (cancellationTokenSource.IsCancellationRequested)
                {
                    return;
                }
                await using var memory = await PixivIO.Download(ugoiraUrl, new Progress <double>(d => Progress = d),
                                                                cancellationTokenSource.Token);

                await using var gifStream =
                                (MemoryStream)InternalIO.MergeGifStream(InternalIO.ReadGifZipEntries(memory), delay);
                if (cancellationTokenSource.IsCancellationRequested)
                {
                    return;
                }
                await using var fileStream = new FileStream(downloadPath, FileMode.OpenOrCreate, FileAccess.ReadWrite,
                                                            FileShare.None);
                gifStream.WriteTo(fileStream);
                DownloadState.Value = DownloadStateEnum.Finished;
            }
            catch (TaskCanceledException)
            {
                if (downloadPath != null && File.Exists(downloadPath))
                {
                    File.Delete(downloadPath);
                }
            }
            catch (Exception e)
            {
                if (!retried)
                {
                    Restart();
                    retried = true;
                }
                else
                {
                    HandleError(e, downloadPath);
                }
            }
        }
        public async void Download()
        {
            if (!modifiable)
            {
                return;
            }

            DownloadState.Value = DownloadStateEnum.Downloading;
            var downloadPath = GetPath();

            if (DownloadContent.IsUgoira)
            {
                DownloadGif();
                return;
            }

            try
            {
                await using var memory = await PixivIO.Download(DownloadContent.GetDownloadUrl(),
                                                                new Progress <double>(d => Progress = d),
                                                                cancellationTokenSource.Token);

                if (cancellationTokenSource.IsCancellationRequested)
                {
                    return;
                }
                await using var fileStream = new FileStream(downloadPath, FileMode.OpenOrCreate, FileAccess.ReadWrite,
                                                            FileShare.None);
                memory.WriteTo(fileStream);
                DownloadState.Value = DownloadStateEnum.Finished;
            }
            catch (OperationCanceledException)
            {
                if (downloadPath != null && File.Exists(downloadPath))
                {
                    File.Delete(downloadPath);
                }
            }
            catch (Exception e)
            {
                if (!retried)
                {
                    Restart();
                    retried = true;
                }
                else
                {
                    HandleError(e, downloadPath);
                }
            }
        }
        private async void DownloadGif()
        {
            var path = Path.Combine(Directory.CreateDirectory(AppContext.DownloadPathProvider.GetIllustrationPath()).FullName, AppContext.FileNameFormatter.FormatGif(DownloadContent));

            try
            {
                var metadata = await HttpClientFactory.AppApiService().GetUgoiraMetadata(DownloadContent.Id);

                var ugoiraUrl = metadata.UgoiraMetadataInfo.ZipUrls.Medium;
                ugoiraUrl = !ugoiraUrl.EndsWith("ugoira1920x1080.zip") ? Regex.Replace(ugoiraUrl, "ugoira(\\d+)x(\\d+).zip", "ugoira1920x1080.zip") : ugoiraUrl;
                var delay = metadata.UgoiraMetadataInfo.Frames.Select(f => f.Delay / 10).ToArray();
                if (cancellationTokenSource.IsCancellationRequested)
                {
                    return;
                }
                await using var memory = await PixivIO.Download(ugoiraUrl, new Progress <double>(d => Progress = d), cancellationTokenSource.Token);

                await using var gifStream = (MemoryStream)PixivIO.MergeGifStream(PixivIO.ReadGifZipEntries(memory), delay);
                if (cancellationTokenSource.IsCancellationRequested)
                {
                    return;
                }
                await using var fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
                gifStream.WriteTo(fileStream);
                Application.Current.Invoke(() => DownloadFinished?.Invoke(this));
            }
            catch (TaskCanceledException)
            {
                if (path != null && File.Exists(path))
                {
                    File.Delete(path);
                }
            }
            catch (Exception e)
            {
                if (!retried)
                {
                    Restart();
                    retried = true;
                }
                else
                {
                    HandleError(e, path);
                }
            }
        }