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
|
2023-01-10 09:31:02 +13:00
|
|
|
|
Copyright (c) 2007-2023 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,
|
2020-06-08 15:56:14 +12:00
|
|
|
|
FolderID = config.GoogleDriveUseFolder ? config.GoogleDriveFolderID : null,
|
|
|
|
|
DriveID = config.GoogleDriveSelectedDrive?.id
|
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
|
|
|
|
|
{
|
2022-12-11 00:19:34 +13:00
|
|
|
|
public GoogleOAuth2 OAuth2 { get; private set; }
|
|
|
|
|
public OAuth2Info AuthInfo => OAuth2.AuthInfo;
|
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; }
|
2020-06-08 15:56:14 +12:00
|
|
|
|
public string DriveID { get; set; }
|
|
|
|
|
|
|
|
|
|
public static GoogleDriveSharedDrive MyDrive = new GoogleDriveSharedDrive
|
|
|
|
|
{
|
|
|
|
|
id = "", // empty defaults to user drive
|
|
|
|
|
name = Resources.GoogleDrive_MyDrive_My_drive
|
|
|
|
|
};
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
public GoogleDrive(OAuth2Info oauth)
|
|
|
|
|
{
|
2022-12-11 00:19:34 +13:00
|
|
|
|
OAuth2 = new GoogleOAuth2(oauth, this)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2022-12-11 00:19:34 +13:00
|
|
|
|
Scope = "https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/userinfo.profile"
|
2018-04-11 16:28:01 +12:00
|
|
|
|
};
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool RefreshAccessToken()
|
|
|
|
|
{
|
2022-12-11 00:19:34 +13:00
|
|
|
|
return OAuth2.RefreshAccessToken();
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool CheckAuthorization()
|
|
|
|
|
{
|
2022-12-11 00:19:34 +13:00
|
|
|
|
return OAuth2.CheckAuthorization();
|
2018-04-11 16:28:01 +12:00
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2018-04-11 16:28:01 +12:00
|
|
|
|
public string GetAuthorizationURL()
|
|
|
|
|
{
|
2022-12-11 00:19:34 +13:00
|
|
|
|
return OAuth2.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
|
|
|
|
{
|
2022-12-11 00:19:34 +13:00
|
|
|
|
return OAuth2.GetAccessToken(code);
|
2014-12-26 07:36:15 +13:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-08 15:56:14 +12:00
|
|
|
|
private string GetMetadata(string name, string parentID, string driveID = "")
|
2014-07-01 20:33:33 +12:00
|
|
|
|
{
|
|
|
|
|
object metadata;
|
|
|
|
|
|
2020-06-08 15:56:14 +12:00
|
|
|
|
// If there's no parent folder, the drive behaves as parent
|
|
|
|
|
if (string.IsNullOrEmpty(parentID))
|
|
|
|
|
{
|
|
|
|
|
parentID = driveID;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-01 20:33:33 +12:00
|
|
|
|
if (!string.IsNullOrEmpty(parentID))
|
|
|
|
|
{
|
|
|
|
|
metadata = new
|
|
|
|
|
{
|
2018-04-11 16:30:09 +12:00
|
|
|
|
name = name,
|
2020-06-08 15:56:14 +12:00
|
|
|
|
driveId = driveID,
|
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
|
|
|
|
|
2020-07-06 11:23:59 +12:00
|
|
|
|
string url = string.Format("https://www.googleapis.com/drive/v3/files/{0}/permissions?supportsAllDrives=true", 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
|
|
|
|
});
|
|
|
|
|
|
2022-12-11 00:19:34 +13:00
|
|
|
|
SendRequest(HttpMethod.POST, url, json, RequestHelpers.ContentTypeJSON, null, OAuth2.GetAuthHeaders());
|
2014-03-18 23:27:22 +13:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-08 15:56:14 +12:00
|
|
|
|
public List<GoogleDriveFile> GetFolders(string driveID = "", 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";
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-08 15:56:14 +12:00
|
|
|
|
if (writer && string.IsNullOrEmpty(driveID))
|
2014-07-02 04:40:40 +12:00
|
|
|
|
{
|
|
|
|
|
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)");
|
2020-06-08 15:56:14 +12:00
|
|
|
|
if (!string.IsNullOrEmpty(driveID))
|
|
|
|
|
{
|
|
|
|
|
args.Add("driveId", driveID);
|
|
|
|
|
args.Add("corpora", "drive");
|
|
|
|
|
args.Add("supportsAllDrives", "true");
|
|
|
|
|
args.Add("includeItemsFromAllDrives", "true");
|
|
|
|
|
}
|
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;
|
2022-12-11 00:19:34 +13:00
|
|
|
|
string response = SendRequest(HttpMethod.GET, "https://www.googleapis.com/drive/v3/files", args, OAuth2.GetAuthHeaders());
|
2018-04-11 16:30:09 +12:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2020-06-08 15:56:14 +12:00
|
|
|
|
public List<GoogleDriveSharedDrive> GetDrives()
|
|
|
|
|
{
|
|
|
|
|
if (!CheckAuthorization()) return null;
|
|
|
|
|
|
|
|
|
|
Dictionary<string, string> args = new Dictionary<string, string>();
|
|
|
|
|
List<GoogleDriveSharedDrive> drives = new List<GoogleDriveSharedDrive>();
|
|
|
|
|
string pageToken = "";
|
|
|
|
|
|
|
|
|
|
// Make sure we get all the pages of results
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
args["pageToken"] = pageToken;
|
2022-12-11 00:19:34 +13:00
|
|
|
|
string response = SendRequest(HttpMethod.GET, "https://www.googleapis.com/drive/v3/drives", args, OAuth2.GetAuthHeaders());
|
2020-06-08 15:56:14 +12:00
|
|
|
|
pageToken = "";
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(response))
|
|
|
|
|
{
|
|
|
|
|
GoogleDriveSharedDriveList driveList = JsonConvert.DeserializeObject<GoogleDriveSharedDriveList>(response);
|
|
|
|
|
|
|
|
|
|
if (driveList != null)
|
|
|
|
|
{
|
|
|
|
|
drives.AddRange(driveList.drives);
|
|
|
|
|
pageToken = driveList.nextPageToken;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
while (!string.IsNullOrEmpty(pageToken));
|
|
|
|
|
|
|
|
|
|
return drives;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
2020-06-08 15:56:14 +12:00
|
|
|
|
string metadata = GetMetadata(fileName, FolderID, DriveID);
|
|
|
|
|
|
2020-06-11 10:10:10 +12:00
|
|
|
|
UploadResult result = SendRequestFile("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields=id,webViewLink,webContentLink&supportsAllDrives=true",
|
2022-12-11 00:19:34 +13:00
|
|
|
|
stream, fileName, "file", headers: OAuth2.GetAuthHeaders(), contentType: "multipart/related", relatedData: 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
|
|
|
|
}
|
2020-06-08 15:56:14 +12:00
|
|
|
|
|
|
|
|
|
public class GoogleDriveSharedDrive
|
|
|
|
|
{
|
|
|
|
|
public string id { get; set; }
|
|
|
|
|
public string name { get; set; }
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class GoogleDriveSharedDriveList
|
|
|
|
|
{
|
|
|
|
|
public List<GoogleDriveSharedDrive> drives { get; set; }
|
|
|
|
|
public string nextPageToken { get; set; }
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|