/// <summary> /// Do the actual upload to OneDrive /// </summary> /// <param name="oAuth2Settings">OAuth2Settings</param> /// <param name="surfaceToUpload">ISurface to upload</param> /// <param name="progress">IProgress</param> /// <param name="token">CancellationToken</param> /// <returns>OneDriveUploadResponse with details</returns> private async Task <OneDriveUploadResponse> UploadToOneDriveAsync(OAuth2Settings oAuth2Settings, ISurface surfaceToUpload, IProgress <int> progress = null, CancellationToken token = default) { var filename = surfaceToUpload.GenerateFilename(CoreConfiguration, _oneDriveConfiguration); var uploadUri = OneDriveUri.AppendSegments("root:", "Screenshots", filename + ":", "content"); var localBehaviour = _oneDriveHttpBehaviour.ShallowClone(); if (progress != null) { localBehaviour.UploadProgress = percent => { UiContext.RunOn(() => progress.Report((int)(percent * 100)), token); }; } var oauthHttpBehaviour = OAuth2HttpBehaviourFactory.Create(oAuth2Settings, localBehaviour); using (var imageStream = new MemoryStream()) { surfaceToUpload.WriteToStream(imageStream, CoreConfiguration, _oneDriveConfiguration); imageStream.Position = 0; using (var content = new StreamContent(imageStream)) { content.Headers.Add("Content-Type", surfaceToUpload.GenerateMimeType(CoreConfiguration, _oneDriveConfiguration)); oauthHttpBehaviour.MakeCurrent(); return(await uploadUri.PutAsync <OneDriveUploadResponse>(content, token)); } } }
/// <summary> /// Do the actual upload to OneDrive /// </summary> /// <param name="oAuth2Settings">OAuth2Settings</param> /// <param name="surfaceToUpload">ISurface to upload</param> /// <param name="outputSettings">OutputSettings for the image file format</param> /// <param name="title">Title</param> /// <param name="filename">Filename</param> /// <param name="progress">IProgress</param> /// <param name="token">CancellationToken</param> /// <returns>ImgurInfo with details</returns> public static async Task <OneDriveUploadResponse> UploadToOneDriveAsync(OAuth2Settings oAuth2Settings, ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, string title, string filename, IProgress <int> progress = null, CancellationToken token = default) { var uploadUri = new Uri(UrlUtils.GetUploadUrl(filename)); var localBehaviour = Behaviour.ShallowClone(); if (progress != null) { localBehaviour.UploadProgress = percent => { UiContext.RunOn(() => progress.Report((int)(percent * 100)), token); }; } var oauthHttpBehaviour = OAuth2HttpBehaviourFactory.Create(oAuth2Settings, localBehaviour); using (var imageStream = new MemoryStream()) { ImageOutput.SaveToStream(surfaceToUpload, imageStream, outputSettings); imageStream.Position = 0; using (var content = new StreamContent(imageStream)) { content.Headers.Add("Content-Type", "image/" + outputSettings.Format); oauthHttpBehaviour.MakeCurrent(); return(await uploadUri.PostAsync <OneDriveUploadResponse>(content, token)); } } }
private static async Task <ImgurImage> AnnonymousUploadToImgurAsync(ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, IDictionary <string, string> otherParameters, IProgress <int> progress = null, CancellationToken token = default) { var uploadUri = new Uri(_imgurConfiguration.ApiUrl).AppendSegments("upload.json").ExtendQuery(otherParameters); var localBehaviour = Behaviour.ShallowClone(); if (progress != null) { localBehaviour.UploadProgress = percent => { UiContext.RunOn(() => progress.Report((int)(percent * 100)), token); }; } using (var imageStream = new MemoryStream()) { ImageOutput.SaveToStream(surfaceToUpload, imageStream, outputSettings); imageStream.Position = 0; using (var content = new StreamContent(imageStream)) { content.Headers.Add("Content-Type", "image/" + outputSettings.Format); localBehaviour.MakeCurrent(); return(await uploadUri.PostAsync <ImgurImage>(content, token).ConfigureAwait(false)); } } }