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);
                }
            }
        }
Ejemplo n.º 2
0
        private async void DownloadGif()
        {
            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(DownloadPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
                gifStream.WriteTo(fileStream);
                DownloadStat.Value = DownloadStatEnum.Finished;
            }
            catch (TaskCanceledException)
            {
                if (DownloadPath != null && File.Exists(DownloadPath))
                {
                    File.Delete(DownloadPath);
                }
            }
            catch (Exception e)
            {
                if (!retried)
                {
                    Restart();
                    retried = true;
                }
                else
                {
                    HandleError(e, DownloadPath);
                }
            }
        }