mirror of
https://github.com/ShareX/ShareX.git
synced 2024-07-05 06:30:36 +12:00
Merge pull request #3337 from matthewburnett/gcs-improvements
Google Cloud Storage: Add Live Preview URL
This commit is contained in:
commit
b1570ba77b
|
@ -64,6 +64,59 @@ public sealed class GoogleCloudStorage : FileUploader, IOAuth2
|
|||
public string Domain { get; set; }
|
||||
public string Prefix { get; set; }
|
||||
|
||||
public class GoogleCloudStorageResponse
|
||||
{
|
||||
public string name { get; set; }
|
||||
}
|
||||
|
||||
public class Metadata
|
||||
{
|
||||
public string name { get; set; }
|
||||
public Acl[] acl { get; set; }
|
||||
}
|
||||
|
||||
public class Acl
|
||||
{
|
||||
public string entity { get; set; }
|
||||
public string role { get; set; }
|
||||
}
|
||||
|
||||
public override UploadResult Upload(Stream stream, string fileName)
|
||||
{
|
||||
if (!CheckAuthorization()) return null;
|
||||
|
||||
string uploadpath = GetUploadPath(fileName);
|
||||
|
||||
Metadata metadata = new Metadata
|
||||
{
|
||||
name = uploadpath,
|
||||
acl = new Acl[]
|
||||
{
|
||||
new Acl
|
||||
{
|
||||
entity = "allUsers",
|
||||
role = "READER"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
string metadatajson = JsonConvert.SerializeObject(metadata);
|
||||
|
||||
UploadResult result = SendRequestFile($"https://www.googleapis.com/upload/storage/v1/b/{Bucket}/o?uploadType=multipart", stream, fileName,
|
||||
headers: googleAuth.GetAuthHeaders(), contentType: "multipart/related", metadata: metadatajson);
|
||||
GoogleCloudStorageResponse upload = JsonConvert.DeserializeObject<GoogleCloudStorageResponse>(result.Response);
|
||||
|
||||
if (upload.name != uploadpath)
|
||||
{
|
||||
Errors.Add("Upload failed.");
|
||||
return null;
|
||||
}
|
||||
|
||||
result.URL = GenerateURL(uploadpath);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public OAuth2Info AuthInfo => googleAuth.AuthInfo;
|
||||
|
||||
private GoogleOAuth2 googleAuth;
|
||||
|
@ -102,62 +155,29 @@ private string GetUploadPath(string fileName)
|
|||
return URLHelpers.CombineURL(path, fileName);
|
||||
}
|
||||
|
||||
public class GoogleCloudStorageResponse
|
||||
public string GenerateURL(string uploadPath)
|
||||
{
|
||||
public string name { get; set; }
|
||||
}
|
||||
|
||||
public class Metadata
|
||||
{
|
||||
public string name { get; set; }
|
||||
public Acl[] acl { get; set; }
|
||||
}
|
||||
|
||||
public class Acl
|
||||
{
|
||||
public string entity { get; set; }
|
||||
public string role { get; set; }
|
||||
}
|
||||
|
||||
public override UploadResult Upload(Stream stream, string fileName)
|
||||
{
|
||||
if (!CheckAuthorization()) return null;
|
||||
|
||||
string uploadpath = GetUploadPath(fileName);
|
||||
if (string.IsNullOrEmpty(Bucket))
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(Domain))
|
||||
{
|
||||
Domain = $"storage.googleapis.com/{Bucket}";
|
||||
}
|
||||
|
||||
Metadata metadata = new Metadata
|
||||
{
|
||||
name = uploadpath,
|
||||
acl = new Acl[]
|
||||
{
|
||||
new Acl
|
||||
{
|
||||
entity = "allUsers",
|
||||
role = "READER"
|
||||
}
|
||||
}
|
||||
};
|
||||
uploadPath = URLHelpers.URLPathEncode(uploadPath);
|
||||
|
||||
string metadatajson = JsonConvert.SerializeObject(metadata);
|
||||
string url = URLHelpers.CombineURL(Domain, uploadPath);
|
||||
|
||||
UploadResult result = SendRequestFile($"https://www.googleapis.com/upload/storage/v1/b/{Bucket}/o?uploadType=multipart", stream, fileName,
|
||||
headers: googleAuth.GetAuthHeaders(), contentType: "multipart/related", metadata: metadatajson);
|
||||
GoogleCloudStorageResponse upload = JsonConvert.DeserializeObject<GoogleCloudStorageResponse>(result.Response);
|
||||
return URLHelpers.FixPrefix(url, "https://");
|
||||
}
|
||||
|
||||
if (upload.name != uploadpath)
|
||||
{
|
||||
Errors.Add("Upload failed.");
|
||||
return null;
|
||||
}
|
||||
|
||||
result.URL = URLHelpers.FixPrefix($"{Domain}/{URLHelpers.URLPathEncode(uploadpath)}", "https://");
|
||||
|
||||
return result;
|
||||
public string GetPreviewURL()
|
||||
{
|
||||
string uploadPath = GetUploadPath("example.png");
|
||||
return GenerateURL(uploadPath);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -303,6 +303,8 @@ private void InitializeComponent()
|
|||
this.lblAmazonS3AccessKey = new System.Windows.Forms.Label();
|
||||
this.txtAmazonS3AccessKey = new System.Windows.Forms.TextBox();
|
||||
this.tpGoogleCloudStorage = new System.Windows.Forms.TabPage();
|
||||
this.lblGoogleCloudStoragePathPreview = new System.Windows.Forms.Label();
|
||||
this.lblGoogleCloudStoragePathPreviewLabel = new System.Windows.Forms.Label();
|
||||
this.txtGoogleCloudStorageObjectPrefix = new System.Windows.Forms.TextBox();
|
||||
this.lblGoogleCloudStorageObjectPrefix = new System.Windows.Forms.Label();
|
||||
this.lblGoogleCloudStorageDomain = new System.Windows.Forms.Label();
|
||||
|
@ -2790,6 +2792,8 @@ private void InitializeComponent()
|
|||
//
|
||||
// tpGoogleCloudStorage
|
||||
//
|
||||
this.tpGoogleCloudStorage.Controls.Add(this.lblGoogleCloudStoragePathPreview);
|
||||
this.tpGoogleCloudStorage.Controls.Add(this.lblGoogleCloudStoragePathPreviewLabel);
|
||||
this.tpGoogleCloudStorage.Controls.Add(this.txtGoogleCloudStorageObjectPrefix);
|
||||
this.tpGoogleCloudStorage.Controls.Add(this.lblGoogleCloudStorageObjectPrefix);
|
||||
this.tpGoogleCloudStorage.Controls.Add(this.lblGoogleCloudStorageDomain);
|
||||
|
@ -2801,6 +2805,16 @@ private void InitializeComponent()
|
|||
this.tpGoogleCloudStorage.Name = "tpGoogleCloudStorage";
|
||||
this.tpGoogleCloudStorage.UseVisualStyleBackColor = true;
|
||||
//
|
||||
// lblGoogleCloudStoragePathPreview
|
||||
//
|
||||
resources.ApplyResources(this.lblGoogleCloudStoragePathPreview, "lblGoogleCloudStoragePathPreview");
|
||||
this.lblGoogleCloudStoragePathPreview.Name = "lblGoogleCloudStoragePathPreview";
|
||||
//
|
||||
// lblGoogleCloudStoragePathPreviewLabel
|
||||
//
|
||||
resources.ApplyResources(this.lblGoogleCloudStoragePathPreviewLabel, "lblGoogleCloudStoragePathPreviewLabel");
|
||||
this.lblGoogleCloudStoragePathPreviewLabel.Name = "lblGoogleCloudStoragePathPreviewLabel";
|
||||
//
|
||||
// txtGoogleCloudStorageObjectPrefix
|
||||
//
|
||||
resources.ApplyResources(this.txtGoogleCloudStorageObjectPrefix, "txtGoogleCloudStorageObjectPrefix");
|
||||
|
@ -6097,5 +6111,7 @@ private void InitializeComponent()
|
|||
private System.Windows.Forms.Button btnSharedFolderAdd;
|
||||
private System.Windows.Forms.PropertyGrid pgSharedFolderAccount;
|
||||
private System.Windows.Forms.ListBox lbSharedFolderAccounts;
|
||||
private System.Windows.Forms.Label lblGoogleCloudStoragePathPreviewLabel;
|
||||
private System.Windows.Forms.Label lblGoogleCloudStoragePathPreview;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3033,16 +3033,19 @@ private void oauth2GoogleCloudStorage_RefreshButtonClicked()
|
|||
private void txtGoogleCloudStorageBucket_TextChanged(object sender, EventArgs e)
|
||||
{
|
||||
Config.GoogleCloudStorageBucket = txtGoogleCloudStorageBucket.Text;
|
||||
UpdateGoogleCloudStorageStatus();
|
||||
}
|
||||
|
||||
private void txtGoogleCloudStorageDomain_TextChanged(object sender, EventArgs e)
|
||||
{
|
||||
Config.GoogleCloudStorageDomain = txtGoogleCloudStorageDomain.Text;
|
||||
UpdateGoogleCloudStorageStatus();
|
||||
}
|
||||
|
||||
private void txtGoogleCloudStorageObjectPrefix_TextChanged(object sender, EventArgs e)
|
||||
{
|
||||
Config.GoogleCloudStorageObjectPrefix = txtGoogleCloudStorageObjectPrefix.Text;
|
||||
UpdateGoogleCloudStorageStatus();
|
||||
}
|
||||
|
||||
#endregion Google Cloud Storage
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -471,6 +471,22 @@ private void UpdateAmazonS3Status()
|
|||
|
||||
#endregion Amazon S3
|
||||
|
||||
#region Google Cloud Storage
|
||||
|
||||
private void UpdateGoogleCloudStorageStatus()
|
||||
{
|
||||
GoogleCloudStorage GCS = new GoogleCloudStorage(Config.GoogleCloudStorageOAuth2Info)
|
||||
{
|
||||
Bucket = Config.GoogleCloudStorageBucket,
|
||||
Domain = Config.GoogleCloudStorageDomain,
|
||||
Prefix = Config.GoogleCloudStorageObjectPrefix
|
||||
};
|
||||
|
||||
lblGoogleCloudStoragePathPreview.Text = GCS.GetPreviewURL();
|
||||
}
|
||||
|
||||
#endregion Google Cloud Storage
|
||||
|
||||
#region Google Drive
|
||||
|
||||
public void GoogleDriveAuthOpen()
|
||||
|
|
Loading…
Reference in a new issue