-
Notifications
You must be signed in to change notification settings - Fork 17
/
OneDriveManager.cs
274 lines (235 loc) · 12.1 KB
/
OneDriveManager.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
// ---------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// The MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE 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 SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// ---------------------------------------------------------------------------------
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.Web.Http;
namespace AnnotatedAudio.ViewModel
{
public class OneDriveManager
{
public string Token { get; set; }
public async Task<bool> DownloadFile(string fileName)
{
HttpResponseMessage httpResponse;
// make a URI with the download address of a file in the "AnnotatedAudio" folder
Uri targetLocation = new Uri($"https://api.onedrive.com/v1.0/drive/root:/AnnotatedAudio/{fileName}:/content");
using (var client = new HttpClient())
{
// the request headers belong to the HttpClient object
client.DefaultRequestHeaders.Add("Authorization", $"bearer {Token}");
// HTTP GET - this checks whether said file is available for download at said location.
httpResponse = await client.GetAsync(targetLocation);
}
// handle http response
if (httpResponse.StatusCode == HttpStatusCode.Found || httpResponse.IsSuccessStatusCode)
{
// the download location was confirmed: proceed with the download
string downloadString = httpResponse.Content.Headers["Content-Location"];
Uri downloadUri = new Uri(downloadString);
using (var client = new HttpClient())
{
// an auth token is not needed here.
// HTTP GET
httpResponse = await client.GetAsync(downloadUri);
}
IBuffer fileBuffer = await httpResponse.Content.ReadAsBufferAsync();
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
// write the bytes to the newly created file
await Task.Run(() => {
System.IO.File.WriteAllBytes(file.Path, fileBuffer.ToArray());
});
this.LogMessage(ToString(), "file downloaded successfully");
}
else
{
// not able to download anything from the given URI
this.LogMessage(ToString(), "download target failed: "+httpResponse.StatusCode);
}
return httpResponse.IsSuccessStatusCode;
}
public async Task<bool> DeleteFile(string fileName)
{
HttpResponseMessage httpResponse;
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", $"bearer {Token}");
httpResponse = await client.DeleteAsync(new Uri($"https://api.onedrive.com/v1.0/drive/root:/AnnotatedAudio/{fileName}"));
}
return httpResponse.IsSuccessStatusCode;
}
public async Task<bool> UploadFile(StorageFile file, Action<float> uploadProgress)
{
// process file
if (file == null) return false;
string fileName = file.Name;
HttpResponseMessage httpResponse;
byte[] byteArray;
ulong streamSize;
Uri uploadSessionRequestLocation = new Uri($"https://api.onedrive.com/v1.0/drive/root:/AnnotatedAudio/{fileName}:/upload.createSession");
// request upload session:
httpResponse = await requestUploadSessionAsync(Token, fileName, uploadSessionRequestLocation);
String httpResponseOutput = "";
if (httpResponse.IsSuccessStatusCode)
{
//operation was successful: print http status code
httpResponseOutput += $"http response: {(int)httpResponse.StatusCode}: {httpResponse.StatusCode}";
this.LogMessage(ToString(), httpResponseOutput);
// begin uploading fragments:
// get JSON response
JObject jsonResponse = JObject.Parse(httpResponse.Content.ReadAsStringAsync().GetResults());
// values to pull from JSON:
Uri uploadSessionUri = null;
DateTime expirationDT = new DateTime();
if (jsonResponse["uploadUrl"] != null && jsonResponse["expirationDateTime"] != null)
{
uploadSessionUri = new Uri(jsonResponse["uploadUrl"].Value<string>());
expirationDT = DateTime.Parse(jsonResponse["expirationDateTime"].Value<string>(), null, System.Globalization.DateTimeStyles.RoundtripKind);
}
else
{
this.LogMessage(ToString(), "upload failed: missing JSON data");
return false;
}
// make the PUT request
using (var client = new HttpClient())
{
// the request headers belong to the HttpClient object
client.DefaultRequestHeaders.Add("Authorization", $"bearer {Token}");
// write the file contents to a byte array
using (IRandomAccessStreamWithContentType stream = await file.OpenReadAsync())
{
streamSize = stream.Size;
byteArray = new byte[streamSize];
using (DataReader reader = new DataReader(stream))
{
await reader.LoadAsync((uint)streamSize);
reader.ReadBytes(byteArray);
}
}
// need to identify the content type for the HTTP PUT request
string contentTypeString;
switch (Path.GetExtension(fileName))
{
case ".mp4":
contentTypeString = "video/mp4";
break;
case ".json":
contentTypeString = "application/json";
break;
case ".gif":
contentTypeString = "image/gif";
break;
case ".zip":
contentTypeString = "application/zip";
break;
default:
contentTypeString = "application/json";
break;
}
// upload all bytes in the byte array, possibly in several discrete "chunks"
IHttpContent putContent;
int bytesRemaining = (int)streamSize;
int chunkSize;
byte[] byteArraySection;
int startIndex = 0;
int chunkNumber = 1;
const int chunkLimit = 62000000;
// as long as there is more data to upload:
while (bytesRemaining > 0)
{
// determine which section of entire byte array to upload:
chunkSize = Math.Min(chunkLimit, bytesRemaining);
byteArraySection = new byte[chunkSize];
Array.Copy(byteArray, startIndex, byteArraySection, 0, chunkSize);
// create an httpContent with a byte buffer
putContent = new HttpBufferContent(byteArraySection.AsBuffer());
// add headers that belong to the putContent (they describe the type and amount of data):
putContent.Headers["Content-Range"] = $"bytes {startIndex}-{startIndex + chunkSize - 1}/{streamSize}";
putContent.Headers["Content-Length"] = chunkSize.ToString();
putContent.Headers["Content-Type"] = contentTypeString;
// HTTP PUT
System.Diagnostics.Debug.WriteLine($"uploading bytes {startIndex} to {startIndex + chunkSize - 1}");
httpResponse = await client.PutAsync(uploadSessionUri, putContent);
this.LogMessage(ToString(), $"http response: {(int)httpResponse.StatusCode}: {httpResponse.StatusCode} (chunk {chunkNumber})");
// if successful, then update placeholders before continuing
startIndex += chunkSize;
bytesRemaining -= chunkSize;
chunkNumber++;
}
}
return true;
}
else
{
//http request returned failure: print http status code
httpResponseOutput += $"http response: {(int)httpResponse.StatusCode}: {httpResponse.StatusCode}";
// get JSON response
JObject jsonResponse = JObject.Parse(httpResponse.Content.ReadAsStringAsync().GetResults());
// check for an "error" property
JToken errorToken = jsonResponse["error"];
if (errorToken != null)
{
// check for an error "message" property
JToken causeToken = errorToken["message"];
if (causeToken != null)
{
// add in the extra information
httpResponseOutput += $"\n{causeToken.ToString()}";
}
}
// write error details
this.LogMessage(ToString(), httpResponseOutput);
return false;
}
}
// Make an HTTP POST that requests to open an upload session, for uploading a file.
private async Task<HttpResponseMessage> requestUploadSessionAsync(string token, string fileName, Uri uploadSessionLocation)
{
using (var client = new HttpClient())
{
// the request headers belong to the HttpClient
client.DefaultRequestHeaders.Add("Authorization", $"bearer {token}");
// the post content string
string jsonstr =
"{" +
"\"item\": {" +
"\"@name.conflictBehavior\": \"replace\"," +
$"\"name\": \"{fileName}\"" +
"}" +
"}";
var postContent = new HttpStringContent(jsonstr);
// other headers belong to the post content
postContent.Headers["Content-Type"] = "application/json";
// HTTP POST
return await client.PostAsync(uploadSessionLocation, postContent);
}
}
}
}