// 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 PixivIOHelper.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); } } }
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 PixivIOHelper.Download(ugoiraUrl, new Progress <double>(d => Progress = d), cancellationTokenSource.Token); await using var gifStream = (MemoryStream)PixivIOHelper.MergeGifStream(PixivIOHelper.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); } } }