mirror of
https://github.com/ShareX/ShareX.git
synced 2024-10-06 05:05:42 +13:00
base functionallity for OneDrive
This commit is contained in:
parent
d46c719560
commit
053987cf5d
4 changed files with 604 additions and 439 deletions
|
@ -30,7 +30,9 @@
|
||||||
<s:String x:Key="/Default/CodeStyle/CodeCleanup/Profiles/=ShareX/@EntryIndexedValue"><?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></s:String>
|
<s:String x:Key="/Default/CodeStyle/CodeCleanup/Profiles/=ShareX/@EntryIndexedValue"><?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></s:String>
|
||||||
<s:String x:Key="/Default/CodeStyle/CodeCleanup/RecentlyUsedProfile/@EntryValue">ShareX</s:String>
|
<s:String x:Key="/Default/CodeStyle/CodeCleanup/RecentlyUsedProfile/@EntryValue">ShareX</s:String>
|
||||||
<s:String x:Key="/Default/CodeStyle/CodeCleanup/SilentCleanupProfile/@EntryValue">ShareX</s:String>
|
<s:String x:Key="/Default/CodeStyle/CodeCleanup/SilentCleanupProfile/@EntryValue">ShareX</s:String>
|
||||||
|
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/BLANK_LINES_AROUND_AUTO_PROPERTY/@EntryValue">0</s:Int64>
|
||||||
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/BLANK_LINES_AROUND_FIELD/@EntryValue">0</s:Int64>
|
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/BLANK_LINES_AROUND_FIELD/@EntryValue">0</s:Int64>
|
||||||
|
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/BLANK_LINES_AROUND_PROPERTY/@EntryValue">0</s:Int64>
|
||||||
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_BLANK_LINES_IN_CODE/@EntryValue">1</s:Int64>
|
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_BLANK_LINES_IN_CODE/@EntryValue">1</s:Int64>
|
||||||
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_BLANK_LINES_IN_DECLARATIONS/@EntryValue">1</s:Int64>
|
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_BLANK_LINES_IN_DECLARATIONS/@EntryValue">1</s:Int64>
|
||||||
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_FIELD_ATTRIBUTE_ON_SAME_LINE/@EntryValue">False</s:Boolean>
|
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_FIELD_ATTRIBUTE_ON_SAME_LINE/@EntryValue">False</s:Boolean>
|
||||||
|
@ -42,4 +44,5 @@
|
||||||
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_BEFORE_SIZEOF_PARENTHESES/@EntryValue">False</s:Boolean>
|
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_BEFORE_SIZEOF_PARENTHESES/@EntryValue">False</s:Boolean>
|
||||||
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_BEFORE_TYPEOF_PARENTHESES/@EntryValue">False</s:Boolean>
|
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_BEFORE_TYPEOF_PARENTHESES/@EntryValue">False</s:Boolean>
|
||||||
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_WITHIN_SINGLE_LINE_ARRAY_INITIALIZER_BRACES/@EntryValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_WITHIN_SINGLE_LINE_ARRAY_INITIALIZER_BRACES/@EntryValue">True</s:Boolean>
|
||||||
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_LINES/@EntryValue">False</s:Boolean></wpf:ResourceDictionary>
|
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_LINES/@EntryValue">False</s:Boolean>
|
||||||
|
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
|
|
@ -78,6 +78,8 @@ public enum FileDestination
|
||||||
{
|
{
|
||||||
[Description("Dropbox")]
|
[Description("Dropbox")]
|
||||||
Dropbox,
|
Dropbox,
|
||||||
|
[Description("OneDrive")]
|
||||||
|
OneDrive,
|
||||||
[Description("FTP")]
|
[Description("FTP")]
|
||||||
FTP,
|
FTP,
|
||||||
[Description("Copy")]
|
[Description("Copy")]
|
||||||
|
|
159
UploadersLib/FileUploaders/OneDrive.cs
Normal file
159
UploadersLib/FileUploaders/OneDrive.cs
Normal file
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#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<OneDriveUploadInfo>(result.Response);
|
||||||
|
result.URL = resultJson.source;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetAuthorizationURL()
|
||||||
|
{
|
||||||
|
Dictionary<string, string> args = new Dictionary<string, string>();
|
||||||
|
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<string, string> args = new Dictionary<string, string>();
|
||||||
|
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<OAuth2Token>(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<string, string> args = new Dictionary<string, string>();
|
||||||
|
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<OAuth2Token>(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; }
|
||||||
|
}
|
||||||
|
}
|
|
@ -110,6 +110,7 @@
|
||||||
<Compile Include="FileUploaders\Jira.cs" />
|
<Compile Include="FileUploaders\Jira.cs" />
|
||||||
<Compile Include="FileUploaders\Hostr.cs" />
|
<Compile Include="FileUploaders\Hostr.cs" />
|
||||||
<Compile Include="FileUploaders\Mega.cs" />
|
<Compile Include="FileUploaders\Mega.cs" />
|
||||||
|
<Compile Include="FileUploaders\OneDrive.cs" />
|
||||||
<Compile Include="FileUploaders\OwnCloud.cs" />
|
<Compile Include="FileUploaders\OwnCloud.cs" />
|
||||||
<Compile Include="FileUploaders\SFTP.cs" />
|
<Compile Include="FileUploaders\SFTP.cs" />
|
||||||
<Compile Include="FileUploaders\Minus.cs" />
|
<Compile Include="FileUploaders\Minus.cs" />
|
||||||
|
|
Loading…
Reference in a new issue