forked from microsoft/BuildCast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BackgroundDownloadHelper.cs
419 lines (363 loc) · 14.9 KB
/
BackgroundDownloadHelper.cs
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// ******************************************************************
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THE CODE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE.
// ******************************************************************
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading.Tasks;
using BuildCast.DataModel;
using Microsoft.Toolkit.Uwp.Helpers;
using Microsoft.Toolkit.Uwp.Notifications;
using Windows.ApplicationModel.Background;
using Windows.Networking.BackgroundTransfer;
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Notifications;
namespace BuildCast.Helpers
{
public enum DownloadStartResult
{
Started,
Error,
AllreadyDownloaded,
}
public static class BackgroundDownloadHelper
{
private static ToastNotifier _notifier = ToastNotificationManager.CreateToastNotifier();
public static async Task<DownloadStartResult> Download(Uri sourceUri)
{
var hash = SafeHashUri(sourceUri);
var file = await CheckLocalFileExistsFromUriHash(sourceUri);
var downloadingAlready = await IsDownloading(sourceUri);
if (file == null && !downloadingAlready)
{
await Task.Run(() =>
{
var task = StartDownload(sourceUri, BackgroundTransferPriority.High, hash);
task.ContinueWith((state) =>
{
if (state.Exception != null)
{
DispatcherHelper.ExecuteOnUIThreadAsync(async () =>
{
await UIHelpers.ShowContentAsync($"An error occured with this download {state.Exception}");
});
}
else
{
Debug.WriteLine("Download Completed");
}
});
});
return DownloadStartResult.Started;
}
else if (file != null)
{
await SetItemDownloaded(hash);
await UIHelpers.ShowContentAsync($"Already downloaded.");
return DownloadStartResult.AllreadyDownloaded;
}
else
{
return DownloadStartResult.Error;
}
}
public static async Task<bool> IsDownloading(Uri sourceUri)
{
var downloads = await BackgroundDownloader.GetCurrentDownloadsAsync();
if (downloads.Where(dl => dl.RequestedUri == sourceUri).FirstOrDefault() != null)
{
return true;
}
return false;
}
public static async Task<StorageFile> CheckLocalFileExistsFromUriHash(Uri sourceUri)
{
string hash = SafeHashUri(sourceUri);
return await CheckLocalFileExists(hash);
}
public static async Task<IReadOnlyList<IStorageItem>> GetAllFiles()
{
var files = await ApplicationData.Current.LocalCacheFolder.GetItemsAsync();
return files;
}
public static string SafeHashUri(Uri sourceUri)
{
string safeUri = sourceUri.ToString().ToLower();
var hash = Hash(safeUri);
return hash;
}
public static async Task UpdateDownloadedFromCache()
{
using (LocalStorageContext c = new LocalStorageContext())
{
var foundInDb = c.EpisodeCache.FirstOrDefault();
foundInDb.IsDownloaded = true;
await c.SaveChangesAsync();
}
}
public static void RegisterBackgroundTask(IBackgroundTrigger trigger)
{
var builder = new BackgroundTaskBuilder();
builder.Name = "DownloadCompleteTrigger";
builder.SetTrigger(trigger);
BackgroundTaskRegistration task = builder.Register();
}
public static async Task AttachToDownloads()
{
var downloads = await BackgroundDownloader.GetCurrentDownloadsAsync();
foreach (var download in downloads)
{
Progress<DownloadOperation> progressCallback = new Progress<DownloadOperation>(DownloadProgress);
await download.AttachAsync().AsTask(progressCallback);
}
}
/// <summary>
/// Deletes the downloaded file.
/// </summary>
/// <param name="fileName">filename</param>
/// <returns>Task</returns>
public static async Task DeleteDownload(string fileName)
{
var fileItem = await ApplicationData.Current.LocalCacheFolder.TryGetItemAsync(fileName);
if (fileItem != null)
{
await fileItem.DeleteAsync();
}
}
/// <summary>
/// This will be called when the app is background activated as a result of a download trigger
/// Enables the database to be updated according to result of download
/// </summary>
/// <param name="instance">A background task instance</param>
/// <returns>a task</returns>
internal static async Task CheckCompletionResult(IBackgroundTaskInstance instance)
{
var deferral = instance.GetDeferral();
var trigger = instance.TriggerDetails as BackgroundTransferCompletionGroupTriggerDetails;
if (trigger != null)
{
foreach (var item in trigger.Downloads)
{
// TODO: handle other bad statuses
if (item.Progress.Status == BackgroundTransferStatus.Error)
{
await item.ResultFile.DeleteAsync();
}
else if (item.Progress.Status == BackgroundTransferStatus.Completed)
{
await SetItemDownloaded(item);
}
}
}
deferral?.Complete();
}
private static async Task SetItemDownloaded(DownloadOperation item)
{
await SetItemDownloaded(item.ResultFile.Name);
DownloadProgress(item);
}
private static async Task SetItemDownloaded(string filename)
{
using (var db = new LocalStorageContext())
{
var dbItems = db.EpisodeCache.Where(ep => ep.LocalFileName == filename);
foreach (var dbMatch in dbItems)
{
dbMatch.IsDownloaded = true;
}
await db.SaveChangesAsync();
}
}
private static async Task<StorageFile> CheckLocalFileExists(string fileName)
{
StorageFile file = null;
try
{
file = await ApplicationData.Current.LocalCacheFolder.GetFileAsync(fileName);
var props = await file.GetBasicPropertiesAsync();
if (props.Size == 0)
{
await file.DeleteAsync();
return null;
}
}
catch (FileNotFoundException)
{
}
return file;
}
private static string Hash(string input)
{
IBuffer buffer = CryptographicBuffer.ConvertStringToBinary(input, BinaryStringEncoding.Utf8);
HashAlgorithmProvider hashAlgorithm = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1);
var hashByte = hashAlgorithm.HashData(buffer).ToArray();
var sb = new StringBuilder(hashByte.Length * 2);
foreach (byte b in hashByte)
{
sb.Append(b.ToString("x2"));
}
return sb.ToString();
}
private static async Task<bool> IsFileEmpty(StorageFile file)
{
if (file != null)
{
var props = await file.GetBasicPropertiesAsync();
return props.Size == 0;
}
return true;
}
private static async Task<StorageFile> GetLocalFileFromUri(Uri sourceUri)
{
string filename = GetFileNameFromUri(sourceUri);
StorageFile file = null;
try
{
file = await ApplicationData.Current.LocalCacheFolder.CreateFileAsync(filename);
}
catch (FileNotFoundException)
{
}
return file;
}
private static async Task<StorageFile> GetLocalFileFromName(string filename)
{
StorageFile file = null;
try
{
file = await ApplicationData.Current.LocalCacheFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
}
catch (FileNotFoundException)
{
}
return file;
}
private static string GetFileNameFromUri(Uri sourceUri)
{
return System.IO.Path.GetFileName(sourceUri.PathAndQuery);
}
private static async Task StartDownload(Uri target, BackgroundTransferPriority priority, string localFilename)
{
var result = await BackgroundExecutionManager.RequestAccessAsync();
StorageFile destinationFile;
destinationFile = await GetLocalFileFromName(localFilename);
var group = BackgroundTransferGroup.CreateGroup(Guid.NewGuid().ToString());
group.TransferBehavior = BackgroundTransferBehavior.Serialized;
BackgroundTransferCompletionGroup completionGroup = new BackgroundTransferCompletionGroup();
// this will cause the app to be activated when the download completes and
// CheckCompletionResult will be called for the final download state
RegisterBackgroundTask(completionGroup.Trigger);
BackgroundDownloader downloader = new BackgroundDownloader(completionGroup);
downloader.TransferGroup = group;
group.TransferBehavior = BackgroundTransferBehavior.Serialized;
CreateNotifications(downloader);
DownloadOperation download = downloader.CreateDownload(target, destinationFile);
download.Priority = priority;
completionGroup.Enable();
Progress<DownloadOperation> progressCallback = new Progress<DownloadOperation>(DownloadProgress);
var downloadTask = download.StartAsync().AsTask(progressCallback);
string tag = GetFileNameFromUri(target);
CreateToast(tag, localFilename);
try
{
await downloadTask;
// Will occur after download completes
ResponseInformation response = download.GetResponseInformation();
}
catch (Exception)
{
Debug.WriteLine("Download exception");
}
}
private static void DownloadProgress(DownloadOperation obj)
{
Debug.WriteLine(obj.Progress.ToString());
var progress = (double)obj.Progress.BytesReceived / (double)obj.Progress.TotalBytesToReceive;
string tag = GetFileNameFromUri(obj.RequestedUri);
UpdateToast(obj.ResultFile.Name, progress);
}
private static void CreateNotifications(BackgroundDownloader downloader)
{
var successToastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
successToastXml.GetElementsByTagName("text").Item(0).InnerText =
"Downloads completed successfully.";
ToastNotification successToast = new ToastNotification(successToastXml);
downloader.SuccessToastNotification = successToast;
var failureToastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
failureToastXml.GetElementsByTagName("text").Item(0).InnerText =
"At least one download completed with failure.";
ToastNotification failureToast = new ToastNotification(failureToastXml);
downloader.FailureToastNotification = failureToast;
}
private static void CreateToast(string title, string tag)
{
ToastContent toastContent = new ToastContent()
{
Visual = new ToastVisual()
{
BindingGeneric = new ToastBindingGeneric()
{
Children =
{
new AdaptiveText()
{
Text = "File downloading...",
},
new AdaptiveProgressBar()
{
Title = title,
Value = new BindableProgressBarValue("progressValue"),
ValueStringOverride = new BindableString("p"),
Status = "Downloading...",
},
},
},
},
};
var data = new Dictionary<string, string>
{
{ "progressValue", "0" },
{ "p", $"cool" }, // TODO: better than cool
};
// And create the toast notification
ToastNotification notification = new ToastNotification(toastContent.GetXml())
{
Tag = tag,
Data = new NotificationData(data),
};
// And then send the toast
ToastNotificationManager.CreateToastNotifier().Show(notification);
}
private static void UpdateToast(string toastTag, double progressValue)
{
var data = new Dictionary<string, string>
{
{ "progressValue", progressValue.ToString() },
{ "p", $"cool" }, // TODO: better than cool
};
try
{
_notifier.Update(new NotificationData(data), toastTag);
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
}
}
}