mirror of
https://github.com/ShareX/ShareX.git
synced 2024-10-01 17:56:51 +13:00
fixed #74: Google Drive metadata (title, folder) support, Uploader changes
This commit is contained in:
parent
fec9d763d7
commit
77906e3203
9 changed files with 69 additions and 47 deletions
|
@ -804,7 +804,8 @@ public UploadResult UploadFile(Stream stream, string fileName)
|
|||
case FileDestination.GoogleDrive:
|
||||
fileUploader = new GoogleDrive(Program.UploadersConfig.GoogleDriveOAuth2Info)
|
||||
{
|
||||
IsPublic = Program.UploadersConfig.GoogleDriveIsPublic
|
||||
IsPublic = Program.UploadersConfig.GoogleDriveIsPublic,
|
||||
FolderID = Program.UploadersConfig.GoogleDriveFolderID
|
||||
};
|
||||
break;
|
||||
case FileDestination.RapidShare:
|
||||
|
|
|
@ -48,7 +48,7 @@ public Ge_ttLogin Login(string email, string password)
|
|||
|
||||
string argsJSON = JsonConvert.SerializeObject(args);
|
||||
|
||||
string response = SendPostRequestJSON("https://open.ge.tt/1/users/login", argsJSON);
|
||||
string response = SendRequestJSON("https://open.ge.tt/1/users/login", argsJSON);
|
||||
|
||||
return JsonConvert.DeserializeObject<Ge_ttLogin>(response);
|
||||
}
|
||||
|
@ -76,7 +76,7 @@ public Ge_ttFile CreateFile(string accessToken, string shareName, string fileNam
|
|||
|
||||
string argsJSON = JsonConvert.SerializeObject(args2);
|
||||
|
||||
string response = SendPostRequestJSON(url, argsJSON);
|
||||
string response = SendRequestJSON(url, argsJSON);
|
||||
|
||||
return JsonConvert.DeserializeObject<Ge_ttFile>(response);
|
||||
}
|
||||
|
|
|
@ -36,6 +36,7 @@ public sealed class GoogleDrive : FileUploader, IOAuth2
|
|||
{
|
||||
public OAuth2Info AuthInfo { get; set; }
|
||||
public bool IsPublic { get; set; }
|
||||
public string FolderID { get; set; }
|
||||
|
||||
public GoogleDrive(OAuth2Info oauth)
|
||||
{
|
||||
|
@ -130,6 +131,39 @@ public bool CheckAuthorization()
|
|||
return true;
|
||||
}
|
||||
|
||||
public void SetMetadata(string fileID, string title, string parentID = null)
|
||||
{
|
||||
string url = string.Format("https://www.googleapis.com/drive/v2/files/{0}", fileID);
|
||||
|
||||
object metadata;
|
||||
|
||||
if (!string.IsNullOrEmpty(parentID))
|
||||
{
|
||||
metadata = new
|
||||
{
|
||||
title = title,
|
||||
parents = new[]
|
||||
{
|
||||
new
|
||||
{
|
||||
id = parentID
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
metadata = new
|
||||
{
|
||||
title = title
|
||||
};
|
||||
}
|
||||
|
||||
string json = JsonConvert.SerializeObject(metadata);
|
||||
|
||||
string response = SendRequestJSON(url, json, headers: GetAuthHeaders(), method: HttpMethod.PUT);
|
||||
}
|
||||
|
||||
public void SetPermissions(string fileID, GoogleDrivePermissionRole role, GoogleDrivePermissionType type, string value, bool withLink)
|
||||
{
|
||||
string url = string.Format("https://www.googleapis.com/drive/v2/files/{0}/permissions", fileID);
|
||||
|
@ -142,7 +176,7 @@ public void SetPermissions(string fileID, GoogleDrivePermissionRole role, Google
|
|||
withLink = withLink.ToString()
|
||||
});
|
||||
|
||||
string response = SendPostRequestJSON(url, json, headers: GetAuthHeaders());
|
||||
string response = SendRequestJSON(url, json, headers: GetAuthHeaders());
|
||||
}
|
||||
|
||||
public override UploadResult Upload(Stream stream, string fileName)
|
||||
|
@ -152,10 +186,7 @@ public override UploadResult Upload(Stream stream, string fileName)
|
|||
return null;
|
||||
}
|
||||
|
||||
Dictionary<string, string> args = new Dictionary<string, string>();
|
||||
args.Add("title", fileName);
|
||||
|
||||
UploadResult result = UploadData(stream, "https://www.googleapis.com/upload/drive/v2/files", fileName, "file", args, headers: GetAuthHeaders());
|
||||
UploadResult result = UploadData(stream, "https://www.googleapis.com/upload/drive/v2/files", fileName, "file", headers: GetAuthHeaders());
|
||||
|
||||
if (!string.IsNullOrEmpty(result.Response))
|
||||
{
|
||||
|
@ -163,9 +194,12 @@ public override UploadResult Upload(Stream stream, string fileName)
|
|||
|
||||
if (upload != null)
|
||||
{
|
||||
AllowReportProgress = false;
|
||||
|
||||
SetMetadata(upload.id, fileName, FolderID);
|
||||
|
||||
if (IsPublic)
|
||||
{
|
||||
AllowReportProgress = false;
|
||||
SetPermissions(upload.id, GoogleDrivePermissionRole.reader, GoogleDrivePermissionType.anyone, "", true);
|
||||
}
|
||||
|
||||
|
|
|
@ -123,7 +123,7 @@ public override UploadResult Upload(Stream stream, string fileName)
|
|||
|
||||
public string PostRequestJson(Uri url, string jsonData)
|
||||
{
|
||||
return SendPostRequestJSON(url.ToString(), jsonData);
|
||||
return SendRequestJSON(url.ToString(), jsonData);
|
||||
}
|
||||
|
||||
public string PostRequestRaw(Uri url, Stream dataStream)
|
||||
|
@ -131,7 +131,7 @@ public string PostRequestRaw(Uri url, Stream dataStream)
|
|||
try
|
||||
{
|
||||
AllowReportProgress = true;
|
||||
return SendPostRequestStream(url.ToString(), dataStream, "application/octet-stream");
|
||||
return SendRequestStream(url.ToString(), dataStream, "application/octet-stream");
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
|
|
@ -180,7 +180,7 @@ public override UploadResult Upload(Stream stream, string fileName)
|
|||
NameValueCollection headers = GetAuthHeaders();
|
||||
headers.Add("Slug", URLHelpers.URLEncode(fileName));
|
||||
|
||||
ur.Response = SendPostRequestStream(url, stream, contentType, headers: headers);
|
||||
ur.Response = SendRequestStream(url, stream, contentType, headers: headers);
|
||||
|
||||
XDocument xd = XDocument.Parse(ur.Response);
|
||||
|
||||
|
|
|
@ -116,7 +116,7 @@ public override UploadResult UploadText(string text, string fileName)
|
|||
url += "?access_token=" + AuthInfo.Token.access_token;
|
||||
}
|
||||
|
||||
string response = SendPostRequestJSON(url, argsJson);
|
||||
string response = SendRequestJSON(url, argsJson);
|
||||
|
||||
if (response != null)
|
||||
{
|
||||
|
|
|
@ -162,7 +162,7 @@ public override UploadResult ShortenURL(string url)
|
|||
|
||||
string json = string.Format("{{\"longUrl\":\"{0}\"}}", url);
|
||||
|
||||
result.Response = SendPostRequestJSON(query, json);
|
||||
result.Response = SendRequestJSON(query, json);
|
||||
|
||||
if (!string.IsNullOrEmpty(result.Response))
|
||||
{
|
||||
|
|
|
@ -114,8 +114,6 @@ public virtual void StopUpload()
|
|||
}
|
||||
}
|
||||
|
||||
#region Request methods
|
||||
|
||||
protected string SendRequest(HttpMethod method, string url, Dictionary<string, string> arguments = null, ResponseType responseType = ResponseType.Text,
|
||||
NameValueCollection headers = null, CookieCollection cookies = null)
|
||||
{
|
||||
|
@ -125,7 +123,7 @@ protected string SendRequest(HttpMethod method, string url, Dictionary<string, s
|
|||
{
|
||||
if (method == HttpMethod.POST) // Multipart form data
|
||||
{
|
||||
response = PostResponseMultiPart(url, arguments, headers, cookies);
|
||||
response = SendRequestMultiPart(url, arguments, headers, cookies);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -221,27 +219,27 @@ private HttpWebResponse GetResponse(HttpMethod method, string url, Dictionary<st
|
|||
return null;
|
||||
}
|
||||
|
||||
#endregion Request methods
|
||||
|
||||
#region Post methods
|
||||
|
||||
protected string SendPostRequestJSON(string url, string json, ResponseType responseType = ResponseType.Text, CookieCollection cookies = null, NameValueCollection headers = null)
|
||||
protected string SendRequestJSON(string url, string json, CookieCollection cookies = null, NameValueCollection headers = null, HttpMethod method = HttpMethod.POST)
|
||||
{
|
||||
using (HttpWebResponse response = PostResponseJSON(url, json, cookies, headers))
|
||||
byte[] data = Encoding.UTF8.GetBytes(json);
|
||||
|
||||
using (MemoryStream stream = new MemoryStream())
|
||||
{
|
||||
return ResponseToString(response, responseType);
|
||||
stream.Write(data, 0, data.Length);
|
||||
|
||||
return SendRequestStream(url, stream, "application/json", cookies, headers, method);
|
||||
}
|
||||
}
|
||||
|
||||
protected string SendPostRequestStream(string url, Stream stream, string contentType, CookieCollection cookies = null, NameValueCollection headers = null)
|
||||
protected string SendRequestStream(string url, Stream stream, string contentType, CookieCollection cookies = null, NameValueCollection headers = null, HttpMethod method = HttpMethod.POST)
|
||||
{
|
||||
using (HttpWebResponse response = GetResponseUsingPost(url, stream, CreateBoundary(), contentType, cookies, headers))
|
||||
using (HttpWebResponse response = GetResponse(url, stream, null, contentType, cookies, headers, method))
|
||||
{
|
||||
return ResponseToString(response);
|
||||
}
|
||||
}
|
||||
|
||||
private HttpWebResponse PostResponseMultiPart(string url, Dictionary<string, string> arguments, NameValueCollection headers = null, CookieCollection cookies = null)
|
||||
private HttpWebResponse SendRequestMultiPart(string url, Dictionary<string, string> arguments, NameValueCollection headers = null, CookieCollection cookies = null, HttpMethod method = HttpMethod.POST)
|
||||
{
|
||||
string boundary = CreateBoundary();
|
||||
byte[] data = MakeInputContent(boundary, arguments);
|
||||
|
@ -249,30 +247,19 @@ private HttpWebResponse PostResponseMultiPart(string url, Dictionary<string, str
|
|||
using (MemoryStream stream = new MemoryStream())
|
||||
{
|
||||
stream.Write(data, 0, data.Length);
|
||||
return GetResponseUsingPost(url, stream, boundary, "multipart/form-data", cookies, headers);
|
||||
return GetResponse(url, stream, boundary, "multipart/form-data", cookies, headers, method);
|
||||
}
|
||||
}
|
||||
|
||||
private HttpWebResponse PostResponseJSON(string url, string json, CookieCollection cookies = null, NameValueCollection headers = null)
|
||||
{
|
||||
byte[] data = Encoding.UTF8.GetBytes(json);
|
||||
|
||||
using (MemoryStream stream = new MemoryStream())
|
||||
{
|
||||
stream.Write(data, 0, data.Length);
|
||||
return GetResponseUsingPost(url, stream, null, "application/json", cookies, headers);
|
||||
}
|
||||
}
|
||||
|
||||
private HttpWebResponse GetResponseUsingPost(string url, Stream dataStream, string boundary, string contentType,
|
||||
CookieCollection cookies = null, NameValueCollection headers = null)
|
||||
private HttpWebResponse GetResponse(string url, Stream dataStream, string boundary, string contentType,
|
||||
CookieCollection cookies = null, NameValueCollection headers = null, HttpMethod method = HttpMethod.POST)
|
||||
{
|
||||
IsUploading = true;
|
||||
StopUploadRequested = false;
|
||||
|
||||
try
|
||||
{
|
||||
HttpWebRequest request = PreparePostWebRequest(url, boundary, dataStream.Length, contentType, cookies, headers);
|
||||
HttpWebRequest request = PrepareDataWebRequest(url, boundary, dataStream.Length, contentType, cookies, headers, method);
|
||||
|
||||
using (Stream requestStream = request.GetRequestStream())
|
||||
{
|
||||
|
@ -316,7 +303,7 @@ protected UploadResult UploadData(Stream dataStream, string url, string fileName
|
|||
byte[] bytesDataClose = MakeFileInputContentClose(boundary);
|
||||
|
||||
long contentLength = bytesArguments.Length + bytesDataOpen.Length + dataStream.Length + bytesDataClose.Length;
|
||||
HttpWebRequest request = PreparePostWebRequest(url, boundary, contentLength, "multipart/form-data", cookies, headers);
|
||||
HttpWebRequest request = PrepareDataWebRequest(url, boundary, contentLength, "multipart/form-data", cookies, headers);
|
||||
|
||||
using (Stream requestStream = request.GetRequestStream())
|
||||
{
|
||||
|
@ -346,11 +333,10 @@ protected UploadResult UploadData(Stream dataStream, string url, string fileName
|
|||
return result;
|
||||
}
|
||||
|
||||
#endregion Post methods
|
||||
|
||||
#region Helper methods
|
||||
|
||||
private HttpWebRequest PreparePostWebRequest(string url, string boundary, long length, string contentType, CookieCollection cookies = null, NameValueCollection headers = null)
|
||||
private HttpWebRequest PrepareDataWebRequest(string url, string boundary, long length, string contentType, CookieCollection cookies = null,
|
||||
NameValueCollection headers = null, HttpMethod method = HttpMethod.POST)
|
||||
{
|
||||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
||||
|
||||
|
@ -369,7 +355,7 @@ private HttpWebRequest PreparePostWebRequest(string url, string boundary, long l
|
|||
if (cookies != null) request.CookieContainer.Add(cookies);
|
||||
if (headers != null) request.Headers.Add(headers);
|
||||
request.KeepAlive = false;
|
||||
request.Method = HttpMethod.POST.ToString();
|
||||
request.Method = method.ToString();
|
||||
request.Pipelined = false;
|
||||
request.ProtocolVersion = HttpVersion.Version11;
|
||||
request.Proxy = ProxyInfo.Current.GetWebProxy();
|
||||
|
|
|
@ -123,6 +123,7 @@ public class UploadersConfig : SettingsBase<UploadersConfig>
|
|||
|
||||
public OAuth2Info GoogleDriveOAuth2Info = null;
|
||||
public bool GoogleDriveIsPublic = false;
|
||||
public string GoogleDriveFolderID = string.Empty;
|
||||
|
||||
// RapidShare
|
||||
|
||||
|
|
Loading…
Reference in a new issue