Skip to content

Commit

Permalink
DownloadCache fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-luberda committed Nov 11, 2016
1 parent 1fc79f0 commit 75b9ba7
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions source/FFImageLoading.Common/Cache/DownloadCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,47 +57,62 @@ public virtual async Task<CacheStream> 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<byte[]> 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)
Expand Down

0 comments on commit 75b9ba7

Please sign in to comment.