Exemple #1
0
        private async void PlayGif_OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ProcessingGif = true;
            var metadata = await HttpClientFactory.AppApiService().GetUgoiraMetadata(Illust.Id);

            var ugoiraZip = metadata.UgoiraMetadataInfo.ZipUrls.Medium;
            var delay     = metadata.UgoiraMetadataInfo.Frames.Select(f => f.Delay / 10).ToArray();
            var streams   = InternalIO.ReadGifZipEntries(await PixivIO.GetBytes(ugoiraZip)).ToArray();

            ProcessingGif = false;
            PlayingGif    = true;

#pragma warning disable 4014
            Task.Run(async() =>
            {
                while (!cancellationToken.IsCancellationRequested)
                {
                    for (var i = 0; i < streams.Length && !cancellationToken.IsCancellationRequested; i++)
                    {
                        streams[i].Position = 0;
                        ImgSource           = InternalIO.CreateBitmapImageFromStream(streams[i]);
                        await Task.Delay((int)delay[i], cancellationToken.Token);
                    }
                }
            });
#pragma warning restore 4014
        }
        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);
                }
            }
        }