/// <summary> /// Parse the upload response /// </summary> /// <param name="response">XML</param> /// <returns>PhotobucketInfo object</returns> public static PhotobucketInfo FromUploadResponse(string response) { Log.Debug().WriteLine(response); var photobucketInfo = new PhotobucketInfo(); try { var doc = new XmlDocument(); doc.LoadXml(response); var nodes = doc.GetElementsByTagName("url"); if (nodes.Count > 0) { photobucketInfo.Original = nodes.Item(0)?.InnerText; } nodes = doc.GetElementsByTagName("browseurl"); if (nodes.Count > 0) { photobucketInfo.Page = nodes.Item(0)?.InnerText; } nodes = doc.GetElementsByTagName("thumb"); if (nodes.Count > 0) { photobucketInfo.Thumbnail = nodes.Item(0)?.InnerText; } } catch (Exception e) { Log.Error().WriteLine("Could not parse Photobucket response due to error {0}, response was: {1}", e.Message, response); } return(photobucketInfo); }
/// <summary> /// Do the actual upload to Photobucket /// For more details on the available parameters, see: http://api.Photobucket.com/resources_anon /// </summary> /// <returns>PhotobucketResponse</returns> public async Task <PhotobucketInfo> UploadToPhotobucket(ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, string albumPath, string title, string filename, IProgress <int> progress = null, CancellationToken token = default) { string responseString; var oAuthHttpBehaviour = _oAuthHttpBehaviour.ShallowClone(); // Use UploadProgress if (progress != null) { oAuthHttpBehaviour.UploadProgress = percent => { UiContext.RunOn(() => progress.Report((int)(percent * 100)), token); }; } _oAuthHttpBehaviour.MakeCurrent(); if (_photobucketConfiguration.Username == null || _photobucketConfiguration.SubDomain == null) { await PhotobucketApiUri.AppendSegments("users").ExtendQuery("format", "json").GetAsAsync <object>(token); } if (_photobucketConfiguration.Album == null) { _photobucketConfiguration.Album = _photobucketConfiguration.Username; } var uploadUri = PhotobucketApiUri.AppendSegments("album", _photobucketConfiguration.Album, "upload"); var signedParameters = new Dictionary <string, object> { { "type", "image" } }; // add type // add title if (title != null) { signedParameters.Add("title", title); } // add filename if (filename != null) { signedParameters.Add("filename", filename); } // Add image using (var imageStream = new MemoryStream()) { ImageOutput.SaveToStream(surfaceToUpload, imageStream, outputSettings); imageStream.Position = 0; using (var streamContent = new StreamContent(imageStream)) { streamContent.Headers.ContentType = new MediaTypeHeaderValue("image/" + outputSettings.Format); streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") { Name = "\"uploadfile\"", FileName = "\"" + filename + "\"" }; HttpBehaviour.Current.SetConfig(new HttpRequestMessageConfiguration { Properties = signedParameters }); try { responseString = await uploadUri.PostAsync <string>(streamContent, token); } catch (Exception ex) { Log.Error().WriteLine(ex, "Error uploading to Photobucket."); throw; } } } if (responseString == null) { return(null); } Log.Info().WriteLine(responseString); var photobucketInfo = PhotobucketInfo.FromUploadResponse(responseString); Log.Debug().WriteLine("Upload to Photobucket was finished"); return(photobucketInfo); }