Creating a branch 1.1 where I will try to make the 1.1.7 build available, this means I need to merge some changes from 2.0 to here.

This commit is contained in:
RKrom 2013-12-04 17:46:02 +01:00
commit a03bc31aef
247 changed files with 6986 additions and 8233 deletions

View file

@ -37,6 +37,9 @@ namespace GreenshotBoxPlugin {
[IniProperty("AfterUploadLinkToClipBoard", Description = "After upload send Box link to clipboard.", DefaultValue = "true")]
public bool AfterUploadLinkToClipBoard;
[IniProperty("UseSharedLink", Description = "Use the shared link, instead of the private, on the clipboard", DefaultValue = "True")]
public bool UseSharedLink;
[IniProperty("BoxToken", Description = "Token.", DefaultValue = "")]
public string BoxToken;

View file

@ -26,6 +26,7 @@ namespace GreenshotBoxPlugin {
/// Copy this file to BoxCredentials.private.cs and fill in valid credentials. (Or empty strings, but of course you won't be able to use the plugin then.)
/// </summary>
public static class BoxCredentials {
public static string API_KEY = empty;
public static string ClientId = string.Empty;
public static string ClientSecret = string.Empty;
}
}

View file

@ -0,0 +1,59 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace GreenshotBoxPlugin {
[DataContract]
public class Authorization {
[DataMember(Name = "access_token")]
public string AccessToken { get; set; }
[DataMember(Name = "expires_in")]
public int ExpiresIn { get; set; }
[DataMember(Name = "refresh_token")]
public string RefreshToken { get; set; }
[DataMember(Name = "token_type")]
public string TokenType { get; set; }
}
[DataContract]
public class SharedLink {
[DataMember(Name = "url")]
public string Url { get; set; }
[DataMember(Name = "download_url")]
public string DownloadUrl { get; set; }
}
[DataContract]
public class FileEntry {
[DataMember(Name = "id")]
public string Id { get; set; }
[DataMember(Name = "name")]
public string Name { get; set; }
[DataMember(Name = "shared_link")]
public SharedLink SharedLink { get; set; }
}
[DataContract]
public class Upload {
[DataMember(Name = "entries")]
public List<FileEntry> Entries { get; set; }
}
}

View file

@ -144,7 +144,7 @@ namespace GreenshotBoxPlugin {
return url;
} catch (Exception ex) {
LOG.Error("Error uploading.", ex);
MessageBox.Show(Language.GetString("box", LangKey.upload_failure) + " " + ex.ToString());
MessageBox.Show(Language.GetString("box", LangKey.upload_failure) + " " + ex.Message);
return null;
}
}

View file

@ -22,67 +22,114 @@ using System;
using System.Collections.Generic;
using System.Drawing;
using System.Net;
using System.Xml;
using System.Text;
using Greenshot.IniFile;
using GreenshotPlugin.Controls;
using GreenshotPlugin.Core;
using System.Runtime.Serialization.Json;
using System.IO;
namespace GreenshotBoxPlugin {
/// <summary>
/// Description of ImgurUtils.
/// </summary>
public class BoxUtils {
public static class BoxUtils {
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(BoxUtils));
private static BoxConfiguration config = IniConfig.GetIniSection<BoxConfiguration>();
private BoxUtils() {
}
private static string ParseTicket(string response) {
try {
XmlDocument doc = new XmlDocument();
doc.LoadXml(response);
XmlNodeList nodes = doc.GetElementsByTagName("ticket");
if(nodes.Count > 0) {
return nodes.Item(0).InnerText;
}
} catch (Exception ex) {
LOG.Error("Error parsing Box Response.", ex);
}
return null;
}
private static readonly BoxConfiguration Config = IniConfig.GetIniSection<BoxConfiguration>();
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 ticketUrl = string.Format("https://www.box.com/api/1.0/rest?action=get_ticket&api_key={0}", BoxCredentials.API_KEY);
string ticketXML = NetworkHelper.GetAsString(new Uri(ticketUrl));
string ticket = ParseTicket(ticketXML);
string authorizeUrl = string.Format("https://www.box.com/api/1.0/auth/{0}", ticket);
OAuthLoginForm loginForm = new OAuthLoginForm("Box Authorize", new Size(1060,600), authorizeUrl, "http://getgreenshot.org");
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) {
if (loginForm.CallbackParameters != null && loginForm.CallbackParameters.ContainsKey("auth_token")) {
config.BoxToken = loginForm.CallbackParameters["auth_token"];
IniConfig.Save();
return true;
}
if (!loginForm.isOk) {
return false;
}
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<Authorization>(authorizationResponse);
Config.BoxToken = authorization.AccessToken;
IniConfig.Save();
return true;
}
/// <summary>
/// Upload file by post
/// Download a url response as string
/// </summary>
/// <param name="url"></param>
/// <param name="parameters"></param>
/// <returns>response</returns>
public static string HttpUploadFile(string url, Dictionary<string, object> parameters) {
/// <param name=url">An Uri to specify the download location</param>
/// <returns>string with the file content</returns>
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);
}
/// <summary>
/// Upload parameters by post
/// </summary>
/// <param name="url"></param>
/// <param name="parameters"></param>
/// <returns>response</returns>
public static string HttpPost(string url, IDictionary<string, object> 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);
}
/// <summary>
/// Upload file by PUT
/// </summary>
/// <param name="url"></param>
/// <param name="content"></param>
/// <returns>response</returns>
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);
}
/// <summary>
/// Get REST request
/// </summary>
/// <param name="url"></param>
/// <returns>response</returns>
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);
}
@ -90,43 +137,102 @@ namespace GreenshotBoxPlugin {
/// 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
/// </summary>
/// <param name="imageData">byte[] with image data</param>
/// <param name="image">Image for box upload</param>
/// <param name="title">Title of box upload</param>
/// <param name="filename">Filename of box upload</param>
/// <returns>url to uploaded image</returns>
public static string UploadToBox(SurfaceContainer image, string title, string filename) {
string folderId = "0";
if (string.IsNullOrEmpty(config.BoxToken)) {
if (!Authorize()) {
return null;
}
}
string strUrl = string.Format("https://upload.box.net/api/1.0/upload/{0}/{1}?file_name={2}&new_copy=1", config.BoxToken, folderId, filename);
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("share", "1");
parameters.Add("new_file", image);
string response = HttpUploadFile(strUrl, parameters);
// Check if the token is wrong
if ("wrong auth token".Equals(response)) {
config.BoxToken = null;
IniConfig.Save();
return UploadToBox(image, title, filename);
}
LOG.DebugFormat("Box response: {0}", response);
XmlDocument doc = new XmlDocument();
doc.LoadXml(response);
XmlNodeList nodes = doc.GetElementsByTagName("status");
if(nodes.Count > 0) {
if ("upload_ok".Equals(nodes.Item(0).InnerText)) {
nodes = doc.GetElementsByTagName("file");
if (nodes.Count > 0) {
long id = long.Parse(nodes.Item(0).Attributes["id"].Value);
return string.Format("http://www.box.com/files/0/f/0/1/f_{0}", id);
while (true) {
const string folderId = "0";
if (string.IsNullOrEmpty(Config.BoxToken)) {
if (!Authorize()) {
return null;
}
}
IDictionary<string, object> parameters = new Dictionary<string, object>();
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<Upload>(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<FileEntry>(filesResponse);
return file.SharedLink.Url;
}
return string.Format("http://www.box.com/files/0/f/0/1/f_{0}", upload.Entries[0].Id);
}
}
}
/// <summary>
/// A simple helper class for the DataContractJsonSerializer
/// </summary>
public static class JSONSerializer {
/// <summary>
/// Helper method to serialize object to JSON
/// </summary>
/// <param name="jsonObject">JSON object</param>
/// <returns>string</returns>
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());
}
}
/// <summary>
/// Helper method to parse JSON to object
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="jsonString"></param>
/// <returns></returns>
public static T Deserialize<T>(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);
}
}
/// <summary>
/// Helper method to parse JSON to object
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="jsonString"></param>
/// <returns></returns>
public static T DeserializeWithDirectory<T>(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);
}
return null;
}
}
}

View file

@ -23,6 +23,7 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Drawing" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
</ItemGroup>
@ -30,6 +31,7 @@
<Compile Include="BoxConfiguration.cs" />
<Compile Include="BoxCredentials.cs" />
<Compile Include="BoxDestination.cs" />
<Compile Include="BoxEntities.cs" />
<Compile Include="BoxPlugin.cs" />
<Compile Include="BoxUtils.cs" />
<Compile Include="Forms\BoxForm.cs">
@ -71,10 +73,9 @@ mkdir "$(SolutionDir)bin\$(Configuration)\Plugins\$(ProjectName)"
copy "$(ProjectDir)bin\$(Configuration)\$(ProjectName).dll" "$(SolutionDir)bin\$(Configuration)\Plugins\$(ProjectName)\*.gsp"
copy "$(ProjectDir)bin\$(Configuration)\$(ProjectName).pdb" "$(SolutionDir)bin\$(Configuration)\Plugins\$(ProjectName)\"
mkdir "$(SolutionDir)bin\$(Configuration)\Languages\Plugins\$(ProjectName)"
copy "$(ProjectDir)Languages\*.xml" "$(SolutionDir)bin\$(Configuration)\Languages\Plugins\$(ProjectName)\"
copy "$(ProjectDir)Languages\*.xml" "$(SolutionDir)bin\$(Configuration)\Languages\Plugins\$(ProjectName)\"
</PostBuildEvent>
<PreBuildEvent>"$(SolutionDir)\tools\TortoiseSVN\SubWCRev.exe" "$(ProjectDir)\" "$(ProjectDir)\Properties\AssemblyInfo.cs.template" "$(ProjectDir)\Properties\AssemblyInfo.cs"
<PreBuildEvent>
if exist "$(ProjectDir)BoxCredentials.private.cs" (
rename "$(ProjectDir)BoxCredentials.cs" "BoxCredentials.orig.cs"
rename "$(ProjectDir)BoxCredentials.private.cs" "BoxCredentials.cs"

View file

@ -1,24 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<language description="Czech" ietf="cs-CZ" version="1.1.0.2411" languagegroup="">
<resources>
<resource name="CANCEL">Zrušit</resource>
<resource name="communication_wait">Komunikace s Box. Počkejte prosím...</resource>
<resource name="Configure">Konfigurovat</resource>
<resource name="delete_question">Jste si jistý, že chcete odstranit obrázek {0} z Box?</resource>
<resource name="delete_title">Odstranit Box {0}</resource>
<resource name="History">Historie</resource>
<resource name="label_AfterUpload">Po odeslání</resource>
<resource name="label_AfterUploadLinkToClipBoard">Odkaz do schránky</resource>
<resource name="label_AfterUploadOpenHistory">Otevřít historii</resource>
<resource name="label_AuthToken">Ověření Token</resource>
<resource name="label_DefaultSize">Výchozí velikost</resource>
<resource name="label_upload_format">Formát obrázku</resource>
<resource name="OK">OK</resource>
<resource name="settings_title">Nastavení</resource>
<resource name="TokenNotSet">Prosím ověřit dostupnost aplikace Box. Nastavení přístupu.</resource>
<resource name="Upload">Odeslat</resource>
<resource name="upload_failure">Při odesílání do Box došlo k chybě:</resource>
<resource name="upload_menu_item">Odeslat do Box</resource>
<resource name="upload_success">Obrázek úspěšně odeslán do Box!</resource>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<language description="Czech" ietf="cs-CZ" version="1.1.0.2411" languagegroup="">
<resources>
<resource name="CANCEL">Zrušit</resource>
<resource name="communication_wait">Komunikace s Box. Počkejte prosím...</resource>
<resource name="Configure">Konfigurovat</resource>
<resource name="delete_question">Jste si jistý, že chcete odstranit obrázek {0} z Box?</resource>
<resource name="delete_title">Odstranit Box {0}</resource>
<resource name="History">Historie</resource>
<resource name="label_AfterUpload">Po odeslání</resource>
<resource name="label_AfterUploadLinkToClipBoard">Odkaz do schránky</resource>
<resource name="label_AfterUploadOpenHistory">Otevřít historii</resource>
<resource name="label_AuthToken">Ověření Token</resource>
<resource name="label_DefaultSize">Výchozí velikost</resource>
<resource name="label_upload_format">Formát obrázku</resource>
<resource name="OK">OK</resource>
<resource name="settings_title">Nastavení</resource>
<resource name="TokenNotSet">Prosím ověřit dostupnost aplikace Box. Nastavení přístupu.</resource>
<resource name="Upload">Odeslat</resource>
<resource name="upload_failure">Při odesílání do Box došlo k chybě:</resource>
<resource name="upload_menu_item">Odeslat do Box</resource>
<resource name="upload_success">Obrázek úspěšně odeslán do Box!</resource>
</resources>
</language>

View file

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<language description="Bahasa Indonesia" ietf="id-ID" version="1.0.0" languagegroup="">
<resources>
<resource name="communication_wait">Menyambungkan ke Box. Tunggu sebentar...</resource>
<resource name="Configure">Konfigurasi Box</resource>
<resource name="label_AfterUpload">Setelah Diunggah</resource>
<resource name="label_AfterUploadLinkToClipBoard">Sambung ke papanklip</resource>
<resource name="label_upload_format">Format gambar</resource>
<resource name="settings_title">Setelan Box</resource>
<resource name="upload_failure">Kesalahan terjadi ketika mengunggah ke Box:</resource>
<resource name="upload_menu_item">Unggah ke Box</resource>
<resource name="upload_success">Gambar telah berhasil diunggah ke Box!</resource>
</resources>
</language>

View file

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<language description="Polski" ietf="pl-PL" version="1.1.4" languagegroup="2">
<resources>
<resource name="communication_wait">Trwa komunikacja z Box. Proszę czekać...</resource>
<resource name="Configure">Konfiguruj Box</resource>
<resource name="label_AfterUpload">Po wysłaniu</resource>
<resource name="label_AfterUploadLinkToClipBoard">Link do schowka</resource>
<resource name="label_upload_format">Format obrazu</resource>
<resource name="settings_title">Ustawienia Box</resource>
<resource name="upload_failure">Wystąpił błąd przy wysyłaniu do Box:</resource>
<resource name="upload_menu_item">Wyślij do Box</resource>
<resource name="upload_success">Wysyłanie obrazu do Box powiodło się!</resource>
</resources>
</language>

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<language description="Русский" ietf="ru-RU" version="1.1.0.2515" languagegroup="5">
<language description="Русский" ietf="ru-RU" version="1.1.4.2622" languagegroup="5">
<resources>
<resource name="communication_wait">Обмен данными с Box. Подождите...</resource>
<resource name="Configure">Настройка Box</resource>

View file

@ -1,14 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<language description="正體中文" ietf="zh-TW" version="1.0.0" languagegroup="9">
<resources>
<resource name="communication_wait">正在與 Box 通訊,請稍候...</resource>
<resource name="Configure">組態 Box</resource>
<resource name="label_AfterUpload">上傳後</resource>
<resource name="label_AfterUploadLinkToClipBoard">連結到剪貼簿</resource>
<resource name="label_upload_format">圖片格式</resource>
<resource name="settings_title">Box 設定</resource>
<resource name="upload_failure">上傳到 Box 時發生錯誤:</resource>
<resource name="upload_menu_item">上傳到 Box</resource>
<resource name="upload_success">上傳圖片到 Box 成功!</resource>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<language description="正體中文" ietf="zh-TW" version="1.0.0" languagegroup="9">
<resources>
<resource name="communication_wait">正在與 Box 通訊,請稍候...</resource>
<resource name="Configure">組態 Box</resource>
<resource name="label_AfterUpload">上傳後</resource>
<resource name="label_AfterUploadLinkToClipBoard">連結到剪貼簿</resource>
<resource name="label_upload_format">圖片格式</resource>
<resource name="settings_title">Box 設定</resource>
<resource name="upload_failure">上傳到 Box 時發生錯誤:</resource>
<resource name="upload_menu_item">上傳到 Box</resource>
<resource name="upload_success">上傳圖片到 Box 成功!</resource>
</resources>
</language>

View file

@ -51,4 +51,4 @@ using Greenshot.Plugin;
//
// You can specify all the values or you can use the default the Revision and
// Build Numbers by using the '*' as shown below:
[assembly: AssemblyVersion("1.1.4.$WCREV$")]
[assembly: AssemblyVersion("1.1.6.$WCREV$")]