diff --git a/ShareX.UploadersLib/BaseUploaders/Uploader.cs b/ShareX.UploadersLib/BaseUploaders/Uploader.cs index 4a3959fc5..450a173f2 100644 --- a/ShareX.UploadersLib/BaseUploaders/Uploader.cs +++ b/ShareX.UploadersLib/BaseUploaders/Uploader.cs @@ -159,7 +159,7 @@ public virtual void StopUpload() protected NameValueCollection SendRequestGetHeaders(HttpMethod method, string url, Stream data, string contentType, Dictionary args, NameValueCollection headers = null, CookieCollection cookies = null) { - using (HttpWebResponse response = GetResponse(method, url, data, contentType, null, headers, cookies)) + using (HttpWebResponse response = GetResponse(method, url, data, contentType, args, headers, cookies)) { if (response != null) { diff --git a/ShareX.UploadersLib/FileUploaders/GoogleCloudStorage.cs b/ShareX.UploadersLib/FileUploaders/GoogleCloudStorage.cs index b81394ab3..9eca8087c 100644 --- a/ShareX.UploadersLib/FileUploaders/GoogleCloudStorage.cs +++ b/ShareX.UploadersLib/FileUploaders/GoogleCloudStorage.cs @@ -1,5 +1,9 @@ -using System.Collections.Generic; +using ShareX.HelpersLib; +using System.Collections.Generic; +using System.Collections.Specialized; +using System.Diagnostics; using System.IO; +using System.Linq; namespace ShareX.UploadersLib.FileUploaders { @@ -16,25 +20,47 @@ public override GenericUploader CreateUploader(UploadersConfig config, TaskRefer { return new GoogleCloudStorage { - APIKey = "null" + token = "", + bucket = "cdn.riolu.com" }; } } public sealed class GoogleCloudStorage : FileUploader { - public string APIKey { get; set; } + public string token { get; set; } public string bucket { get; set; } public override UploadResult Upload(Stream stream, string fileName) { string uploadurl = $"https://www.googleapis.com/upload/storage/v1/b/{bucket}/o"; - string objecturl = $"https://www.googleapis.com/upload/storage/v1/b/{bucket}/o/{fileName}/acl"; + string publicurl = $"https://storage.googleapis.com/{bucket}/{fileName}"; + + string contentType = Helpers.GetMimeType(fileName); Dictionary args = new Dictionary(); - args.Add("key", APIKey); + args.Add("uploadType", "media"); + args.Add("name", fileName); - return null; + NameValueCollection headers = new NameValueCollection + { + ["Content-Length"] = stream.Length.ToString(), + ["Authorization"] = "Bearer " + token + }; + + NameValueCollection responseHeaders = SendRequestGetHeaders(HttpMethod.POST, uploadurl, stream, contentType, args, headers); + + if (responseHeaders == null) + { + Errors.Add("Upload to Google Cloud Storage failed."); + return null; + } + + return new UploadResult + { + IsSuccess = true, + URL = publicurl + }; } } } \ No newline at end of file