Skip to content

Commit

Permalink
fixed ShareX#2805: If Dropbox shared link already exists then get URL…
Browse files Browse the repository at this point in the history
… of it
  • Loading branch information
Jaex committed Nov 11, 2017
1 parent 452a039 commit 4d54841
Show file tree
Hide file tree
Showing 3 changed files with 8,673 additions and 14,940 deletions.
47 changes: 46 additions & 1 deletion ShareX.UploadersLib/FileUploaders/Dropbox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public sealed class Dropbox : FileUploader, IOAuth2Basic
private const string URLUpload = URLContent + "/files/upload";
private const string URLGetMetadata = URLAPI + "/files/get_metadata";
private const string URLCreateSharedLink = URLAPI + "/sharing/create_shared_link_with_settings";
private const string URLListSharedLinks = URLAPI + "/sharing/list_shared_links";
private const string URLCopy = URLAPI + "/files/copy";
private const string URLCreateFolder = URLAPI + "/files/create_folder";
private const string URLDelete = URLAPI + "/files/delete";
Expand Down Expand Up @@ -296,10 +297,24 @@ public string CreateShareableLink(string path, bool directLink)

string response = SendRequest(HttpMethod.POST, URLCreateSharedLink, json, ContentTypeJSON, null, GetAuthHeaders());

DropboxLinkMetadata linkMetadata = null;

if (!string.IsNullOrEmpty(response))
{
DropboxLinkMetadata linkMetadata = JsonConvert.DeserializeObject<DropboxLinkMetadata>(response);
linkMetadata = JsonConvert.DeserializeObject<DropboxLinkMetadata>(response);
}
else if (IsError && Errors[Errors.Count - 1].Contains("\"shared_link_already_exists\"")) // Ugly workaround
{
DropboxListSharedLinksResult result = ListSharedLinks(path, true);

if (result != null && result.links != null && result.links.Length > 0)
{
linkMetadata = result.links[0];
}
}

if (linkMetadata != null)
{
if (directLink)
{
return GetDirectShareableURL(linkMetadata.url);
Expand All @@ -314,6 +329,29 @@ public string CreateShareableLink(string path, bool directLink)
return null;
}

public DropboxListSharedLinksResult ListSharedLinks(string path, bool directOnly = false)
{
DropboxListSharedLinksResult result = null;

if (path != null && OAuth2Info.CheckOAuth(AuthInfo))
{
string json = JsonConvert.SerializeObject(new
{
path = VerifyPath(path),
direct_only = directOnly
});

string response = SendRequest(HttpMethod.POST, URLListSharedLinks, json, ContentTypeJSON, null, GetAuthHeaders());

if (!string.IsNullOrEmpty(response))
{
result = JsonConvert.DeserializeObject<DropboxListSharedLinksResult>(response);
}
}

return result;
}

public DropboxMetadata Copy(string fromPath, string toPath)
{
DropboxMetadata metadata = null;
Expand Down Expand Up @@ -541,4 +579,11 @@ public class DropboxLinkMetadataTeamInfo
public string id { get; set; }
public string name { get; set; }
}

public class DropboxListSharedLinksResult
{
public DropboxLinkMetadata[] links { get; set; }
public bool has_more { get; set; }
public string cursor { get; set; }
}
}
16 changes: 8 additions & 8 deletions ShareX.UploadersLib/Forms/UploadersConfigForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4d54841

Please sign in to comment.