From 75b9ba7bac6782e1341bf326a2da286e6e78ea62 Mon Sep 17 00:00:00 2001 From: Daniel Luberda Date: Fri, 11 Nov 2016 10:14:06 +0100 Subject: [PATCH] DownloadCache fixes --- .../Cache/DownloadCache.cs | 45 ++++++++++++------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/source/FFImageLoading.Common/Cache/DownloadCache.cs b/source/FFImageLoading.Common/Cache/DownloadCache.cs index 77f2871cd..57a1cb842 100644 --- a/source/FFImageLoading.Common/Cache/DownloadCache.cs +++ b/source/FFImageLoading.Common/Cache/DownloadCache.cs @@ -57,47 +57,62 @@ public virtual async Task DownloadAndCacheIfNeededAsync(string url, if (responseBytes == null) return null; - var memoryStream = new MemoryStream(responseBytes, false); - if (allowDiskCaching) { await configuration.DiskCache.AddToSavingQueueIfNotExistsAsync(filename, responseBytes, duration).ConfigureAwait(false); } filePath = await configuration.DiskCache.GetFilePathAsync(filename).ConfigureAwait(false); + var memoryStream = new MemoryStream(responseBytes, false); return new CacheStream(memoryStream, false, filePath); } protected virtual async Task DownloadAsync(string url, CancellationToken token, HttpClient client) { - try + using (var cancelHeadersToken = new CancellationTokenSource()) { - using (var cancelHeadersToken = new CancellationTokenSource()) - { - cancelHeadersToken.CancelAfter(TimeSpan.FromSeconds(Configuration.HttpHeadersTimeout)); + cancelHeadersToken.CancelAfter(TimeSpan.FromSeconds(Configuration.HttpHeadersTimeout)); - using (var linkedHeadersToken = CancellationTokenSource.CreateLinkedTokenSource(token, cancelHeadersToken.Token)) + using (var linkedHeadersToken = CancellationTokenSource.CreateLinkedTokenSource(token, cancelHeadersToken.Token)) + { + try { using (var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, linkedHeadersToken.Token).ConfigureAwait(false)) { - if (!response.IsSuccessStatusCode || response.Content == null) - return null; + if (!response.IsSuccessStatusCode) + throw new HttpRequestException(response.StatusCode.ToString()); + + if (response.Content == null) + throw new HttpRequestException("No HttpContent"); using (var cancelReadTimeoutToken = new CancellationTokenSource()) { cancelReadTimeoutToken.CancelAfter(TimeSpan.FromSeconds(Configuration.HttpReadTimeout)); - return await Task.Run(async () => await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false), - cancelReadTimeoutToken.Token).ConfigureAwait(false); + try + { + return await Task.Run(async () => await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false), + cancelReadTimeoutToken.Token).ConfigureAwait(false); + } + catch (OperationCanceledException) + { + if (cancelReadTimeoutToken.IsCancellationRequested) + throw new Exception("HttpReadTimeout"); + else + throw; + } } } } + catch (OperationCanceledException) + { + if (cancelHeadersToken.IsCancellationRequested) + throw new Exception("HttpHeadersTimeout"); + else + throw; + } } } - catch (OperationCanceledException) - { - throw new Exception("HttpHeadersTimeout"); - } } protected virtual bool AllowDiskCaching(CacheType? cacheType)