Beispiel #1
0
 public LargeFileUploader(ODConnection connection, ODUploadSession uploadSession, Stream dataSource, ItemUploadOptions options)
 {
     Connection    = connection;
     UploadSession = uploadSession;
     DataSource    = dataSource;
     UploadOptions = options;
 }
        private async Task <ODItem> UploadToUrl(Stream sourceFileStream, ItemUploadOptions options, long localItemSize, Uri serviceUri)
        {
            var request = await CreateHttpRequestAsync(serviceUri, ApiConstants.HttpPut);

            request.ContentType = ApiConstants.ContentTypeBinary;

            options.ModifyRequest(request);

            var requestStream = await request.GetRequestStreamAsync();

            await sourceFileStream.CopyWithProgressAsync(requestStream, options.ProgressReporter, localItemSize);

            return(await GetResponseDataModel <ODItem>(request));
        }
        private async Task <ODItem> StartLargeFileTransfer(Uri createSessionUri, Stream sourceFileStream, ItemUploadOptions options)
        {
            var request = await CreateHttpRequestAsync(createSessionUri, ApiConstants.HttpPost);

            //object requestBody = new { item = new ODUploadSessionDescriptor { FilenameConflictBehavior = options.NameConflict } };
            //await SerializeObjectToRequestBody(requestBody, request);
            var response = await DataModelForRequest <ODUploadSession>(request);

            var uploader = new LargeFileUploader(this, response, sourceFileStream, options);
            var item     = await uploader.UploadFileStream();

            return(item);
        }
        /// <summary>
        /// Uploads a file using the resumable fragment upload API, splitting the file into smaller pieces to upload.
        ///
        /// You can use this method to replace an existing item (using itemReference.Id) or to upload a new item
        /// (using itemReference.Path).
        /// </summary>
        /// <param name="itemReference"></param>
        /// <param name="sourceFileStream"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public async Task <ODItem> UploadLargeFileAsync(ODItemReference itemReference, Stream sourceFileStream, ItemUploadOptions options)
        {
            if (!itemReference.IsValid())
            {
                throw new ArgumentException("parentItemReference isn't valid");
            }
            if (null == sourceFileStream)
            {
                throw new ArgumentNullException("sourceFileStream");
            }
            if (null == options)
            {
                throw new ArgumentNullException("options");
            }

            Uri serviceUri = UriForItemReference(itemReference, ApiConstants.UploadCreateSessionAction);

            return(await StartLargeFileTransfer(serviceUri, sourceFileStream, options));
        }
        /// <summary>
        /// Uploads a file using the resumable fragment upload API, splitting the file into smaller pieces to upload.
        /// </summary>
        /// <param name="parentItemReference"></param>
        /// <param name="filename"></param>
        /// <param name="sourceFileStream"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public async Task <ODItem> UploadLargeFileAsync(ODItemReference parentItemReference, string filename, Stream sourceFileStream, ItemUploadOptions options)
        {
            if (!parentItemReference.IsValid())
            {
                throw new ArgumentException("parentItemReference isn't valid");
            }
            if (string.IsNullOrEmpty(filename))
            {
                throw new ArgumentNullException("filename");
            }
            if (!filename.ValidFilename())
            {
                throw new ArgumentException("filename contains invalid characters");
            }
            if (null == sourceFileStream)
            {
                throw new ArgumentNullException("sourceFileStream");
            }
            if (null == options)
            {
                throw new ArgumentNullException("options");
            }

            string navigation = string.Concat(":/", filename, ":/", ApiConstants.UploadCreateSessionAction);
            Uri    serviceUri = UriForItemReference(parentItemReference, navigation);

            return(await StartLargeFileTransfer(serviceUri, sourceFileStream, options));
        }
        /// <summary>
        /// Upload a new file to a parent folder item.
        /// </summary>
        /// <param name="parentItemReference">Item ID for the parent folder.</param>
        /// <param name="filename">Filename of the new file to create</param>
        /// <param name="sourceFileStream">Data stream for the new file</param>
        /// <param name="options">Upload options</param>
        /// <returns></returns>
        public async Task <ODItem> PutNewFileToParentItemAsync(ODItemReference parentItemReference, string filename, Stream sourceFileStream, ItemUploadOptions options)
        {
            if (!filename.ValidFilename())
            {
                throw new ArgumentException("Filename contains invalid characters.");
            }
            if (!parentItemReference.IsValid())
            {
                throw new ArgumentException("parentItemReference was invalid. Requires either an ID or Path");
            }
            if (null == options)
            {
                throw new ArgumentNullException("options");
            }
            if (null == sourceFileStream)
            {
                throw new ArgumentNullException("sourceFileStream");
            }
            if (sourceFileStream.Length > MaxRegularUploadSize)
            {
                throw new ODException("Stream is longer than the maximum upload size allowed.");
            }

            long localItemSize;

            if (!sourceFileStream.TryGetLength(out localItemSize))
            {
                System.Diagnostics.Debug.WriteLine("Warning: Couldn't determine length of sourceFileStream.");
            }

            string navigationValue = string.Concat(ApiConstants.ChildrenRelationshipName, UrlSeperator, filename, UrlSeperator, ApiConstants.ContentRelationshipName);
            Uri    serviceUri      = UriForItemReference(parentItemReference, navigationValue);

            await CreateHttpRequestAsync(serviceUri, ApiConstants.HttpPut);

            return(await UploadToUrl(sourceFileStream, options, localItemSize, serviceUri));
        }
        /// <summary>
        /// Uploads the contents of sourceFileStream to the service as the contents for itemId. This method can
        /// be used to replace the contents of an existing file or create a new file if the itemReference property
        /// is set to a path-only.
        /// </summary>
        /// <param name="itemReference"></param>
        /// <param name="sourceFileStream"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public async Task <ODItem> PutContentsAsync(ODItemReference itemReference, Stream sourceFileStream, ItemUploadOptions options)
        {
            if (!itemReference.IsValid())
            {
                throw new ArgumentException("ItemReference was invalid. Requires either an ID or Path");
            }
            if (null == options)
            {
                throw new ArgumentNullException("options");
            }
            if (null == sourceFileStream)
            {
                throw new ArgumentNullException("sourceFileStream");
            }
            if (sourceFileStream.Length > MaxRegularUploadSize)
            {
                throw new ODException("Stream is longer than the maximum upload size allowed.");
            }

            long localItemSize;

            if (!sourceFileStream.TryGetLength(out localItemSize))
            {
                throw new ODException("Couldn't get length of sourceFileStream.");
            }

            Uri serviceUri = UriForItemReference(itemReference);

            return(await UploadToUrl(sourceFileStream, options, localItemSize, serviceUri));
        }