Jesus/dropbox_api

How to generate a link to a dropbox folder?

robskrob opened this issue · 5 comments

I've been searching through the docs and before I dive into the gem source code itself any further I would like to know the following: is it possible to generate a link to a dropbox folder with your gem?

I am building a string from DropboxApi::Metadata::Folder#path_lower, which indeed returns the url path for the folder. However, the path is missing /home, which seems to be the root of all created dropbox folders. To actually generate a url to the folder, I have to do something that I do not love:

"https://dropbox.com/home#{DropboxAPI::Client.new('/some/path/to/folder/').get_metadata('/some/path/to/folder/').path_lower}"
# => https://dropbox.com/home/some/path/to/folder/
Jesus commented

I'm sorry but that URL isn't something we can pull from their API.

Keep in mind that the URL you've generated in your example is only useful to you, the same file may be at a different path if you access it from another account.

In order to get a link to a file or folder, you should use create_shared_link_with_setting, for example:

client = DropboxApi::Client.new
shared_link = client.create_shared_link_with_settings "/target.txt"
shared_link.url # => "https://www.dropbox.com/s/s2bago3f67e1bsy/target.txt?dl=0" 

The good part of this is that when you open https://www.dropbox.com/s/s2bago3f67e1bsy/target.txt?dl=0 in your browser, it'll redirect to something like https://www.dropbox.com/home/... if the file is accessible from your account.

Unfortunately you have to go through all this hassle, I'm sorry but I don't think this library can provide a better solution.

@Jesus thank you for your response. Just for your information when I run:

client = DropboxAPI::Client.new('/some/path/to/folder/')
# get link to existing file in the above folder path
shared_link = client.create_shared_link_with_settings "/some/path/to/folder/existing_file.xls"
shared_link.url
# => "https://www.dropbox.com/s/#{random_character_string}/existing_file.xls?dl=0"

I get a link that takes me directly to the file -- but not to the folder where the file exists. Indirectly while on the page I suppose I can find the folder of the file. But my goal is to generate a url to the file's folder and not to the file itself.

I'll look into this issue some more. I am just worried that Dropbox may change the /home part of the url path. I am testing out two different dropbox accounts and it looks like /home is present as the root of every directory.

So perhaps there's no reason to be alarmed.

Jesus commented

I may be missing something, but you can simply use create_shared_link_with_settings with folders. For example, to get the URL of the file's folder you'd use:

client = DropboxApi::Client.new
shared_link = client.create_shared_link_with_settings File.dirname("/some/path/to/folder/existing_file.xls")
shared_link.url
# => "https://www.dropbox.com/sh/ro7nx98rp9edpu4/AAAHZ1TwaKzUmneyoUpq5AONa?dl=0"
Jesus commented

I'm assuming you got this sorted out. Please reopen if I can help any further.

@Jesus thank you for closing the issue, and I apologize for not responding more quickly to your helpful communication.