Added Copy, CreateFolder, Delete, Move

This commit is contained in:
Jaex 2016-06-29 22:15:56 +03:00
parent 23ef49333b
commit e0d8e234ec

View file

@ -183,11 +183,14 @@ public bool DownloadFile(string path, Stream downloadStream)
if (!string.IsNullOrEmpty(path) && OAuth2Info.CheckOAuth(AuthInfo)) if (!string.IsNullOrEmpty(path) && OAuth2Info.CheckOAuth(AuthInfo))
{ {
NameValueCollection headers = GetAuthHeaders(); NameValueCollection headers = GetAuthHeaders();
path = URLHelpers.AddSlash(path, SlashType.Prefix); path = URLHelpers.AddSlash(path, SlashType.Prefix);
string arg = JsonConvert.SerializeObject(new string arg = JsonConvert.SerializeObject(new
{ {
path = path path = path
}); });
headers.Add("Dropbox-API-Arg", arg); headers.Add("Dropbox-API-Arg", arg);
return SendRequest(HttpMethod.POST, downloadStream, URLDownload, headers: headers, contentType: ContentTypeJSON); return SendRequest(HttpMethod.POST, downloadStream, URLDownload, headers: headers, contentType: ContentTypeJSON);
@ -205,8 +208,10 @@ public UploadResult UploadFile(Stream stream, string path, string fileName, bool
} }
NameValueCollection headers = GetAuthHeaders(); NameValueCollection headers = GetAuthHeaders();
path = URLHelpers.AddSlash(path, SlashType.Prefix); path = URLHelpers.AddSlash(path, SlashType.Prefix);
path = URLHelpers.CombineURL(path, fileName); path = URLHelpers.CombineURL(path, fileName);
string arg = JsonConvert.SerializeObject(new string arg = JsonConvert.SerializeObject(new
{ {
path = path, path = path,
@ -214,6 +219,7 @@ public UploadResult UploadFile(Stream stream, string path, string fileName, bool
autorename = false, autorename = false,
mute = true mute = true
}); });
headers.Add("Dropbox-API-Arg", arg); headers.Add("Dropbox-API-Arg", arg);
string response = SendRequestStream(URLUpload, stream, ContentTypeOctetStream, headers); string response = SendRequestStream(URLUpload, stream, ContentTypeOctetStream, headers);
@ -247,8 +253,8 @@ public DropboxMetadata GetMetadata(string path)
if (OAuth2Info.CheckOAuth(AuthInfo)) if (OAuth2Info.CheckOAuth(AuthInfo))
{ {
NameValueCollection headers = GetAuthHeaders();
path = URLHelpers.AddSlash(path, SlashType.Prefix); path = URLHelpers.AddSlash(path, SlashType.Prefix);
string arg = JsonConvert.SerializeObject(new string arg = JsonConvert.SerializeObject(new
{ {
path = path, path = path,
@ -257,7 +263,7 @@ public DropboxMetadata GetMetadata(string path)
include_has_explicit_shared_members = false include_has_explicit_shared_members = false
}); });
string response = SendRequestJSON(URLGetMetadata, arg, headers); string response = SendRequestJSON(URLGetMetadata, arg, GetAuthHeaders());
if (!string.IsNullOrEmpty(response)) if (!string.IsNullOrEmpty(response))
{ {
@ -278,8 +284,8 @@ public string CreateShareableLink(string path, DropboxURLType urlType)
{ {
if (!string.IsNullOrEmpty(path) && OAuth2Info.CheckOAuth(AuthInfo)) if (!string.IsNullOrEmpty(path) && OAuth2Info.CheckOAuth(AuthInfo))
{ {
NameValueCollection headers = GetAuthHeaders();
path = URLHelpers.AddSlash(path, SlashType.Prefix); path = URLHelpers.AddSlash(path, SlashType.Prefix);
string arg = JsonConvert.SerializeObject(new string arg = JsonConvert.SerializeObject(new
{ {
path = path, path = path,
@ -291,7 +297,7 @@ public string CreateShareableLink(string path, DropboxURLType urlType)
// TODO: args.Add("short_url", urlType == DropboxURLType.Shortened ? "true" : "false"); // TODO: args.Add("short_url", urlType == DropboxURLType.Shortened ? "true" : "false");
string response = SendRequestJSON(URLCreateSharedLink, arg, headers); string response = SendRequestJSON(URLCreateSharedLink, arg, GetAuthHeaders());
if (!string.IsNullOrEmpty(response)) if (!string.IsNullOrEmpty(response))
{ {
@ -327,88 +333,102 @@ public string CreateShareableLink(string path, DropboxURLType urlType)
public DropboxMetadata Copy(string from_path, string to_path) public DropboxMetadata Copy(string from_path, string to_path)
{ {
DropboxMetadata contentInfo = null; DropboxMetadata metadata = null;
if (!string.IsNullOrEmpty(from_path) && !string.IsNullOrEmpty(to_path) && OAuth2Info.CheckOAuth(AuthInfo)) if (!string.IsNullOrEmpty(from_path) && !string.IsNullOrEmpty(to_path) && OAuth2Info.CheckOAuth(AuthInfo))
{ {
Dictionary<string, string> args = new Dictionary<string, string>(); from_path = URLHelpers.AddSlash(from_path, SlashType.Prefix);
args.Add("root", Root); to_path = URLHelpers.AddSlash(to_path, SlashType.Prefix);
args.Add("from_path", from_path);
args.Add("to_path", to_path);
string response = SendRequest(HttpMethod.POST, URLCopy, args, GetAuthHeaders()); string arg = JsonConvert.SerializeObject(new
{
from_path = from_path,
to_path = to_path
});
string response = SendRequestJSON(URLCopy, arg, GetAuthHeaders());
if (!string.IsNullOrEmpty(response)) if (!string.IsNullOrEmpty(response))
{ {
contentInfo = JsonConvert.DeserializeObject<DropboxMetadata>(response); metadata = JsonConvert.DeserializeObject<DropboxMetadata>(response);
} }
} }
return contentInfo; return metadata;
} }
public DropboxMetadata CreateFolder(string path) public DropboxMetadata CreateFolder(string path)
{ {
DropboxMetadata contentInfo = null; DropboxMetadata metadata = null;
if (!string.IsNullOrEmpty(path) && OAuth2Info.CheckOAuth(AuthInfo)) if (!string.IsNullOrEmpty(path) && OAuth2Info.CheckOAuth(AuthInfo))
{ {
Dictionary<string, string> args = new Dictionary<string, string>(); path = URLHelpers.AddSlash(path, SlashType.Prefix);
args.Add("root", Root);
args.Add("path", path);
string response = SendRequest(HttpMethod.POST, URLCreateFolder, args, GetAuthHeaders()); string arg = JsonConvert.SerializeObject(new
{
path = path
});
string response = SendRequestJSON(URLCreateFolder, arg, GetAuthHeaders());
if (!string.IsNullOrEmpty(response)) if (!string.IsNullOrEmpty(response))
{ {
contentInfo = JsonConvert.DeserializeObject<DropboxMetadata>(response); metadata = JsonConvert.DeserializeObject<DropboxMetadata>(response);
} }
} }
return contentInfo; return metadata;
} }
public DropboxMetadata Delete(string path) public DropboxMetadata Delete(string path)
{ {
DropboxMetadata contentInfo = null; DropboxMetadata metadata = null;
if (!string.IsNullOrEmpty(path) && OAuth2Info.CheckOAuth(AuthInfo)) if (!string.IsNullOrEmpty(path) && OAuth2Info.CheckOAuth(AuthInfo))
{ {
Dictionary<string, string> args = new Dictionary<string, string>(); path = URLHelpers.AddSlash(path, SlashType.Prefix);
args.Add("root", Root);
args.Add("path", path);
string response = SendRequest(HttpMethod.POST, URLDelete, args, GetAuthHeaders()); string arg = JsonConvert.SerializeObject(new
{
path = path
});
string response = SendRequestJSON(URLDelete, arg, GetAuthHeaders());
if (!string.IsNullOrEmpty(response)) if (!string.IsNullOrEmpty(response))
{ {
contentInfo = JsonConvert.DeserializeObject<DropboxMetadata>(response); metadata = JsonConvert.DeserializeObject<DropboxMetadata>(response);
} }
} }
return contentInfo; return metadata;
} }
public DropboxMetadata Move(string from_path, string to_path) public DropboxMetadata Move(string from_path, string to_path)
{ {
DropboxMetadata contentInfo = null; DropboxMetadata metadata = null;
if (!string.IsNullOrEmpty(from_path) && !string.IsNullOrEmpty(to_path) && OAuth2Info.CheckOAuth(AuthInfo)) if (!string.IsNullOrEmpty(from_path) && !string.IsNullOrEmpty(to_path) && OAuth2Info.CheckOAuth(AuthInfo))
{ {
Dictionary<string, string> args = new Dictionary<string, string>(); from_path = URLHelpers.AddSlash(from_path, SlashType.Prefix);
args.Add("root", Root); to_path = URLHelpers.AddSlash(to_path, SlashType.Prefix);
args.Add("from_path", from_path);
args.Add("to_path", to_path);
string response = SendRequest(HttpMethod.POST, URLMove, args, GetAuthHeaders()); string arg = JsonConvert.SerializeObject(new
{
from_path = from_path,
to_path = to_path
});
string response = SendRequestJSON(URLMove, arg, GetAuthHeaders());
if (!string.IsNullOrEmpty(response)) if (!string.IsNullOrEmpty(response))
{ {
contentInfo = JsonConvert.DeserializeObject<DropboxMetadata>(response); metadata = JsonConvert.DeserializeObject<DropboxMetadata>(response);
} }
} }
return contentInfo; return metadata;
} }
#endregion File operations #endregion File operations