Merge upstream

This commit is contained in:
Matthew Burnett 2019-01-24 21:40:00 -05:00
commit 54c90e1d15
4 changed files with 20 additions and 36 deletions

View file

@ -196,7 +196,7 @@ protected string SendRequestMultiPart(string url, Dictionary<string, string> arg
protected UploadResult SendRequestFile(string url, Stream data, string fileName, string fileFormName, Dictionary<string, string> args = null,
NameValueCollection headers = null, CookieCollection cookies = null, HttpMethod method = HttpMethod.POST, string contentType = UploadHelpers.ContentTypeMultipartFormData,
string metadata = null)
string relatedData = null)
{
UploadResult result = new UploadResult();
@ -210,12 +210,10 @@ protected UploadResult SendRequestFile(string url, Stream data, string fileName,
byte[] bytesArguments = UploadHelpers.MakeInputContent(boundary, args, false);
byte[] bytesDataOpen;
byte[] bytesDataDatafile = { };
if (metadata != null)
if (relatedData != null)
{
bytesDataOpen = UploadHelpers.MakeFileInputContentOpen(boundary, fileFormName, fileName, metadata);
bytesDataDatafile = UploadHelpers.MakeFileInputContentOpen(boundary, fileFormName, fileName, null);
bytesDataOpen = UploadHelpers.MakeRelatedFileInputContentOpen(boundary, "application/json; charset=UTF-8", relatedData, fileName);
}
else
{
@ -224,7 +222,7 @@ protected UploadResult SendRequestFile(string url, Stream data, string fileName,
byte[] bytesDataClose = UploadHelpers.MakeFileInputContentClose(boundary);
long contentLength = bytesArguments.Length + bytesDataOpen.Length + bytesDataDatafile.Length + data.Length + bytesDataClose.Length;
long contentLength = bytesArguments.Length + bytesDataOpen.Length + data.Length + bytesDataClose.Length;
HttpWebRequest request = CreateWebRequest(method, url, headers, cookies, contentType, contentLength);
@ -232,7 +230,6 @@ protected UploadResult SendRequestFile(string url, Stream data, string fileName,
{
requestStream.Write(bytesArguments, 0, bytesArguments.Length);
requestStream.Write(bytesDataOpen, 0, bytesDataOpen.Length);
requestStream.Write(bytesDataDatafile, 0, bytesDataDatafile.Length);
if (!TransferData(data, requestStream)) return null;
requestStream.Write(bytesDataClose, 0, bytesDataClose.Length);
}

View file

@ -201,7 +201,7 @@ public override UploadResult Upload(Stream stream, string fileName)
string metadata = GetMetadata(fileName, FolderID);
UploadResult result = SendRequestFile("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields=id,webViewLink,webContentLink", stream, fileName,
"file", headers: GoogleAuth.GetAuthHeaders(), contentType: "multipart/related", metadata: metadata);
"file", headers: GoogleAuth.GetAuthHeaders(), contentType: "multipart/related", relatedData: metadata);
if (!string.IsNullOrEmpty(result.Response))
{

View file

@ -114,7 +114,7 @@ public override UploadResult Upload(Stream stream, string fileName)
string metadata = GetMetadata(fileName);
UploadResult result = SendRequestFile("https://www.googleapis.com/upload/youtube/v3/videos?part=id,snippet,status", stream, fileName, "file",
headers: googleAuth.GetAuthHeaders(), metadata: metadata);
headers: googleAuth.GetAuthHeaders(), relatedData: metadata);
if (!string.IsNullOrEmpty(result.Response))
{

View file

@ -151,8 +151,8 @@ public static string CreateBoundary()
public static byte[] MakeInputContent(string boundary, string name, string value)
{
string format = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}\r\n", boundary, name, value);
return Encoding.UTF8.GetBytes(format);
string content = $"--{boundary}\r\nContent-Disposition: form-data; name=\"{name}\"\r\n\r\n{value}\r\n";
return Encoding.UTF8.GetBytes(content);
}
public static byte[] MakeInputContent(string boundary, Dictionary<string, string> contents, bool isFinal = true)
@ -160,10 +160,11 @@ public static byte[] MakeInputContent(string boundary, Dictionary<string, string
using (MemoryStream stream = new MemoryStream())
{
if (string.IsNullOrEmpty(boundary)) boundary = CreateBoundary();
byte[] bytes;
if (contents != null)
{
byte[] bytes;
foreach (KeyValuePair<string, string> content in contents)
{
if (!string.IsNullOrEmpty(content.Key) && !string.IsNullOrEmpty(content.Value))
@ -175,7 +176,7 @@ public static byte[] MakeInputContent(string boundary, Dictionary<string, string
if (isFinal)
{
bytes = MakeFinalBoundary(boundary);
bytes = Encoding.UTF8.GetBytes($"--{boundary}--\r\n");
stream.Write(bytes, 0, bytes.Length);
}
}
@ -186,36 +187,22 @@ public static byte[] MakeInputContent(string boundary, Dictionary<string, string
public static byte[] MakeFileInputContentOpen(string boundary, string fileFormName, string fileName)
{
string format = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\"\r\nContent-Type: {3}\r\n\r\n",
boundary, fileFormName, fileName, GetMimeType(fileName));
return Encoding.UTF8.GetBytes(format);
string mimeType = GetMimeType(fileName);
string content = $"--{boundary}\r\nContent-Disposition: form-data; name=\"{fileFormName}\"; filename=\"{fileName}\"\r\nContent-Type: {mimeType}\r\n\r\n";
return Encoding.UTF8.GetBytes(content);
}
public static byte[] MakeFileInputContentOpen(string boundary, string fileFormName, string fileName, string metadata)
public static byte[] MakeRelatedFileInputContentOpen(string boundary, string contentType, string relatedData, string fileName)
{
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, ContentTypeJSON, metadata);
}
else
{
format = string.Format("--{0}\r\nContent-Type: {1}\r\n\r\n", boundary, GetMimeType(fileName));
}
return Encoding.UTF8.GetBytes(format);
string mimeType = GetMimeType(fileName);
string content = $"--{boundary}\r\nContent-Type: {contentType}\r\n\r\n{relatedData}\r\n\r\n";
content += $"--{boundary}\r\nContent-Type: {mimeType}\r\n\r\n";
return Encoding.UTF8.GetBytes(content);
}
public static byte[] MakeFileInputContentClose(string boundary)
{
return Encoding.UTF8.GetBytes(string.Format("\r\n--{0}--\r\n", boundary));
}
public static byte[] MakeFinalBoundary(string boundary)
{
return Encoding.UTF8.GetBytes(string.Format("--{0}--\r\n", boundary));
return Encoding.UTF8.GetBytes($"\r\n--{boundary}--\r\n");
}
public static string ResponseToString(WebResponse response)