2018-04-20 16:26:00 +12:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
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;
|
|
|
|
|
using System.Collections.Specialized;
|
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 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-20 16:26:00 +12:00
|
|
|
|
return OAuth2Info.CheckOAuth(config.GoogleCloudStorageOAuth2Info);
|
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-20 16:26:00 +12:00
|
|
|
|
bucket = config.GoogleCloudStorageBucket
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-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();
|
|
|
|
|
|
|
|
|
|
result.URL = $"https://storage.googleapis.com/{bucket}/{fileName}";
|
2018-04-20 16:26:00 +12:00
|
|
|
|
|
2018-04-20 09:48:59 +12:00
|
|
|
|
string uploadurl = $"https://www.googleapis.com/upload/storage/v1/b/{bucket}/o";
|
2018-04-20 16:26:00 +12:00
|
|
|
|
string aclurl = $"https://www.googleapis.com/storage/v1/b/{bucket}/o/{fileName}/acl";
|
2018-04-20 15:03:28 +12:00
|
|
|
|
|
|
|
|
|
string contentType = Helpers.GetMimeType(fileName);
|
2018-04-20 09:48:59 +12:00
|
|
|
|
|
2018-04-20 16:26:00 +12:00
|
|
|
|
Dictionary<string, string> args = new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{ "uploadType", "media" },
|
|
|
|
|
{ "name", fileName }
|
|
|
|
|
};
|
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-20 16:26:00 +12:00
|
|
|
|
NameValueCollection headers = googleAuth.GetAuthHeaders();
|
|
|
|
|
headers.Add("Content-Length", stream.Length.ToString());
|
|
|
|
|
|
2018-04-21 02:51:39 +12:00
|
|
|
|
result.Response = SendRequest(HttpMethod.POST, uploadurl, stream, contentType, args, headers);
|
|
|
|
|
string responsename = JsonConvert.DeserializeObject<GoogleCloudStorageResponse>(result.Response).name;
|
2018-04-20 09:30:56 +12:00
|
|
|
|
|
2018-04-21 02:51:39 +12:00
|
|
|
|
if (responsename == fileName)
|
2018-04-20 15:03:28 +12:00
|
|
|
|
{
|
2018-04-20 17:33:58 +12:00
|
|
|
|
string requestjson = JsonConvert.SerializeObject(publicacl);
|
2018-04-20 16:26:00 +12:00
|
|
|
|
SendRequest(HttpMethod.POST, aclurl, requestjson, ContentTypeJSON, headers: googleAuth.GetAuthHeaders());
|
2018-04-20 15:03:28 +12:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-21 02:51:39 +12:00
|
|
|
|
return result;
|
2018-04-20 09:30:56 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|