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
|
2018-01-02 03:59:14 +13:00
|
|
|
|
Copyright (c) 2007-2018 ShareX Team
|
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;
|
2016-06-28 05:20:55 +12:00
|
|
|
|
using ShareX.UploadersLib.Properties;
|
2016-06-11 22:11:58 +12:00
|
|
|
|
using System;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.Specialized;
|
2016-06-28 05:20:55 +12:00
|
|
|
|
using System.Drawing;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
using System.IO;
|
2016-06-11 22:11:58 +12:00
|
|
|
|
using System.Web;
|
2016-05-01 20:09:11 +12:00
|
|
|
|
using System.Windows.Forms;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
namespace ShareX.UploadersLib.FileUploaders
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2016-03-23 06:24:41 +13:00
|
|
|
|
public class GoogleDriveFileUploaderService : FileUploaderService
|
|
|
|
|
{
|
|
|
|
|
public override FileDestination EnumValue { get; } = FileDestination.GoogleDrive;
|
|
|
|
|
|
2016-06-28 05:20:55 +12:00
|
|
|
|
public override Icon ServiceIcon => Resources.GoogleDrive;
|
|
|
|
|
|
2016-03-24 07:47:59 +13:00
|
|
|
|
public override bool CheckConfig(UploadersConfig config)
|
2016-03-23 06:24:41 +13:00
|
|
|
|
{
|
2016-03-24 07:47:59 +13:00
|
|
|
|
return OAuth2Info.CheckOAuth(config.GoogleDriveOAuth2Info);
|
2016-03-23 06:24:41 +13:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-04 21:53:11 +12:00
|
|
|
|
public override GenericUploader CreateUploader(UploadersConfig config, TaskReferenceHelper taskInfo)
|
2016-03-23 06:24:41 +13:00
|
|
|
|
{
|
2016-03-24 07:47:59 +13:00
|
|
|
|
return new GoogleDrive(config.GoogleDriveOAuth2Info)
|
2016-03-23 06:24:41 +13:00
|
|
|
|
{
|
2016-03-24 07:47:59 +13:00
|
|
|
|
IsPublic = config.GoogleDriveIsPublic,
|
2016-06-11 22:11:58 +12:00
|
|
|
|
DirectLink = config.GoogleDriveDirectLink,
|
2016-03-24 07:47:59 +13:00
|
|
|
|
FolderID = config.GoogleDriveUseFolder ? config.GoogleDriveFolderID : null
|
2016-03-23 06:24:41 +13:00
|
|
|
|
};
|
|
|
|
|
}
|
2016-05-01 20:09:11 +12:00
|
|
|
|
|
|
|
|
|
public override TabPage GetUploadersConfigTabPage(UploadersConfigForm form) => form.tpGoogleDrive;
|
2016-03-23 06:24:41 +13:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-17 19:07:02 +12:00
|
|
|
|
public enum GoogleDrivePermissionRole
|
|
|
|
|
{
|
2018-04-11 16:30:09 +12:00
|
|
|
|
owner, reader, writer, organizer, commenter
|
2016-09-17 19:07:02 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum GoogleDrivePermissionType
|
|
|
|
|
{
|
|
|
|
|
user, group, domain, anyone
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-03 23:53:49 +13:00
|
|
|
|
public sealed class GoogleDrive : FileUploader, IOAuth2
|
|
|
|
|
{
|
2018-04-11 16:28:01 +12:00
|
|
|
|
private GoogleOAuth2 GoogleAuth { get; set; }
|
2014-03-18 23:27:22 +13:00
|
|
|
|
public bool IsPublic { get; set; }
|
2016-06-11 22:11:58 +12:00
|
|
|
|
public bool DirectLink { 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)
|
|
|
|
|
{
|
2018-04-11 16:28:01 +12:00
|
|
|
|
GoogleAuth = new GoogleOAuth2(oauth, this)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2018-04-11 16:28:01 +12:00
|
|
|
|
Scope = "https://www.googleapis.com/auth/drive"
|
|
|
|
|
};
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-11 16:28:01 +12:00
|
|
|
|
public OAuth2Info AuthInfo => GoogleAuth.AuthInfo;
|
|
|
|
|
|
2013-11-03 23:53:49 +13:00
|
|
|
|
public bool RefreshAccessToken()
|
|
|
|
|
{
|
2018-04-11 16:28:01 +12:00
|
|
|
|
return GoogleAuth.RefreshAccessToken();
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool CheckAuthorization()
|
|
|
|
|
{
|
2018-04-11 16:28:01 +12:00
|
|
|
|
return GoogleAuth.CheckAuthorization();
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2018-04-11 16:28:01 +12:00
|
|
|
|
public string GetAuthorizationURL()
|
|
|
|
|
{
|
|
|
|
|
return GoogleAuth.GetAuthorizationURL();
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-11 16:28:01 +12:00
|
|
|
|
public bool GetAccessToken(string code)
|
2014-12-26 07:36:15 +13:00
|
|
|
|
{
|
2018-04-11 16:28:01 +12:00
|
|
|
|
return GoogleAuth.GetAccessToken(code);
|
2014-12-26 07:36:15 +13:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-11 16:30:09 +12:00
|
|
|
|
private string GetMetadata(string name, string parentID)
|
2014-07-01 20:33:33 +12:00
|
|
|
|
{
|
|
|
|
|
object metadata;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(parentID))
|
|
|
|
|
{
|
|
|
|
|
metadata = new
|
|
|
|
|
{
|
2018-04-11 16:30:09 +12:00
|
|
|
|
name = name,
|
2014-07-01 20:33:33 +12:00
|
|
|
|
parents = new[]
|
|
|
|
|
{
|
2018-04-30 08:29:59 +12:00
|
|
|
|
parentID
|
2014-07-01 20:33:33 +12:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
metadata = new
|
|
|
|
|
{
|
2018-04-11 16:30:09 +12:00
|
|
|
|
name = name
|
2014-07-01 20:33:33 +12:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-02 00:12:21 +13:00
|
|
|
|
return JsonConvert.SerializeObject(metadata);
|
2014-07-01 20:33:33 +12:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-11 16:30:09 +12:00
|
|
|
|
private void SetPermissions(string fileID, GoogleDrivePermissionRole role, GoogleDrivePermissionType type, bool allowFileDiscovery)
|
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
|
|
|
|
|
2018-04-11 16:30:09 +12:00
|
|
|
|
string url = string.Format("https://www.googleapis.com/drive/v3/files/{0}/permissions", fileID);
|
2014-03-18 23:27:22 +13:00
|
|
|
|
|
|
|
|
|
string json = JsonConvert.SerializeObject(new
|
|
|
|
|
{
|
|
|
|
|
role = role.ToString(),
|
|
|
|
|
type = type.ToString(),
|
2018-04-11 16:30:09 +12:00
|
|
|
|
allowFileDiscovery = allowFileDiscovery.ToString()
|
2014-03-18 23:27:22 +13:00
|
|
|
|
});
|
|
|
|
|
|
2018-10-18 05:06:06 +13:00
|
|
|
|
string response = SendRequest(HttpMethod.POST, url, json, UploadHelpers.ContentTypeJSON, null, GoogleAuth.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);
|
2018-04-11 16:30:09 +12:00
|
|
|
|
args.Add("fields", "nextPageToken,files(id,name,description)");
|
2014-07-01 22:18:07 +12:00
|
|
|
|
|
2018-04-11 16:30:09 +12:00
|
|
|
|
List<GoogleDriveFile> folders = new List<GoogleDriveFile>();
|
|
|
|
|
string pageToken = "";
|
2014-07-01 22:18:07 +12:00
|
|
|
|
|
2018-04-11 16:30:09 +12:00
|
|
|
|
// Make sure we get all the pages of results
|
|
|
|
|
do
|
2014-07-01 22:18:07 +12:00
|
|
|
|
{
|
2018-04-11 16:30:09 +12:00
|
|
|
|
args["pageToken"] = pageToken;
|
|
|
|
|
string response = SendRequest(HttpMethod.GET, "https://www.googleapis.com/drive/v3/files", args, GoogleAuth.GetAuthHeaders());
|
|
|
|
|
pageToken = "";
|
2014-07-01 22:18:07 +12:00
|
|
|
|
|
2018-04-11 16:30:09 +12:00
|
|
|
|
if (!string.IsNullOrEmpty(response))
|
2014-07-01 22:18:07 +12:00
|
|
|
|
{
|
2018-04-11 16:30:09 +12:00
|
|
|
|
GoogleDriveFileList fileList = JsonConvert.DeserializeObject<GoogleDriveFileList>(response);
|
|
|
|
|
|
|
|
|
|
if (fileList != null)
|
|
|
|
|
{
|
|
|
|
|
folders.AddRange(fileList.files);
|
|
|
|
|
pageToken = fileList.nextPageToken;
|
|
|
|
|
}
|
2014-07-01 22:18:07 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-11 16:30:09 +12:00
|
|
|
|
while (!string.IsNullOrEmpty(pageToken));
|
2014-07-01 22:18:07 +12:00
|
|
|
|
|
2018-04-11 16:30:09 +12:00
|
|
|
|
return folders;
|
2014-07-01 22:18:07 +12:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
2018-04-11 16:30:09 +12:00
|
|
|
|
UploadResult result = SendRequestFile("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields=id,webViewLink,webContentLink", stream, fileName,
|
|
|
|
|
headers: GoogleAuth.GetAuthHeaders(), contentType: "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)
|
|
|
|
|
{
|
2018-04-11 16:30:09 +12:00
|
|
|
|
SetPermissions(upload.id, GoogleDrivePermissionRole.reader, GoogleDrivePermissionType.anyone, false);
|
2014-03-18 23:27:22 +13:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-11 22:11:58 +12:00
|
|
|
|
if (DirectLink)
|
|
|
|
|
{
|
|
|
|
|
Uri webContentLink = new Uri(upload.webContentLink);
|
|
|
|
|
|
|
|
|
|
string leftPart = webContentLink.GetLeftPart(UriPartial.Path);
|
|
|
|
|
|
|
|
|
|
NameValueCollection queryString = HttpUtility.ParseQueryString(webContentLink.Query);
|
|
|
|
|
queryString.Remove("export");
|
|
|
|
|
|
|
|
|
|
result.URL = $"{leftPart}?{queryString}";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-04-11 16:30:09 +12:00
|
|
|
|
result.URL = upload.webViewLink;
|
2016-06-11 22:11:58 +12:00
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2016-09-17 19:07:02 +12:00
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2016-09-17 19:07:02 +12:00
|
|
|
|
public class GoogleDriveFile
|
|
|
|
|
{
|
|
|
|
|
public string id { get; set; }
|
2018-04-11 16:30:09 +12:00
|
|
|
|
public string webViewLink { get; set; }
|
2016-09-17 19:07:02 +12:00
|
|
|
|
public string webContentLink { get; set; }
|
2018-04-11 16:30:09 +12:00
|
|
|
|
public string name { get; set; }
|
2016-09-17 19:07:02 +12:00
|
|
|
|
public string description { get; set; }
|
|
|
|
|
}
|
2014-03-18 23:27:22 +13:00
|
|
|
|
|
2016-09-17 19:07:02 +12:00
|
|
|
|
public class GoogleDriveFileList
|
|
|
|
|
{
|
2018-04-11 16:30:09 +12:00
|
|
|
|
public List<GoogleDriveFile> files { get; set; }
|
|
|
|
|
public string nextPageToken { get; set; }
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|