mirror of
https://github.com/ShareX/ShareX.git
synced 2024-10-01 09:47:22 +13:00
Do it in one request :^)
This commit is contained in:
parent
997921e0e5
commit
e70858b90c
1 changed files with 35 additions and 38 deletions
|
@ -31,7 +31,6 @@ You should have received a copy of the GNU General Public License
|
|||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Web;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace ShareX.UploadersLib.FileUploaders
|
||||
|
@ -100,17 +99,23 @@ private string GetUploadPath(string fileName)
|
|||
return URLHelpers.CombineURL(path, fileName);
|
||||
}
|
||||
|
||||
public class ObjectACL
|
||||
{
|
||||
public string entity { get; set; }
|
||||
public string role { 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 string bucket { get; set; }
|
||||
public string domain { get; set; }
|
||||
public string prefix { get; set; }
|
||||
|
@ -119,45 +124,37 @@ public override UploadResult Upload(Stream stream, string fileName)
|
|||
{
|
||||
if (!CheckAuthorization()) return null;
|
||||
|
||||
UploadResult result = new UploadResult();
|
||||
|
||||
string contentType = Helpers.GetMimeType(fileName);
|
||||
string uploadpath = GetUploadPath(fileName);
|
||||
|
||||
Dictionary<string, string> args = new Dictionary<string, string>
|
||||
{
|
||||
{ "uploadType", "media" },
|
||||
{ "name", uploadpath }
|
||||
};
|
||||
|
||||
ObjectACL publicacl = new ObjectACL
|
||||
{
|
||||
entity = "allUsers",
|
||||
role = "READER"
|
||||
};
|
||||
|
||||
result.Response = SendRequest(HttpMethod.POST, $"https://www.googleapis.com/upload/storage/v1/b/{bucket}/o",
|
||||
stream, contentType, args, googleAuth.GetAuthHeaders());
|
||||
string responsename = JsonConvert.DeserializeObject<GoogleCloudStorageResponse>(result.Response).name;
|
||||
|
||||
if (responsename == uploadpath)
|
||||
{
|
||||
string encodeduploadpath = HttpUtility.UrlEncode(uploadpath);
|
||||
string requestjson = JsonConvert.SerializeObject(publicacl);
|
||||
SendRequest(HttpMethod.POST, $"https://www.googleapis.com/storage/v1/b/{bucket}/o/{encodeduploadpath}/acl",
|
||||
requestjson, ContentTypeJSON, headers: googleAuth.GetAuthHeaders());
|
||||
}
|
||||
else
|
||||
{
|
||||
Errors.Add("Upload to Google Cloud Storage failed.");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(domain))
|
||||
{
|
||||
domain = $"storage.googleapis.com/{bucket}";
|
||||
}
|
||||
|
||||
Dictionary<string, string> args = new Dictionary<string, string>
|
||||
{
|
||||
{ "uploadType", "multipart" }
|
||||
};
|
||||
|
||||
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", stream, fileName,
|
||||
headers: googleAuth.GetAuthHeaders(), contentType: "multipart/related", metadata: metadatajson, args: args);
|
||||
|
||||
result.URL = $"https://{domain}/{uploadpath}";
|
||||
|
||||
return result;
|
||||
|
|
Loading…
Reference in a new issue