예제 #1
0
        public void CancelTransfer(ResumableUploadSession oneDriveSession)
        {
            var request = WebRequest.Create(oneDriveSession.Location);

            request.Method = "DELETE";
            using (request.GetResponse())
            {
            }
        }
예제 #2
0
        public void Transfer(ResumableUploadSession oneDriveSession, Stream stream, long chunkLength)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            if (oneDriveSession.Status != ResumableUploadSessionStatus.Started)
            {
                throw new InvalidOperationException("Can't upload chunk for given upload session.");
            }

            var request = WebRequest.Create(oneDriveSession.Location);

            request.Method        = "PUT";
            request.ContentLength = chunkLength;
            request.Headers.Add("Authorization", "Bearer " + AccessToken);
            request.Headers.Add("Content-Range", string.Format("bytes {0}-{1}/{2}",
                                                               oneDriveSession.BytesTransfered,
                                                               oneDriveSession.BytesTransfered + chunkLength - 1,
                                                               oneDriveSession.BytesToTransfer));

            using (var requestStream = request.GetRequestStream())
            {
                stream.CopyTo(requestStream);
            }

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                if (response.StatusCode != HttpStatusCode.Created && response.StatusCode != HttpStatusCode.OK)
                {
                    oneDriveSession.BytesTransfered += chunkLength;
                }
                else
                {
                    oneDriveSession.Status = ResumableUploadSessionStatus.Completed;

                    using (var responseStream = response.GetResponseStream())
                    {
                        if (responseStream == null)
                        {
                            return;
                        }
                        using (var readStream = new StreamReader(responseStream))
                        {
                            var responseString = readStream.ReadToEnd();
                            var responseJson   = JObject.Parse(responseString);

                            oneDriveSession.FileId = responseJson.Value <string>("id");
                        }
                    }
                }
            }
        }
예제 #3
0
        public ResumableUploadSession CreateResumableSession(Item onedriveFile, long contentLength)
        {
            if (onedriveFile == null)
            {
                throw new ArgumentNullException("onedriveFile");
            }

            var folderId = onedriveFile.ParentReference.Id;
            var fileName = onedriveFile.Name;

            var uploadUriBuilder = new UriBuilder(OneDriveLoginProvider.OneDriveApiUrl)
            {
                Path = "/" + ApiVersion + "/drive/items/" + folderId + ":/" + fileName + ":/oneDrive.createUploadSession"
            };

            var request = WebRequest.Create(uploadUriBuilder.Uri);

            request.Method        = "POST";
            request.ContentLength = 0;

            request.ContentType = "application/json; charset=UTF-8";
            request.Headers.Add("Authorization", "Bearer " + AccessToken);

            var uploadSession = new ResumableUploadSession(onedriveFile.Id, folderId, contentLength);

            using (var response = request.GetResponse())
                using (var responseStream = response.GetResponseStream())
                {
                    if (responseStream != null)
                    {
                        using (var readStream = new StreamReader(responseStream))
                        {
                            var responseString = readStream.ReadToEnd();
                            var responseJson   = JObject.Parse(responseString);
                            uploadSession.Location = responseJson.Value <string>("uploadUrl");
                        }
                    }
                }

            uploadSession.Status = ResumableUploadSessionStatus.Started;

            return(uploadSession);
        }