/// <summary> /// Performs the BackgroundUploadOperation. /// </summary> public async Task <LiveOperationResult> ExecuteAsync() { Debug.Assert(this.status != OperationStatus.Completed, "Cannot execute on a completed operation."); if (this.status == OperationStatus.Cancelled) { return(await this.tcs.Task); } this.status = OperationStatus.Started; string filename = Path.GetFileName(this.uploadLocationOnDevice.OriginalString); Debug.Assert(!string.IsNullOrEmpty(filename)); Uri requestUri = this.client.GetResourceUri(this.path, ApiMethod.Upload); // TODO: Figure out how to mock out GetUploadLinkOperation elegantly so this can be tested. this.getUploadLinkOperation = new GetUploadLinkOperation( this.client, requestUri, filename, this.overwriteOption, null); LiveOperationResult uploadLinkResult = await this.getUploadLinkOperation.ExecuteAsync(); var uploadRequestUri = new Uri(uploadLinkResult.RawResult, UriKind.Absolute); var downloadLocationOnDevice = new Uri(this.uploadLocationOnDevice.OriginalString + ".json", UriKind.RelativeOrAbsolute); var builder = new BackgroundUploadRequestBuilder { RequestUri = uploadRequestUri, AccessToken = this.client.Session.AccessToken, UploadLocationOnDevice = this.uploadLocationOnDevice, DownloadLocationOnDevice = downloadLocationOnDevice, TransferPreferences = this.transferPreferences }; this.request = builder.Build(); var eventAdapter = new BackgroundUploadEventAdapter(this.backgroundTransferService, this.tcs); Task <LiveOperationResult> task = progress == null? eventAdapter.ConvertTransferStatusChangedToTask(this.request) : eventAdapter.ConvertTransferStatusChangedToTask(this.request, progress); Debug.Assert(this.tcs.Task == task, "EventAdapter returned a different task. This could affect cancel."); if (this.status != OperationStatus.Cancelled) { this.backgroundTransferService.Add(this.request); this.requestAddedToService = true; } LiveOperationResult result = await task; this.status = OperationStatus.Completed; return(result); }
/// <summary> /// Attaches to the upload operation and receive the result of the operation when it is finished. /// </summary> /// <param name="ct">a token that is used to cancel the background upload operation.</param> /// <param name="progress">an object that is called to report the background upload's progress.</param> /// <returns>A Task object representing the asynchronous operation.</returns> public Task <LiveOperationResult> AttachAsync(CancellationToken ct, IProgress <LiveOperationProgress> progress) { var tcs = new TaskCompletionSource <LiveOperationResult>(); ct.Register(() => { // Remove from the service to cancel. this.backgroundTransferService.Remove(this.request); tcs.TrySetCanceled(); }); var uploadEventHandler = new BackgroundUploadEventAdapter(this.backgroundTransferService, tcs); return(uploadEventHandler.ConvertTransferStatusChangedToTask(this.request, progress)); }
/// <summary> /// Performs the BackgroundUploadOperation. /// </summary> public async Task<LiveOperationResult> ExecuteAsync() { Debug.Assert(this.status != OperationStatus.Completed, "Cannot execute on a completed operation."); if (this.status == OperationStatus.Cancelled) { return await this.tcs.Task; } this.status = OperationStatus.Started; string filename = Path.GetFileName(this.uploadLocationOnDevice.OriginalString); Debug.Assert(!string.IsNullOrEmpty(filename)); Uri requestUri = this.client.GetResourceUri(this.path, ApiMethod.Upload); // TODO: Figure out how to mock out GetUploadLinkOperation elegantly so this can be tested. this.getUploadLinkOperation = new GetUploadLinkOperation( this.client, requestUri, filename, this.overwriteOption, null); LiveOperationResult uploadLinkResult = await this.getUploadLinkOperation.ExecuteAsync(); var uploadRequestUri = new Uri(uploadLinkResult.RawResult, UriKind.Absolute); var downloadLocationOnDevice = new Uri(this.uploadLocationOnDevice.OriginalString + ".json", UriKind.RelativeOrAbsolute); var builder = new BackgroundUploadRequestBuilder { RequestUri = uploadRequestUri, AccessToken = (this.client.Session != null) ? this.client.Session.AccessToken : "", UploadLocationOnDevice = this.uploadLocationOnDevice, DownloadLocationOnDevice = downloadLocationOnDevice, TransferPreferences = this.transferPreferences }; this.request = builder.Build(); var eventAdapter = new BackgroundUploadEventAdapter(this.backgroundTransferService, this.tcs); Task<LiveOperationResult> task = progress == null ? eventAdapter.ConvertTransferStatusChangedToTask(this.request) : eventAdapter.ConvertTransferStatusChangedToTask(this.request, progress); Debug.Assert(this.tcs.Task == task, "EventAdapter returned a different task. This could affect cancel."); if (this.status != OperationStatus.Cancelled) { this.backgroundTransferService.Add(this.request); this.requestAddedToService = true; } LiveOperationResult result = await task; this.status = OperationStatus.Completed; return result; }