Ejemplo n.º 1
0
 public Downloader(Windows.Storage.IStorageFile loсalFile, OneDrive.IFileStorageFile file)
 {
     RemoteFile           = file;
     _loсalFile           = loсalFile;
     TotalBytesToTransfer = RemoteFile.Size;
     _localFileToken      = StorageApplicationPermissions.FutureAccessList.Add(loсalFile);
 }
Ejemplo n.º 2
0
        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);
        }