/* * Greenshot - a free and open source screenshot tool * Copyright (C) 2007-2013 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 System; using System.Collections.Generic; using System.Drawing; using System.Net; using System.Text; using Greenshot.IniFile; using GreenshotPlugin.Controls; using GreenshotPlugin.Core; using System.Runtime.Serialization.Json; using System.IO; 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 RedirectUri = "https://app.box.com/home/"; private const string UploadFileUri = "https://upload.box.com/api/2.0/files/content"; private const string AuthorizeUri = "https://www.box.com/api/oauth2/authorize"; private const string TokenUri = "https://www.box.com/api/oauth2/token"; private const string FilesUri = "https://www.box.com/api/2.0/files/{0}"; private static bool Authorize() { string authorizeUrl = string.Format("{0}?client_id={1}&response_type=code&state=dropboxplugin&redirect_uri={2}", AuthorizeUri, BoxCredentials.ClientId, RedirectUri); OAuthLoginForm loginForm = new OAuthLoginForm("Box Authorize", new Size(1060, 600), authorizeUrl, RedirectUri); loginForm.ShowDialog(); if (!loginForm.isOk) { return false; } var callbackParameters = loginForm.CallbackParameters; if (callbackParameters == null || !callbackParameters.ContainsKey("code")) { return false; } string authorizationResponse = PostAndReturn(new Uri(TokenUri), string.Format("grant_type=authorization_code&code={0}&client_id={1}&client_secret={2}", callbackParameters["code"], BoxCredentials.ClientId, BoxCredentials.ClientSecret)); var authorization = JSONSerializer.Deserialize(authorizationResponse); Config.BoxToken = authorization.AccessToken; IniConfig.Save(); return true; } /// /// Download a url response as string /// /// An Uri to specify the download location /// string with the file content public static string PostAndReturn(Uri url, string postMessage) { HttpWebRequest webRequest = (HttpWebRequest)NetworkHelper.CreateWebRequest(url); webRequest.Method = "POST"; webRequest.KeepAlive = true; webRequest.Credentials = System.Net.CredentialCache.DefaultCredentials; webRequest.ContentType = "application/x-www-form-urlencoded"; byte[] data = Encoding.UTF8.GetBytes(postMessage.ToString()); using (var requestStream = webRequest.GetRequestStream()) { requestStream.Write(data, 0, data.Length); } return NetworkHelper.GetResponse(webRequest); } /// /// Upload parameters by post /// /// /// /// response public static string HttpPost(string url, IDictionary parameters) { var webRequest = (HttpWebRequest)NetworkHelper.CreateWebRequest(url); webRequest.Method = "POST"; webRequest.KeepAlive = true; webRequest.Credentials = CredentialCache.DefaultCredentials; webRequest.Headers.Add("Authorization", "Bearer " + Config.BoxToken); NetworkHelper.WriteMultipartFormData(webRequest, parameters); return NetworkHelper.GetResponse(webRequest); } /// /// Upload file by PUT /// /// /// /// response public static string HttpPut(string url, string content) { var webRequest = (HttpWebRequest)NetworkHelper.CreateWebRequest(url); webRequest.Method = "PUT"; webRequest.KeepAlive = true; webRequest.Credentials = CredentialCache.DefaultCredentials; webRequest.Headers.Add("Authorization", "Bearer " + Config.BoxToken); byte[] data = Encoding.UTF8.GetBytes(content); using (var requestStream = webRequest.GetRequestStream()) { requestStream.Write(data, 0, data.Length); } return NetworkHelper.GetResponse(webRequest); } /// /// Get REST request /// /// /// response public static string HttpGet(string url) { var webRequest = (HttpWebRequest)NetworkHelper.CreateWebRequest(url); webRequest.Method = "GET"; webRequest.KeepAlive = true; webRequest.Credentials = CredentialCache.DefaultCredentials; webRequest.Headers.Add("Authorization", "Bearer " + Config.BoxToken); return NetworkHelper.GetResponse(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) { while (true) { const string folderId = "0"; if (string.IsNullOrEmpty(Config.BoxToken)) { if (!Authorize()) { return null; } } IDictionary parameters = new Dictionary(); parameters.Add("filename", image); parameters.Add("parent_id", folderId); var response = ""; try { response = HttpPost(UploadFileUri, parameters); } catch (WebException ex) { if (ex.Status == WebExceptionStatus.ProtocolError) { Config.BoxToken = null; continue; } } LOG.DebugFormat("Box response: {0}", response); // Check if the token is wrong if ("wrong auth token".Equals(response)) { Config.BoxToken = null; IniConfig.Save(); continue; } 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\"}}"); 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); } } } /// /// A simple helper class for the DataContractJsonSerializer /// public 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); } } /// /// Helper method to parse JSON to object /// /// /// /// public static T DeserializeWithDirectory(string jsonString) { DataContractJsonSerializerSettings settings = new DataContractJsonSerializerSettings(); settings.UseSimpleDictionaryFormat = true; var deserializer = new DataContractJsonSerializer(typeof(T), settings); 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); } } } }