2018-04-21 03:24:08 +12:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
|
|
|
|
Copyright (c) 2007-2018 ShareX Team
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
/* https://github.com/matthewburnett */
|
|
|
|
|
|
|
|
|
|
using Newtonsoft.Json;
|
2018-04-20 16:26:00 +12:00
|
|
|
|
using ShareX.HelpersLib;
|
2018-04-21 02:51:39 +12:00
|
|
|
|
using ShareX.UploadersLib.Properties;
|
2018-04-20 15:03:28 +12:00
|
|
|
|
using System.Collections.Generic;
|
2018-04-21 02:51:39 +12:00
|
|
|
|
using System.Drawing;
|
2018-04-20 09:30:56 +12:00
|
|
|
|
using System.IO;
|
2018-04-21 08:47:30 +12:00
|
|
|
|
using System.Web;
|
2018-04-21 03:20:12 +12:00
|
|
|
|
using System.Windows.Forms;
|
2018-04-20 09:30:56 +12:00
|
|
|
|
|
|
|
|
|
namespace ShareX.UploadersLib.FileUploaders
|
|
|
|
|
{
|
|
|
|
|
public class GoogleCloudStorageNewFileUploaderService : FileUploaderService
|
|
|
|
|
{
|
|
|
|
|
public override FileDestination EnumValue { get; } = FileDestination.GoogleCloudStorage;
|
|
|
|
|
|
2018-04-21 02:51:39 +12:00
|
|
|
|
public override Image ServiceImage => Resources.GoogleCloudStorage;
|
|
|
|
|
|
2018-04-20 09:30:56 +12:00
|
|
|
|
public override bool CheckConfig(UploadersConfig config)
|
|
|
|
|
{
|
2018-04-21 04:59:42 +12:00
|
|
|
|
return OAuth2Info.CheckOAuth(config.GoogleCloudStorageOAuth2Info) && !string.IsNullOrEmpty(config.GoogleCloudStorageBucket);
|
2018-04-20 09:30:56 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override GenericUploader CreateUploader(UploadersConfig config, TaskReferenceHelper taskInfo)
|
|
|
|
|
{
|
2018-04-20 17:33:58 +12:00
|
|
|
|
return new GoogleCloudStorage(config.GoogleCloudStorageOAuth2Info)
|
2018-04-20 09:30:56 +12:00
|
|
|
|
{
|
2018-04-21 04:59:42 +12:00
|
|
|
|
bucket = config.GoogleCloudStorageBucket,
|
2018-04-21 08:39:14 +12:00
|
|
|
|
domain = config.GoogleCloudStorageDomain,
|
|
|
|
|
prefix = config.GoogleCloudStorageObjectPrefix
|
2018-04-20 09:30:56 +12:00
|
|
|
|
};
|
|
|
|
|
}
|
2018-04-21 03:20:12 +12:00
|
|
|
|
|
|
|
|
|
public override TabPage GetUploadersConfigTabPage(UploadersConfigForm form) => form.tpGoogleCloudStorage;
|
2018-04-20 09:30:56 +12:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-20 16:26:00 +12:00
|
|
|
|
public sealed class GoogleCloudStorage : FileUploader, IOAuth2
|
2018-04-20 09:30:56 +12:00
|
|
|
|
{
|
2018-04-20 16:26:00 +12:00
|
|
|
|
public OAuth2Info AuthInfo => googleAuth.AuthInfo;
|
|
|
|
|
|
|
|
|
|
private GoogleOAuth2 googleAuth;
|
|
|
|
|
|
|
|
|
|
public GoogleCloudStorage(OAuth2Info oauth)
|
|
|
|
|
{
|
|
|
|
|
googleAuth = new GoogleOAuth2(oauth, this)
|
|
|
|
|
{
|
|
|
|
|
Scope = "https://www.googleapis.com/auth/devstorage.full_control"
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool RefreshAccessToken()
|
|
|
|
|
{
|
|
|
|
|
return googleAuth.RefreshAccessToken();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool CheckAuthorization()
|
|
|
|
|
{
|
|
|
|
|
return googleAuth.CheckAuthorization();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetAuthorizationURL()
|
|
|
|
|
{
|
|
|
|
|
return googleAuth.GetAuthorizationURL();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool GetAccessToken(string code)
|
|
|
|
|
{
|
|
|
|
|
return googleAuth.GetAccessToken(code);
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-21 08:39:14 +12:00
|
|
|
|
private string GetUploadPath(string fileName)
|
|
|
|
|
{
|
|
|
|
|
string path = NameParser.Parse(NameParserType.FolderPath, prefix.Trim('/'));
|
|
|
|
|
return URLHelpers.CombineURL(path, fileName);
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-20 16:26:00 +12:00
|
|
|
|
public class ObjectACL
|
|
|
|
|
{
|
|
|
|
|
public string entity { get; set; }
|
|
|
|
|
public string role { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-21 02:51:39 +12:00
|
|
|
|
public class GoogleCloudStorageResponse
|
|
|
|
|
{
|
|
|
|
|
public string name { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-20 09:48:59 +12:00
|
|
|
|
public string bucket { get; set; }
|
2018-04-21 04:59:42 +12:00
|
|
|
|
public string domain { get; set; }
|
2018-04-21 08:39:14 +12:00
|
|
|
|
public string prefix { get; set; }
|
2018-04-20 09:30:56 +12:00
|
|
|
|
|
|
|
|
|
public override UploadResult Upload(Stream stream, string fileName)
|
|
|
|
|
{
|
2018-04-20 16:26:00 +12:00
|
|
|
|
if (!CheckAuthorization()) return null;
|
|
|
|
|
|
2018-04-21 02:51:39 +12:00
|
|
|
|
UploadResult result = new UploadResult();
|
|
|
|
|
|
2018-04-20 15:03:28 +12:00
|
|
|
|
string contentType = Helpers.GetMimeType(fileName);
|
2018-04-21 08:39:14 +12:00
|
|
|
|
string uploadpath = GetUploadPath(fileName);
|
|
|
|
|
|
2018-04-20 16:26:00 +12:00
|
|
|
|
Dictionary<string, string> args = new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{ "uploadType", "media" },
|
2018-04-21 08:39:14 +12:00
|
|
|
|
{ "name", uploadpath }
|
2018-04-20 16:26:00 +12:00
|
|
|
|
};
|
2018-04-20 15:03:28 +12:00
|
|
|
|
|
2018-04-20 17:33:58 +12:00
|
|
|
|
ObjectACL publicacl = new ObjectACL
|
2018-04-20 15:03:28 +12:00
|
|
|
|
{
|
2018-04-20 16:26:00 +12:00
|
|
|
|
entity = "allUsers",
|
|
|
|
|
role = "READER"
|
2018-04-20 15:03:28 +12:00
|
|
|
|
};
|
|
|
|
|
|
2018-04-21 08:39:14 +12:00
|
|
|
|
result.Response = SendRequest(HttpMethod.POST, $"https://www.googleapis.com/upload/storage/v1/b/{bucket}/o",
|
|
|
|
|
stream, contentType, args, googleAuth.GetAuthHeaders());
|
2018-04-21 02:51:39 +12:00
|
|
|
|
string responsename = JsonConvert.DeserializeObject<GoogleCloudStorageResponse>(result.Response).name;
|
2018-04-20 09:30:56 +12:00
|
|
|
|
|
2018-04-21 08:39:14 +12:00
|
|
|
|
if (responsename == uploadpath)
|
2018-04-20 15:03:28 +12:00
|
|
|
|
{
|
2018-04-21 08:47:30 +12:00
|
|
|
|
string encodeduploadpath = HttpUtility.UrlEncode(uploadpath);
|
2018-04-20 17:33:58 +12:00
|
|
|
|
string requestjson = JsonConvert.SerializeObject(publicacl);
|
2018-04-21 08:47:30 +12:00
|
|
|
|
SendRequest(HttpMethod.POST, $"https://www.googleapis.com/storage/v1/b/{bucket}/o/{encodeduploadpath}/acl",
|
2018-04-21 08:39:14 +12:00
|
|
|
|
requestjson, ContentTypeJSON, headers: googleAuth.GetAuthHeaders());
|
2018-04-20 15:03:28 +12:00
|
|
|
|
}
|
2018-04-21 09:16:46 +12:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Errors.Add("Upload to Google Cloud Storage failed.");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2018-04-20 15:03:28 +12:00
|
|
|
|
|
2018-04-21 04:59:42 +12:00
|
|
|
|
if (string.IsNullOrEmpty(domain))
|
|
|
|
|
{
|
2018-04-21 09:16:46 +12:00
|
|
|
|
domain = $"storage.googleapis.com/{bucket}";
|
2018-04-21 04:59:42 +12:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-21 08:39:14 +12:00
|
|
|
|
result.URL = $"https://{domain}/{uploadpath}";
|
2018-04-21 04:59:42 +12:00
|
|
|
|
|
2018-04-21 02:51:39 +12:00
|
|
|
|
return result;
|
2018-04-20 09:30:56 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|