Better request method

This commit is contained in:
Matthew Burnett 2018-04-20 10:51:39 -04:00
parent d26b0eb4bc
commit d8be3a041d

View file

@ -1,7 +1,9 @@
using Newtonsoft.Json;
using ShareX.HelpersLib;
using ShareX.UploadersLib.Properties;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Drawing;
using System.IO;
namespace ShareX.UploadersLib.FileUploaders
@ -10,6 +12,8 @@ public class GoogleCloudStorageNewFileUploaderService : FileUploaderService
{
public override FileDestination EnumValue { get; } = FileDestination.GoogleCloudStorage;
public override Image ServiceImage => Resources.GoogleCloudStorage;
public override bool CheckConfig(UploadersConfig config)
{
return OAuth2Info.CheckOAuth(config.GoogleCloudStorageOAuth2Info);
@ -64,20 +68,22 @@ public class ObjectACL
public string role { get; set; }
}
public class GoogleCloudStorageResponse
{
public string name { 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";
}
UploadResult result = new UploadResult();
result.URL = $"https://storage.googleapis.com/{bucket}/{fileName}";
string uploadurl = $"https://www.googleapis.com/upload/storage/v1/b/{bucket}/o";
string publicurl = $"https://{domain}/{bucket}/{fileName}";
string aclurl = $"https://www.googleapis.com/storage/v1/b/{bucket}/o/{fileName}/acl";
string contentType = Helpers.GetMimeType(fileName);
@ -97,19 +103,16 @@ public override UploadResult Upload(Stream stream, string fileName)
NameValueCollection headers = googleAuth.GetAuthHeaders();
headers.Add("Content-Length", stream.Length.ToString());
NameValueCollection responseHeaders = SendRequestGetHeaders(HttpMethod.POST, uploadurl, stream, contentType, args, headers);
result.Response = SendRequest(HttpMethod.POST, uploadurl, stream, contentType, args, headers);
string responsename = JsonConvert.DeserializeObject<GoogleCloudStorageResponse>(result.Response).name;
if (responseHeaders != null)
if (responsename == fileName)
{
string requestjson = JsonConvert.SerializeObject(publicacl);
SendRequest(HttpMethod.POST, aclurl, requestjson, ContentTypeJSON, headers: googleAuth.GetAuthHeaders());
}
return new UploadResult
{
IsSuccess = true,
URL = publicurl
};
return result;
}
}
}