Ejemplo n.º 1
0
        public static async Task <MemoryStream> DownloadImage(string Uri, CancellationToken CancellationToken, ManualResetEvent PauseEvent = null, Func <long, long, Task> ProgressCallback = null)
        {
            //var Uri = _Uri;
            //if (Uri.StartsWith("https"))
            //{
            //    Uri = Uri.Replace("https://", "http://");
            //}
            var tmpFileName = Uri.Split('/').Last();
            var cachedFile  = await CacheManager.GetCachedFileAsync(tmpFileName);

            if (cachedFile == null)
            {
                //没有对应的缓存文件
                var res = await new PixivAppAPI(GlobalBaseAPI).RequestCall("GET",
                                                                           Uri, new Dictionary <string, string>()
                {
                    { "Referer", "https://app-api.pixiv.net/" }
                });
                //读取长度
                var length = res.Content.Headers.ContentLength ?? -1;
                using (var resStream = await res.Content.ReadAsStreamAsync())
                {
                    var memStream = new MemoryStream();
                    memStream.Position = 0;
                    var    bytesCounter = 0L;
                    int    bytesRead;
                    byte[] buffer = new byte[4096];
                    try
                    {
                        while ((bytesRead = await resStream.ReadAsync(buffer, 0, 4096, CancellationToken)) != 0)
                        {
                            PauseEvent?.WaitOne();
                            bytesCounter += bytesRead;
                            await memStream.WriteAsync(buffer, 0, bytesRead, CancellationToken);

                            _ = ProgressCallback?.Invoke(bytesCounter, length);
                        }
                    }
                    catch (TaskCanceledException)
                    {
                        memStream.Position = 0;
                        return(memStream);
                    }
                    catch
                    {
                        throw;
                    }
                    memStream.Position = 0;
                    var newCachedFile = await CacheManager.CreateCacheFileAsync(tmpFileName);

                    using (var fileStream = await newCachedFile.Value.File.OpenStreamForWriteAsync())
                        await memStream.CopyToAsync(fileStream);
                    await CacheManager.FinishCachedFileAsync(newCachedFile.Value, true);

                    memStream.Position = 0;
                    return(memStream);
                }
            }
            else
            {
                //有缓存文件
                using (var fileStream = await cachedFile.OpenStreamForReadAsync())
                {
                    var memStream = new MemoryStream();
                    await fileStream.CopyToAsync(memStream);

                    var length = memStream.Length;
                    _ = ProgressCallback?.Invoke(length, length);
                    memStream.Position = 0;
                    return(memStream);
                }
            }
        }