/* * Greenshot - a free and open source screenshot tool * Copyright (C) 2007-2015 Thomas Braun, Jens Klingen, Robin Krom, Francis Noel * * For more information see: http://getgreenshot.org/ * The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/ * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 1 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ using Greenshot.IniFile; using GreenshotPlugin.Core; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Runtime.Serialization.Json; using System.Text; namespace GreenshotBoxPlugin { /// /// Description of ImgurUtils. /// public static class BoxUtils { private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(BoxUtils)); private static readonly BoxConfiguration Config = IniConfig.GetIniSection(); private const string UploadFileUri = "https://upload.box.com/api/2.0/files/content"; private const string FilesUri = "https://www.box.com/api/2.0/files/{0}"; /// /// Put string /// /// /// /// OAuth2Settings /// response public static string HttpPut(string url, string content, OAuth2Settings settings) { var webRequest= OAuth2Helper.CreateOAuth2WebRequest(HTTPMethod.PUT, url, settings); byte[] data = Encoding.UTF8.GetBytes(content); using (var requestStream = webRequest.GetRequestStream()) { requestStream.Write(data, 0, data.Length); } return NetworkHelper.GetResponseAsString(webRequest); } /// /// Do the actual upload to Box /// For more details on the available parameters, see: http://developers.box.net/w/page/12923951/ApiFunction_Upload%20and%20Download /// /// Image for box upload /// Title of box upload /// Filename of box upload /// url to uploaded image public static string UploadToBox(SurfaceContainer image, string title, string filename) { // Fill the OAuth2Settings OAuth2Settings settings = new OAuth2Settings(); settings.AuthUrlPattern = "https://www.box.com/api/oauth2/authorize?client_id={ClientId}&response_type={response_type}&state{State}&redirect_uri={RedirectUrl}"; settings.TokenUrlPattern = "https://www.box.com/api/oauth2/token"; settings.CloudServiceName = "Box"; settings.ClientId = BoxCredentials.ClientId; settings.ClientSecret = BoxCredentials.ClientSecret; settings.RedirectUrl = "https://www.box.com/home/"; settings.BrowserSize = new Size(1060, 600); settings.AuthorizeMode = OAuth2AuthorizeMode.EmbeddedBrowser; // Copy the settings from the config, which is kept in memory and on the disk settings.RefreshToken = Config.RefreshToken; settings.AccessToken = Config.AccessToken; settings.AccessTokenExpires = Config.AccessTokenExpires; try { var webRequest = OAuth2Helper.CreateOAuth2WebRequest(HTTPMethod.POST, FilesUri, settings); IDictionary parameters = new Dictionary(); if (Config.AddFilename) { parameters.Add("filename", image); } parameters.Add("parent_id", Config.FolderId); NetworkHelper.WriteMultipartFormData(webRequest, parameters); var response = NetworkHelper.GetResponseAsString(webRequest); LOG.DebugFormat("Box response: {0}", response); var upload = JsonSerializer.Deserialize(response); if (upload == null || upload.Entries == null || upload.Entries.Count == 0) return null; if (Config.UseSharedLink) { string filesResponse = HttpPut(string.Format(FilesUri, upload.Entries[0].Id), "{\"shared_link\": {\"access\": \"open\"}}", settings); var file = JsonSerializer.Deserialize(filesResponse); return file.SharedLink.Url; } return string.Format("http://www.box.com/files/0/f/0/1/f_{0}", upload.Entries[0].Id); } finally { // Copy the settings back to the config, so they are stored. Config.RefreshToken = settings.RefreshToken; Config.AccessToken = settings.AccessToken; Config.AccessTokenExpires = settings.AccessTokenExpires; Config.IsDirty = true; } } } /// /// A simple helper class for the DataContractJsonSerializer /// internal static class JsonSerializer { /// /// Helper method to serialize object to JSON /// /// JSON object /// string public static string Serialize(object jsonObject) { var serializer = new DataContractJsonSerializer(jsonObject.GetType()); using (MemoryStream stream = new MemoryStream()) { serializer.WriteObject(stream, jsonObject); return Encoding.UTF8.GetString(stream.ToArray()); } } /// /// Helper method to parse JSON to object /// /// /// /// public static T Deserialize(string jsonString) { var deserializer = new DataContractJsonSerializer(typeof(T)); using (MemoryStream stream = new MemoryStream()) { byte[] content = Encoding.UTF8.GetBytes(jsonString); stream.Write(content, 0, content.Length); stream.Seek(0, SeekOrigin.Begin); return (T)deserializer.ReadObject(stream); } } } }