From 053987cf5d2dbf51ca2568b8f52eb26c107228e7 Mon Sep 17 00:00:00 2001 From: l0nley Date: Tue, 15 Jul 2014 14:18:55 +0300 Subject: [PATCH] base functionallity for OneDrive --- ShareX.sln.DotSettings | 5 +- UploadersLib/Enums.cs | 2 + UploadersLib/FileUploaders/OneDrive.cs | 159 +++++ UploadersLib/UploadersLib.csproj | 877 +++++++++++++------------ 4 files changed, 604 insertions(+), 439 deletions(-) create mode 100644 UploadersLib/FileUploaders/OneDrive.cs diff --git a/ShareX.sln.DotSettings b/ShareX.sln.DotSettings index 3b36d87e9..2f78778b3 100644 --- a/ShareX.sln.DotSettings +++ b/ShareX.sln.DotSettings @@ -30,7 +30,9 @@ <?xml version="1.0" encoding="utf-16"?><Profile name="ShareX"><CSReformatCode>True</CSReformatCode><CSOptimizeUsings><OptimizeUsings>False</OptimizeUsings><EmbraceInRegion>False</EmbraceInRegion><RegionName></RegionName></CSOptimizeUsings><XAMLCollapseEmptyTags>False</XAMLCollapseEmptyTags></Profile> ShareX ShareX + 0 0 + 0 1 1 False @@ -42,4 +44,5 @@ False False True - False \ No newline at end of file + False + True \ No newline at end of file diff --git a/UploadersLib/Enums.cs b/UploadersLib/Enums.cs index 8f859e44d..3d731d729 100644 --- a/UploadersLib/Enums.cs +++ b/UploadersLib/Enums.cs @@ -78,6 +78,8 @@ public enum FileDestination { [Description("Dropbox")] Dropbox, + [Description("OneDrive")] + OneDrive, [Description("FTP")] FTP, [Description("Copy")] diff --git a/UploadersLib/FileUploaders/OneDrive.cs b/UploadersLib/FileUploaders/OneDrive.cs new file mode 100644 index 000000000..7ad692683 --- /dev/null +++ b/UploadersLib/FileUploaders/OneDrive.cs @@ -0,0 +1,159 @@ +#region License Information (GPL v3) + +/* + ShareX - A program that allows you to take screenshots and share any file type + Copyright (C) 2007-2014 ShareX Developers + + 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 2 + 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, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + Optionally you can also view the license at . +*/ + +#endregion License Information (GPL v3) + +using System.Collections.Generic; +using System.IO; +using Newtonsoft.Json; +using UploadersLib.HelperClasses; + +namespace UploadersLib.FileUploaders +{ + public sealed class OneDrive : FileUploader, IOAuth2 + { + public OneDrive(OAuth2Info authInfo) + { + AuthInfo = authInfo; + } + + public OAuth2Info AuthInfo { get; private set; } + + public string FolderId { get; set; } + + public override UploadResult Upload(Stream stream, string fileName) + { + if (!CheckAuthorization()) + { + return null; + } + + if (string.IsNullOrEmpty(FolderId)) + { + FolderId = "me/skydrive/files"; + } + + var uploadUri = string.Format("https://apis.live.net/v5.0/{0}/{1}?access_token={2}&overwrite=true", FolderId, fileName, AuthInfo.Token.access_token); + var result = UploadData(stream, uploadUri, string.Empty, string.Empty, null, null, null, ResponseType.Text, HttpMethod.PUT); + if (!result.IsSuccess) + { + return result; + } + + var resultJson = JsonConvert.DeserializeObject(result.Response); + result.URL = resultJson.source; + return result; + } + + public string GetAuthorizationURL() + { + Dictionary args = new Dictionary(); + args.Add("response_type", "code"); + args.Add("client_id", AuthInfo.Client_ID); + args.Add("scope", "wl.skydrive_update"); + return CreateQuery("https://login.live.com/oauth20_authorize.srf", args); + } + + public bool GetAccessToken(string code) + { + Dictionary args = new Dictionary(); + args.Add("grant_type", "authorization_code"); + args.Add("code", code); + args.Add("client_id", AuthInfo.Client_ID); + args.Add("client_secret", AuthInfo.Client_Secret); + + var response = SendRequest(HttpMethod.POST, "https://login.live.com/oauth20_token.srf", args); + + if (string.IsNullOrEmpty(response)) + { + return false; + } + + OAuth2Token token = JsonConvert.DeserializeObject(response); + + if (token == null || string.IsNullOrEmpty(token.access_token)) + { + return false; + } + + token.UpdateExpireDate(); + AuthInfo.Token = token; + return true; + } + + public bool RefreshAccessToken() + { + if (!OAuth2Info.CheckOAuth(AuthInfo) || string.IsNullOrEmpty(AuthInfo.Token.refresh_token)) + { + return false; + } + + Dictionary args = new Dictionary(); + args.Add("grant_type", "refresh_token"); + args.Add("refresh_token", AuthInfo.Token.refresh_token); + args.Add("client_id", AuthInfo.Client_ID); + args.Add("client_secret", AuthInfo.Client_Secret); + + var response = SendRequest(HttpMethod.POST, "https://login.live.com/oauth20_token.srf", args); + + if (string.IsNullOrEmpty(response)) + { + return false; + } + + OAuth2Token token = JsonConvert.DeserializeObject(response); + + if (token == null || string.IsNullOrEmpty(token.access_token)) + { + return false; + } + + token.UpdateExpireDate(); + AuthInfo.Token = token; + return true; + } + + public bool CheckAuthorization() + { + if (OAuth2Info.CheckOAuth(AuthInfo)) + { + if (!AuthInfo.Token.IsExpired || RefreshAccessToken()) + { + return true; + } + + Errors.Add("Refresh access token failed."); + return false; + } + + Errors.Add("Live Id login is required."); + return false; + } + } + + internal sealed class OneDriveUploadInfo + { + public string id { get; set; } + public string source { get; set; } + } +} diff --git a/UploadersLib/UploadersLib.csproj b/UploadersLib/UploadersLib.csproj index 1dcd626a5..28559edf7 100644 --- a/UploadersLib/UploadersLib.csproj +++ b/UploadersLib/UploadersLib.csproj @@ -1,449 +1,450 @@ - - - - Debug - AnyCPU - 9.0.30729 - 2.0 - {E1C94415-3424-4517-A2A1-B2FDD1F59C67} - Library - Properties - UploadersLib - UploadersLib - v4.0 - 512 - false - - - - - - - - - - - 3.5 - - false - publish\ - true - Disk - false - Foreground - 7 - Days - false - false - true - 0 - 1.0.0.%2a - false - true - - ..\ - true - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - MinimumRecommendedRules.ruleset - Off - false - - - none - true - bin\Release\ - TRACE - prompt - 4 - MinimumRecommendedRules.ruleset - false - Off - - - - False - ..\packages\MegaApiClient.1.0.4\lib\MegaApiClient.dll - - - False - ..\packages\Newtonsoft.Json.6.0.3\lib\net40\Newtonsoft.Json.dll - - - False - ..\packages\SSH.NET.2014.4.6-beta1\lib\net40\Renci.SshNet.dll - - - - - - - False - ..\packages\System.Net.FtpClient.1.0.5281.14359\lib\net40\System.Net.FtpClient.dll - - - - - - - - 3.5 - - - - - - - - - - - - - - - - - - - - - Form - - - FTPClientForm.cs - - - - - Component - - - UserControl - - - AccountTypeControl.cs - - - Form - - - EmailForm.cs - - - Form - - - GoogleTranslateGUI.cs - - - Form - - - JiraUpload.cs - - - UserControl - - - OAuthControl.cs - - - Form - - - OAuthWebForm.cs - - - Form - - - ResponseForm.cs - - - Form - - - Form - - - UploadersConfigForm.cs - - - - - - - - Form - - - UserPassBox.cs - - - UserControl - - - AccountsControl.cs - - - - Form - - - DropboxFilesForm.cs - - - Form - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Form - - - TwitterTweetForm.cs - - - - - - - - - - - - - - - - - - - - - - - - - - True - True - Resources.resx - - - - - - - - - - - - - - - - - - - - - - - FTPClientForm.cs - - - AccountTypeControl.cs - - - EmailForm.cs - - - GoogleTranslateGUI.cs - - - JiraUpload.cs - - - OAuthControl.cs - - - OAuthWebForm.cs - - - ResponseForm.cs - - - UploadersConfigForm.cs - Designer - - - UserPassBox.cs - Designer - - - AccountsControl.cs - - - DropboxFilesForm.cs - - - TwitterTweetForm.cs - Designer - - - ResXFileCodeGenerator - Resources.Designer.cs - Designer - - - - - False - .NET Framework 3.5 SP1 Client Profile - false - - - False - .NET Framework 3.5 SP1 - true - - - False - Windows Installer 3.1 - true - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - {327750E1-9FB7-4CC3-8AEA-9BC42180CAD3} - HelpersLib - - - - - - - - - - - - - - - - + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {E1C94415-3424-4517-A2A1-B2FDD1F59C67} + Library + Properties + UploadersLib + UploadersLib + v4.0 + 512 + false + + + + + + + + + + + 3.5 + + false + publish\ + true + Disk + false + Foreground + 7 + Days + false + false + true + 0 + 1.0.0.%2a + false + true + + ..\ + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + MinimumRecommendedRules.ruleset + Off + false + + + none + true + bin\Release\ + TRACE + prompt + 4 + MinimumRecommendedRules.ruleset + false + Off + + + + False + ..\packages\MegaApiClient.1.0.4\lib\MegaApiClient.dll + + + False + ..\packages\Newtonsoft.Json.6.0.3\lib\net40\Newtonsoft.Json.dll + + + False + ..\packages\SSH.NET.2014.4.6-beta1\lib\net40\Renci.SshNet.dll + + + + + + + False + ..\packages\System.Net.FtpClient.1.0.5281.14359\lib\net40\System.Net.FtpClient.dll + + + + + + + + 3.5 + + + + + + + + + + + + + + + + + + + + + + Form + + + FTPClientForm.cs + + + + + Component + + + UserControl + + + AccountTypeControl.cs + + + Form + + + EmailForm.cs + + + Form + + + GoogleTranslateGUI.cs + + + Form + + + JiraUpload.cs + + + UserControl + + + OAuthControl.cs + + + Form + + + OAuthWebForm.cs + + + Form + + + ResponseForm.cs + + + Form + + + Form + + + UploadersConfigForm.cs + + + + + + + + Form + + + UserPassBox.cs + + + UserControl + + + AccountsControl.cs + + + + Form + + + DropboxFilesForm.cs + + + Form + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Form + + + TwitterTweetForm.cs + + + + + + + + + + + + + + + + + + + + + + + + + + True + True + Resources.resx + + + + + + + + + + + + + + + + + + + + + + + FTPClientForm.cs + + + AccountTypeControl.cs + + + EmailForm.cs + + + GoogleTranslateGUI.cs + + + JiraUpload.cs + + + OAuthControl.cs + + + OAuthWebForm.cs + + + ResponseForm.cs + + + UploadersConfigForm.cs + Designer + + + UserPassBox.cs + Designer + + + AccountsControl.cs + + + DropboxFilesForm.cs + + + TwitterTweetForm.cs + Designer + + + ResXFileCodeGenerator + Resources.Designer.cs + Designer + + + + + False + .NET Framework 3.5 SP1 Client Profile + false + + + False + .NET Framework 3.5 SP1 + true + + + False + Windows Installer 3.1 + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {327750E1-9FB7-4CC3-8AEA-9BC42180CAD3} + HelpersLib + + + + + + + + + + + + + + + + cd $(ProjectDir)APIKeys\ if not exist APIKeysLocal.cs ( type nul > APIKeysLocal.cs -) - +) + + --> \ No newline at end of file