2013-11-03 23:53:49 +13:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
2014-12-31 22:41:32 +13:00
|
|
|
|
Copyright © 2007-2015 ShareX Developers
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
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 Newtonsoft.Json;
|
2014-12-11 12:19:28 +13:00
|
|
|
|
using ShareX.UploadersLib.HelperClasses;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.Specialized;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
namespace ShareX.UploadersLib.FileUploaders
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
public sealed class GoogleDrive : FileUploader, IOAuth2
|
|
|
|
|
{
|
|
|
|
|
public OAuth2Info AuthInfo { get; set; }
|
2014-03-18 23:27:22 +13:00
|
|
|
|
public bool IsPublic { get; set; }
|
2014-07-01 20:33:33 +12:00
|
|
|
|
public string FolderID { get; set; }
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
public GoogleDrive(OAuth2Info oauth)
|
|
|
|
|
{
|
|
|
|
|
AuthInfo = oauth;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetAuthorizationURL()
|
|
|
|
|
{
|
2014-12-26 07:36:15 +13:00
|
|
|
|
Dictionary<string, string> args = new Dictionary<string, string>();
|
|
|
|
|
args.Add("response_type", "code");
|
|
|
|
|
args.Add("client_id", AuthInfo.Client_ID);
|
|
|
|
|
args.Add("redirect_uri", "urn:ietf:wg:oauth:2.0:oob");
|
|
|
|
|
args.Add("scope", "https://www.googleapis.com/auth/drive");
|
|
|
|
|
|
|
|
|
|
return CreateQuery("https://accounts.google.com/o/oauth2/auth", args);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool GetAccessToken(string code)
|
|
|
|
|
{
|
|
|
|
|
Dictionary<string, string> args = new Dictionary<string, string>();
|
|
|
|
|
args.Add("code", code);
|
|
|
|
|
args.Add("client_id", AuthInfo.Client_ID);
|
|
|
|
|
args.Add("client_secret", AuthInfo.Client_Secret);
|
|
|
|
|
args.Add("redirect_uri", "urn:ietf:wg:oauth:2.0:oob");
|
|
|
|
|
args.Add("grant_type", "authorization_code");
|
|
|
|
|
|
2014-04-17 05:55:07 +12:00
|
|
|
|
string response = SendRequest(HttpMethod.POST, "https://accounts.google.com/o/oauth2/token", args);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(response))
|
|
|
|
|
{
|
|
|
|
|
OAuth2Token token = JsonConvert.DeserializeObject<OAuth2Token>(response);
|
|
|
|
|
|
|
|
|
|
if (token != null && !string.IsNullOrEmpty(token.access_token))
|
|
|
|
|
{
|
|
|
|
|
token.UpdateExpireDate();
|
|
|
|
|
AuthInfo.Token = token;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool RefreshAccessToken()
|
|
|
|
|
{
|
|
|
|
|
if (OAuth2Info.CheckOAuth(AuthInfo) && !string.IsNullOrEmpty(AuthInfo.Token.refresh_token))
|
|
|
|
|
{
|
|
|
|
|
Dictionary<string, string> args = new Dictionary<string, string>();
|
|
|
|
|
args.Add("refresh_token", AuthInfo.Token.refresh_token);
|
|
|
|
|
args.Add("client_id", AuthInfo.Client_ID);
|
|
|
|
|
args.Add("client_secret", AuthInfo.Client_Secret);
|
|
|
|
|
args.Add("grant_type", "refresh_token");
|
|
|
|
|
|
2014-04-17 05:55:07 +12:00
|
|
|
|
string response = SendRequest(HttpMethod.POST, "https://accounts.google.com/o/oauth2/token", args);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(response))
|
|
|
|
|
{
|
|
|
|
|
OAuth2Token token = JsonConvert.DeserializeObject<OAuth2Token>(response);
|
|
|
|
|
|
|
|
|
|
if (token != null && !string.IsNullOrEmpty(token.access_token))
|
|
|
|
|
{
|
|
|
|
|
token.UpdateExpireDate();
|
|
|
|
|
string refresh_token = AuthInfo.Token.refresh_token;
|
|
|
|
|
AuthInfo.Token = token;
|
|
|
|
|
AuthInfo.Token.refresh_token = refresh_token;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool CheckAuthorization()
|
|
|
|
|
{
|
|
|
|
|
if (OAuth2Info.CheckOAuth(AuthInfo))
|
|
|
|
|
{
|
|
|
|
|
if (AuthInfo.Token.IsExpired && !RefreshAccessToken())
|
|
|
|
|
{
|
|
|
|
|
Errors.Add("Refresh access token failed.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Errors.Add("Login is required.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-26 07:36:15 +13:00
|
|
|
|
private NameValueCollection GetAuthHeaders()
|
|
|
|
|
{
|
|
|
|
|
NameValueCollection headers = new NameValueCollection();
|
|
|
|
|
headers.Add("Authorization", "Bearer " + AuthInfo.Token.access_token);
|
|
|
|
|
return headers;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-31 16:32:36 +13:00
|
|
|
|
private string GetMetadata(string title, string parentID)
|
2014-07-01 20:33:33 +12:00
|
|
|
|
{
|
|
|
|
|
object metadata;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(parentID))
|
|
|
|
|
{
|
|
|
|
|
metadata = new
|
|
|
|
|
{
|
|
|
|
|
title = title,
|
|
|
|
|
parents = new[]
|
|
|
|
|
{
|
|
|
|
|
new
|
|
|
|
|
{
|
|
|
|
|
id = parentID
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
metadata = new
|
|
|
|
|
{
|
|
|
|
|
title = title
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-02 00:12:21 +13:00
|
|
|
|
return JsonConvert.SerializeObject(metadata);
|
2014-07-01 20:33:33 +12:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-02 04:40:40 +12:00
|
|
|
|
private void SetPermissions(string fileID, GoogleDrivePermissionRole role, GoogleDrivePermissionType type, string value, bool withLink)
|
2014-03-18 23:27:22 +13:00
|
|
|
|
{
|
2015-07-08 11:43:04 +12:00
|
|
|
|
if (!CheckAuthorization()) return;
|
2015-07-07 17:30:50 +12:00
|
|
|
|
|
2014-03-18 23:27:22 +13:00
|
|
|
|
string url = string.Format("https://www.googleapis.com/drive/v2/files/{0}/permissions", fileID);
|
|
|
|
|
|
|
|
|
|
string json = JsonConvert.SerializeObject(new
|
|
|
|
|
{
|
|
|
|
|
role = role.ToString(),
|
|
|
|
|
type = type.ToString(),
|
|
|
|
|
value = value,
|
|
|
|
|
withLink = withLink.ToString()
|
|
|
|
|
});
|
|
|
|
|
|
2014-07-07 04:55:50 +12:00
|
|
|
|
string response = SendRequestJSON(url, json, GetAuthHeaders());
|
2014-03-18 23:27:22 +13:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-02 04:40:40 +12:00
|
|
|
|
public List<GoogleDriveFile> GetFolders(bool trashed = false, bool writer = true)
|
2014-07-01 22:18:07 +12:00
|
|
|
|
{
|
2014-07-02 04:40:40 +12:00
|
|
|
|
if (!CheckAuthorization()) return null;
|
|
|
|
|
|
|
|
|
|
string query = "mimeType = 'application/vnd.google-apps.folder'";
|
|
|
|
|
|
|
|
|
|
if (!trashed)
|
|
|
|
|
{
|
|
|
|
|
query += " and trashed = false";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (writer)
|
|
|
|
|
{
|
|
|
|
|
query += " and 'me' in writers";
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-01 22:18:07 +12:00
|
|
|
|
Dictionary<string, string> args = new Dictionary<string, string>();
|
2014-07-02 04:40:40 +12:00
|
|
|
|
args.Add("q", query);
|
2014-07-01 22:18:07 +12:00
|
|
|
|
|
2014-07-07 04:55:50 +12:00
|
|
|
|
string response = SendRequest(HttpMethod.GET, "https://www.googleapis.com/drive/v2/files", args, GetAuthHeaders());
|
2014-07-01 22:18:07 +12:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(response))
|
|
|
|
|
{
|
|
|
|
|
GoogleDriveFileList fileList = JsonConvert.DeserializeObject<GoogleDriveFileList>(response);
|
|
|
|
|
|
|
|
|
|
if (fileList != null)
|
|
|
|
|
{
|
|
|
|
|
return fileList.items;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-03 23:53:49 +13:00
|
|
|
|
public override UploadResult Upload(Stream stream, string fileName)
|
|
|
|
|
{
|
2014-07-02 04:40:40 +12:00
|
|
|
|
if (!CheckAuthorization()) return null;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2015-03-31 16:32:36 +13:00
|
|
|
|
string metadata = GetMetadata(fileName, FolderID);
|
2015-03-31 15:55:37 +13:00
|
|
|
|
|
2015-04-02 00:12:21 +13:00
|
|
|
|
UploadResult result = UploadData(stream, "https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart", fileName, headers: GetAuthHeaders(),
|
|
|
|
|
requestContentType: "multipart/related", metadata: metadata);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(result.Response))
|
|
|
|
|
{
|
2014-07-01 22:18:07 +12:00
|
|
|
|
GoogleDriveFile upload = JsonConvert.DeserializeObject<GoogleDriveFile>(result.Response);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
if (upload != null)
|
|
|
|
|
{
|
2014-07-01 20:33:33 +12:00
|
|
|
|
AllowReportProgress = false;
|
|
|
|
|
|
2014-03-18 23:27:22 +13:00
|
|
|
|
if (IsPublic)
|
|
|
|
|
{
|
|
|
|
|
SetPermissions(upload.id, GoogleDrivePermissionRole.reader, GoogleDrivePermissionType.anyone, "", true);
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-03 23:53:49 +13:00
|
|
|
|
result.URL = upload.alternateLink;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-01 22:18:07 +12:00
|
|
|
|
public class GoogleDriveFile
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
public string id { get; set; }
|
|
|
|
|
public string alternateLink { get; set; }
|
2014-07-01 22:18:07 +12:00
|
|
|
|
public string title { get; set; }
|
2014-07-02 04:40:40 +12:00
|
|
|
|
public string description { get; set; }
|
2014-07-01 22:18:07 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class GoogleDriveFileList
|
|
|
|
|
{
|
|
|
|
|
public List<GoogleDriveFile> items { get; set; }
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
2014-03-18 23:27:22 +13:00
|
|
|
|
|
|
|
|
|
public enum GoogleDrivePermissionRole
|
|
|
|
|
{
|
|
|
|
|
owner, reader, writer
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum GoogleDrivePermissionType
|
|
|
|
|
{
|
|
|
|
|
user, group, domain, anyone
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|