mirror of
https://github.com/ShareX/ShareX.git
synced 2024-10-02 10:16:19 +13:00
#404 - Use multipart/related for GDriveSDK uploads (only sets the title metadata.)
This commit is contained in:
parent
e3b745e843
commit
3395ff0af1
2 changed files with 39 additions and 12 deletions
|
@ -135,10 +135,8 @@ private NameValueCollection GetAuthHeaders()
|
|||
return headers;
|
||||
}
|
||||
|
||||
private void SetMetadata(string fileID, string title, string parentID = null)
|
||||
private string GetMetadata(string title, string parentID = null)
|
||||
{
|
||||
string url = string.Format("https://www.googleapis.com/drive/v2/files/{0}", fileID);
|
||||
|
||||
object metadata;
|
||||
|
||||
if (!string.IsNullOrEmpty(parentID))
|
||||
|
@ -165,7 +163,7 @@ private void SetMetadata(string fileID, string title, string parentID = null)
|
|||
|
||||
string json = JsonConvert.SerializeObject(metadata);
|
||||
|
||||
string response = SendRequestJSON(url, json, GetAuthHeaders(), method: HttpMethod.PUT);
|
||||
return json;
|
||||
}
|
||||
|
||||
private void SetPermissions(string fileID, GoogleDrivePermissionRole role, GoogleDrivePermissionType type, string value, bool withLink)
|
||||
|
@ -221,7 +219,9 @@ public override UploadResult Upload(Stream stream, string fileName)
|
|||
{
|
||||
if (!CheckAuthorization()) return null;
|
||||
|
||||
UploadResult result = UploadData(stream, "https://www.googleapis.com/upload/drive/v2/files", fileName, headers: GetAuthHeaders());
|
||||
string metadata = GetMetadata(fileName);
|
||||
|
||||
UploadResult result = UploadData(stream, "https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart", fileName, headers: GetAuthHeaders(), requestContentType: "multipart/related", metadata: metadata);
|
||||
|
||||
if (!string.IsNullOrEmpty(result.Response))
|
||||
{
|
||||
|
@ -231,8 +231,6 @@ public override UploadResult Upload(Stream stream, string fileName)
|
|||
{
|
||||
AllowReportProgress = false;
|
||||
|
||||
SetMetadata(upload.id, fileName, FolderID);
|
||||
|
||||
if (IsPublic)
|
||||
{
|
||||
SetPermissions(upload.id, GoogleDrivePermissionRole.reader, GoogleDrivePermissionType.anyone, "", true);
|
||||
|
|
|
@ -302,28 +302,41 @@ private HttpWebResponse GetResponse(string url, Stream dataStream, string bounda
|
|||
}
|
||||
|
||||
protected UploadResult UploadData(Stream dataStream, string url, string fileName, string fileFormName = "file", Dictionary<string, string> arguments = null,
|
||||
NameValueCollection headers = null, CookieCollection cookies = null, ResponseType responseType = ResponseType.Text, HttpMethod method = HttpMethod.POST)
|
||||
NameValueCollection headers = null, CookieCollection cookies = null, ResponseType responseType = ResponseType.Text, HttpMethod method = HttpMethod.POST,
|
||||
string requestContentType = "multipart/form-data", string metadata = null)
|
||||
{
|
||||
UploadResult result = new UploadResult();
|
||||
|
||||
IsUploading = true;
|
||||
StopUploadRequested = false;
|
||||
|
||||
try
|
||||
{
|
||||
string boundary = CreateBoundary();
|
||||
|
||||
byte[] bytesArguments = MakeInputContent(boundary, arguments, false);
|
||||
byte[] bytesDataOpen = MakeFileInputContentOpen(boundary, fileFormName, fileName);
|
||||
byte[] bytesDataOpen = { };
|
||||
byte[] bytesDataDatafile = { };
|
||||
|
||||
if (metadata != null)
|
||||
{
|
||||
bytesDataOpen = MakeFileInputContentOpen(boundary, fileFormName, fileName, metadata);
|
||||
bytesDataDatafile = MakeFileInputContentOpen(boundary, fileFormName, fileName, null);
|
||||
}
|
||||
else
|
||||
{
|
||||
bytesDataOpen = MakeFileInputContentOpen(boundary, fileFormName, fileName);
|
||||
}
|
||||
|
||||
byte[] bytesDataClose = MakeFileInputContentClose(boundary);
|
||||
|
||||
long contentLength = bytesArguments.Length + bytesDataOpen.Length + dataStream.Length + bytesDataClose.Length;
|
||||
HttpWebRequest request = PrepareDataWebRequest(url, boundary, contentLength, "multipart/form-data", cookies, headers, method);
|
||||
long contentLength = bytesArguments.Length + bytesDataOpen.Length + bytesDataDatafile.Length + dataStream.Length + bytesDataClose.Length;
|
||||
HttpWebRequest request = PrepareDataWebRequest(url, boundary, contentLength, requestContentType, cookies, headers, method);
|
||||
|
||||
using (Stream requestStream = request.GetRequestStream())
|
||||
{
|
||||
requestStream.Write(bytesArguments, 0, bytesArguments.Length);
|
||||
requestStream.Write(bytesDataOpen, 0, bytesDataOpen.Length);
|
||||
requestStream.Write(bytesDataDatafile, 0, bytesDataDatafile.Length);
|
||||
if (!TransferData(dataStream, requestStream)) return null;
|
||||
requestStream.Write(bytesDataClose, 0, bytesDataClose.Length);
|
||||
}
|
||||
|
@ -471,6 +484,22 @@ private byte[] MakeFileInputContentOpen(string boundary, string fileFormName, st
|
|||
return Encoding.UTF8.GetBytes(format);
|
||||
}
|
||||
|
||||
private byte[] MakeFileInputContentOpen(string boundary, string fileFormName, string fileName, string metadata)
|
||||
{
|
||||
string format = "";
|
||||
|
||||
if (metadata != null)
|
||||
{
|
||||
format = string.Format("--{0}\r\nContent-Type: {1}; charset=UTF-8\r\n\r\n{2}\r\n\r\n", boundary, "application/json", metadata);
|
||||
}
|
||||
else
|
||||
{
|
||||
format = string.Format("--{0}\r\nContent-Type: {1}\r\n\r\n", boundary, Helpers.GetMimeType(fileName));
|
||||
}
|
||||
|
||||
return Encoding.UTF8.GetBytes(format);
|
||||
}
|
||||
|
||||
private byte[] MakeFileInputContentClose(string boundary)
|
||||
{
|
||||
return Encoding.UTF8.GetBytes(string.Format("\r\n--{0}--\r\n", boundary));
|
||||
|
|
Loading…
Reference in a new issue