public async Task Start() { string fullPath = AddBackslash(_storageFolder.Path); fullPath += _loсalFile.Name; var request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Post, "https://graph.microsoft.com/v1.0/me" + fullPath + ":/createUploadSession"); request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _storageFolder.Account.Token); var httpClient = new System.Net.Http.HttpClient(); var response = await httpClient.SendAsync(request); if (response.StatusCode != HttpStatusCode.Created && response.StatusCode != HttpStatusCode.OK) { throw new InvalidOperationException("Session of upload did not create"); } DeserializedUploadSession deserializedUploadSession = new DeserializedUploadSession(); using (System.IO.Stream stream = await response.Content.ReadAsStreamAsync()) { DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedUploadSession.GetType()); deserializedUploadSession = ser.ReadObject(stream) as DeserializedUploadSession; if (deserializedUploadSession?.UploadUrl == null) { throw new NullReferenceException("Couldn't deserialized the data"); } } _uploadUrl = deserializedUploadSession.UploadUrl; await Run(); }
private async Task Run() { _cancellationToken = new CancellationTokenSource(); var request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, _uploadUrl); request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _storageFolder.Account.Token); var httpClient = new System.Net.Http.HttpClient(); var response = await httpClient.SendAsync(request); if (response.StatusCode != HttpStatusCode.Created && response.StatusCode != HttpStatusCode.OK) { throw new InvalidOperationException("Session of upload did not create"); } DeserializedUploadSession deserializedUploadSession = new DeserializedUploadSession(); using (System.IO.Stream stream = await response.Content.ReadAsStreamAsync()) { DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedUploadSession.GetType()); deserializedUploadSession = ser.ReadObject(stream) as DeserializedUploadSession; if (deserializedUploadSession?.NextExpectedRanges == null) { throw new NullReferenceException("Couldn't deserialized the data"); } } var fileStream = await _loсalFile.OpenReadAsync(); uint bytesCount = 13 * 320 * 1024; Byte[] bytes = new byte[bytesCount]; Regex regex = new Regex("[0-9]*"); var result = regex.Match(deserializedUploadSession.NextExpectedRanges[0]); _previewUploadBytes = UInt32.Parse(result.Value); fileStream.Seek(_previewUploadBytes); ulong offset = _previewUploadBytes; string token = _storageFolder.Account.Token; do { request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Put, _uploadUrl); request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); var reader = await fileStream.ReadAsync(bytes.AsBuffer(), bytesCount, InputStreamOptions.None); request.Content = new ByteArrayContent(reader.ToArray()); request.Content.Headers.ContentRange = new ContentRangeHeaderValue((long)offset, (long)offset + reader.Length - 1, (long)TotalBytesToTransfer); response = await httpClient.SendAsync(request); offset += reader.Length; } while (offset != TotalBytesToTransfer && !_cancellationToken.IsCancellationRequested); if (_cancellationToken.IsCancellationRequested) { StorageApplicationPermissions.FutureAccessList.Remove(_localFileToken); return; } DeserializedItem deserializedItem = new DeserializedItem(); using (System.IO.Stream stream = await response.Content.ReadAsStreamAsync()) { DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedItem.GetType()); deserializedItem = ser.ReadObject(stream) as DeserializedItem; if (deserializedItem?.File == null) { throw new NullReferenceException("Couldn't deserialized the data"); } } RemoteFile = new FileStorageFile(new FileBuilder(deserializedItem)) { Account = _storageFolder.Account }; StorageApplicationPermissions.FutureAccessList.Remove(_localFileToken); }