-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathUrlSessionDownloadDelegate.cs
133 lines (111 loc) · 4.69 KB
/
UrlSessionDownloadDelegate.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
using System.Linq;
using Foundation;
using Plugin.DownloadManager.Abstractions;
namespace Plugin.DownloadManager
{
public class UrlSessionDownloadDelegate : NSUrlSessionDownloadDelegate
{
public DownloadManagerImplementation Controller;
protected DownloadFileImplementation GetDownloadFileByTask (NSUrlSessionTask downloadTask)
{
return Controller.Queue
.Cast<DownloadFileImplementation> ()
.FirstOrDefault (
i => i.Task != null &&
(int)i.Task.TaskIdentifier == (int)downloadTask.TaskIdentifier
);
}
/**
* A Task was resumed (or started ..)
*/
public override void DidResume (NSUrlSession session, NSUrlSessionDownloadTask downloadTask, long resumeFileOffset, long expectedTotalBytes)
{
var file = GetDownloadFileByTask (downloadTask);
if (file == null) {
downloadTask.Cancel ();
return;
}
file.Status = DownloadFileStatus.RUNNING;
}
public override void DidCompleteWithError (NSUrlSession session, NSUrlSessionTask task, NSError error)
{
var file = GetDownloadFileByTask (task);
if (file == null)
return;
file.Status = DownloadFileStatus.FAILED;
file.StatusDetails = error.LocalizedDescription;
Controller.RemoveFile (file);
}
/**
* The Task keeps receiving data. Keep track of the current progress ...
*/
public override void DidWriteData (NSUrlSession session, NSUrlSessionDownloadTask downloadTask, long bytesWritten, long totalBytesWritten, long totalBytesExpectedToWrite)
{
var file = GetDownloadFileByTask (downloadTask);
if (file == null) {
downloadTask.Cancel ();
return;
}
file.Status = DownloadFileStatus.RUNNING;
file.TotalBytesExpected = totalBytesExpectedToWrite;
file.TotalBytesWritten = totalBytesWritten;
}
public override void DidFinishDownloading (NSUrlSession session, NSUrlSessionDownloadTask downloadTask, NSUrl location)
{
var file = GetDownloadFileByTask (downloadTask);
if (file == null) {
downloadTask.Cancel ();
return;
}
// On iOS 9 and later, this method is called even so the response-code is 400 or higher. See https://github.com/cocos2d/cocos2d-x/pull/14683
var response = downloadTask.Response as NSHttpUrlResponse;
if (response != null && response.StatusCode >= 400) {
file.Status = DownloadFileStatus.FAILED;
file.StatusDetails = "Error.HttpCode: " + response.StatusCode;
Controller.RemoveFile (file);
return;
}
var success = true;
var destinationPathName = Controller.PathNameForDownloadedFile?.Invoke (file);
if (destinationPathName != null) {
success = MoveDownloadedFile (file, location, destinationPathName);
file.DestinationPathName = destinationPathName;
}
else
{
file.DestinationPathName = location.ToString();
}
// If the file destination is unknown or was moved successfully ...
if (success) {
file.Status = DownloadFileStatus.COMPLETED;
}
Controller.RemoveFile (file);
}
/**
* Move the downloaded file to it's destination
*/
public bool MoveDownloadedFile (DownloadFileImplementation file, NSUrl location, string destinationPathName)
{
var fileManager = NSFileManager.DefaultManager;
var destinationUrl = new NSUrl (destinationPathName, false);
NSError removeCopy;
NSError errorCopy;
fileManager.Remove (destinationUrl, out removeCopy);
var success = fileManager.Copy (location, destinationUrl, out errorCopy);
if (!success) {
file.StatusDetails = errorCopy.LocalizedDescription;
file.Status = DownloadFileStatus.FAILED;
}
return success;
}
public override void DidFinishEventsForBackgroundSession(NSUrlSession session)
{
var handler = CrossDownloadManager.BackgroundSessionCompletionHandler;
if (handler != null)
{
CrossDownloadManager.BackgroundSessionCompletionHandler = null;
handler();
}
}
}
}