-
Notifications
You must be signed in to change notification settings - Fork 372
Expand file tree
/
Copy pathDownloadCache.cs
More file actions
213 lines (174 loc) · 10.3 KB
/
DownloadCache.cs
File metadata and controls
213 lines (174 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using System;
using System.Threading.Tasks;
using System.IO;
using System.Net.Http;
using FFImageLoading.Helpers;
using System.Threading;
using FFImageLoading.Work;
using FFImageLoading.Config;
using System.Linq;
using FFImageLoading.Exceptions;
namespace FFImageLoading.Cache
{
public class DownloadCache : IDownloadCache
{
public DownloadCache(Configuration configuration)
{
Configuration = configuration;
}
public string[] InvalidContentTypes { get; set; } = new[] { "text/html", "application/json", "audio/", "video/", "message" };
protected Configuration Configuration { get; private set; }
protected virtual IMD5Helper MD5Helper => Configuration.MD5Helper;
public virtual TimeSpan DelayBetweenRetry { get; set; } = TimeSpan.FromSeconds(1);
public virtual async Task<CacheStream> DownloadAndCacheIfNeededAsync(string url, TaskParameter parameters, Configuration configuration, CancellationToken token)
{
var allowCustomKey = !string.IsNullOrWhiteSpace(parameters.CustomCacheKey)
&& (string.IsNullOrWhiteSpace(parameters.LoadingPlaceholderPath) || parameters.LoadingPlaceholderPath != url)
&& (string.IsNullOrWhiteSpace(parameters.ErrorPlaceholderPath) || parameters.ErrorPlaceholderPath != url);
var filename = (allowCustomKey ? MD5Helper.MD5(parameters.CustomCacheKey) : MD5Helper.MD5(url));
var allowDiskCaching = AllowDiskCaching(parameters.CacheType);
var duration = parameters.CacheDuration ?? configuration.DiskCacheDuration;
string filePath = null;
if (allowDiskCaching)
{
var diskStream = await configuration.DiskCache.TryGetStreamAsync(filename).ConfigureAwait(false);
if (diskStream != null)
{
token.ThrowIfCancellationRequested();
filePath = await configuration.DiskCache.GetFilePathAsync(filename).ConfigureAwait(false);
return new CacheStream(diskStream, true, filePath);
}
}
token.ThrowIfCancellationRequested();
var downloadInfo = new DownloadInformation(url, parameters.CustomCacheKey, filename, allowDiskCaching, duration);
parameters.OnDownloadStarted?.Invoke(downloadInfo);
var responseBytes = await Retry.DoAsync(
async () => await DownloadAsync(url, token, configuration.HttpClient, parameters, downloadInfo).ConfigureAwait(false),
parameters.RetryDelayInMs > 0 ? TimeSpan.FromMilliseconds(parameters.RetryDelayInMs) : DelayBetweenRetry,
parameters.RetryCount,
() => configuration.Logger.Debug(string.Format("Retry download: {0}", url))).ConfigureAwait(false);
if (responseBytes == null)
throw new HttpRequestException("No Content");
if (allowDiskCaching)
{
Action finishedAction = null;
var onFileWriteFinished = parameters?.OnFileWriteFinished;
if (onFileWriteFinished != null)
{
finishedAction = new Action(() =>
{
onFileWriteFinished?.Invoke(new FileWriteInfo(filePath, url));
});
}
await configuration.DiskCache.AddToSavingQueueIfNotExistsAsync(filename, responseBytes, downloadInfo.CacheValidity, finishedAction).ConfigureAwait(false);
}
token.ThrowIfCancellationRequested();
filePath = await configuration.DiskCache.GetFilePathAsync(filename).ConfigureAwait(false);
token.ThrowIfCancellationRequested();
var memoryStream = new MemoryStream(responseBytes, false);
return new CacheStream(memoryStream, false, filePath);
}
protected virtual async Task<byte[]> DownloadAsync(string url, CancellationToken token, HttpClient client, TaskParameter parameters, DownloadInformation downloadInformation)
{
if (!parameters.Preload)
{
await Task.Delay(25).ConfigureAwait(false);
token.ThrowIfCancellationRequested();
}
var progressAction = parameters.OnDownloadProgress;
using (var httpHeadersTimeoutTokenSource = new CancellationTokenSource())
using (var headersTimeoutTokenSource = CancellationTokenSource.CreateLinkedTokenSource(token, httpHeadersTimeoutTokenSource.Token))
{
httpHeadersTimeoutTokenSource.CancelAfter(TimeSpan.FromSeconds(Configuration.HttpHeadersTimeout));
try
{
var headerTimeoutToken = headersTimeoutTokenSource.Token;
using (var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, headerTimeoutToken).ConfigureAwait(false))
{
headerTimeoutToken.ThrowIfCancellationRequested();
if (!response.IsSuccessStatusCode)
{
if (response.Content == null)
throw new DownloadHttpStatusCodeException(response.StatusCode);
using (response.Content)
{
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new DownloadHttpStatusCodeException(response.StatusCode, content);
}
}
if (response.Content == null)
throw new DownloadException("No content");
var mediaType = response.Content.Headers?.ContentType?.MediaType;
if (!string.IsNullOrWhiteSpace(mediaType) && !mediaType.StartsWith("image/", StringComparison.OrdinalIgnoreCase))
{
if (InvalidContentTypes.Any(v => mediaType.StartsWith(v, StringComparison.OrdinalIgnoreCase)))
throw new DownloadException($"Invalid response content type ({mediaType})");
}
if (!parameters.CacheDuration.HasValue && Configuration.TryToReadDiskCacheDurationFromHttpHeaders
&& response.Headers?.CacheControl?.MaxAge != null && response.Headers.CacheControl.MaxAge > TimeSpan.Zero)
{
downloadInformation.CacheValidity = response.Headers.CacheControl.MaxAge.Value;
}
ModifyParametersAfterResponse(response, parameters);
using (var httpReadTimeoutTokenSource = new CancellationTokenSource())
using (var readTimeoutTokenSource = CancellationTokenSource.CreateLinkedTokenSource(token, httpReadTimeoutTokenSource.Token))
{
var readTimeoutToken = readTimeoutTokenSource.Token;
var httpReadTimeoutToken = httpReadTimeoutTokenSource.Token;
var total = (int)(response.Content.Headers.ContentLength ?? -1);
var canReportProgress = progressAction != null;
httpReadTimeoutTokenSource.CancelAfter(TimeSpan.FromSeconds(Configuration.HttpReadTimeout));
readTimeoutToken.ThrowIfCancellationRequested();
try
{
using (var outputStream = new MemoryStream())
using (var sourceStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
{
httpReadTimeoutToken.Register(() => sourceStream.TryDispose());
var totalRead = 0;
var buffer = new byte[Configuration.HttpReadBufferSize];
var read = 0;
while ((read = await sourceStream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) > 0)
{
readTimeoutToken.ThrowIfCancellationRequested();
outputStream.Write(buffer, 0, read);
totalRead += read;
if (canReportProgress)
progressAction(new DownloadProgress(totalRead, total));
}
if (outputStream.Length == 0)
throw new DownloadException("Zero length stream");
if (outputStream.Length < 32)
throw new DownloadException("Invalid stream");
return outputStream.ToArray();
}
}
catch (Exception ex) when (ex is OperationCanceledException || ex is ObjectDisposedException)
{
if (httpReadTimeoutTokenSource.IsCancellationRequested)
throw new DownloadReadTimeoutException();
throw;
}
}
}
}
catch (OperationCanceledException)
{
if (httpHeadersTimeoutTokenSource.IsCancellationRequested)
throw new DownloadHeadersTimeoutException();
else
throw;
}
}
}
protected virtual void ModifyParametersAfterResponse(HttpResponseMessage response, TaskParameter parameters)
{
// YOUR CUSTOM LOGIC HERE
// eg: parameters.CacheDuration = response.Headers.CacheControl.MaxAge;
}
protected virtual bool AllowDiskCaching(CacheType? cacheType)
{
return cacheType.HasValue == false || cacheType == CacheType.All || cacheType == CacheType.Disk;
}
}
}