From debb4e5cce114f14c1d250d5c6c00c8849ca25b1 Mon Sep 17 00:00:00 2001 From: Matthew Burnett Date: Fri, 20 Apr 2018 00:26:00 -0400 Subject: [PATCH] More progress OAuth, public read, custom domain support --- .../FileUploaders/GoogleCloudStorage.cs | 89 ++++++++++++++----- ShareX.UploadersLib/UploadersConfig.cs | 7 ++ 2 files changed, 76 insertions(+), 20 deletions(-) diff --git a/ShareX.UploadersLib/FileUploaders/GoogleCloudStorage.cs b/ShareX.UploadersLib/FileUploaders/GoogleCloudStorage.cs index 9eca8087c..7ad520d28 100644 --- a/ShareX.UploadersLib/FileUploaders/GoogleCloudStorage.cs +++ b/ShareX.UploadersLib/FileUploaders/GoogleCloudStorage.cs @@ -1,9 +1,8 @@ -using ShareX.HelpersLib; +using Newtonsoft.Json; +using ShareX.HelpersLib; using System.Collections.Generic; using System.Collections.Specialized; -using System.Diagnostics; using System.IO; -using System.Linq; namespace ShareX.UploadersLib.FileUploaders { @@ -13,47 +12,97 @@ public class GoogleCloudStorageNewFileUploaderService : FileUploaderService public override bool CheckConfig(UploadersConfig config) { - return true; + return OAuth2Info.CheckOAuth(config.GoogleCloudStorageOAuth2Info); } public override GenericUploader CreateUploader(UploadersConfig config, TaskReferenceHelper taskInfo) { - return new GoogleCloudStorage + return new GoogleCloudStorage(config.YouTubeOAuth2Info) { - token = "", - bucket = "cdn.riolu.com" + bucket = config.GoogleCloudStorageBucket }; } } - public sealed class GoogleCloudStorage : FileUploader + public sealed class GoogleCloudStorage : FileUploader, IOAuth2 { - public string token { get; set; } + 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); + } + + public class ObjectACL + { + public string entity { get; set; } + public string role { get; set; } + } + public string bucket { get; set; } + public string domain { get; set; } public override UploadResult Upload(Stream stream, string fileName) { + if (!CheckAuthorization()) return null; + + if (string.IsNullOrEmpty(domain)) + { + domain = "storage.googleapis.com"; + } + string uploadurl = $"https://www.googleapis.com/upload/storage/v1/b/{bucket}/o"; - string publicurl = $"https://storage.googleapis.com/{bucket}/{fileName}"; + string publicurl = $"https://{domain}/{bucket}/{fileName}"; + string aclurl = $"https://www.googleapis.com/storage/v1/b/{bucket}/o/{fileName}/acl"; string contentType = Helpers.GetMimeType(fileName); - Dictionary args = new Dictionary(); - args.Add("uploadType", "media"); - args.Add("name", fileName); - - NameValueCollection headers = new NameValueCollection + Dictionary args = new Dictionary { - ["Content-Length"] = stream.Length.ToString(), - ["Authorization"] = "Bearer " + token + { "uploadType", "media" }, + { "name", fileName } }; + ObjectACL requestacl = new ObjectACL + { + entity = "allUsers", + role = "READER" + }; + + NameValueCollection headers = googleAuth.GetAuthHeaders(); + headers.Add("Content-Length", stream.Length.ToString()); + NameValueCollection responseHeaders = SendRequestGetHeaders(HttpMethod.POST, uploadurl, stream, contentType, args, headers); - if (responseHeaders == null) + if (responseHeaders != null) { - Errors.Add("Upload to Google Cloud Storage failed."); - return null; + string requestjson = JsonConvert.SerializeObject(requestacl); + SendRequest(HttpMethod.POST, aclurl, requestjson, ContentTypeJSON, headers: googleAuth.GetAuthHeaders()); } return new UploadResult diff --git a/ShareX.UploadersLib/UploadersConfig.cs b/ShareX.UploadersLib/UploadersConfig.cs index 8eae5233c..1f71cad9a 100644 --- a/ShareX.UploadersLib/UploadersConfig.cs +++ b/ShareX.UploadersLib/UploadersConfig.cs @@ -389,6 +389,13 @@ public class UploadersConfig : SettingsBase #endregion YouTube + #region Google Cloud Storage + + public OAuth2Info GoogleCloudStorageOAuth2Info = null; + public string GoogleCloudStorageBucket = ""; + + #endregion Google Cloud Storage + #endregion File uploaders #region URL shorteners