Background saving to Dropbox from iOS
Last week Dropbox released a new API endpoint that allows to lazily download files to your Dropbox folder without you needing to wait until the file is uploaded.
We’ve recently introduced a new feature to the Dropbox API: /save_url. This new endpoint lets app developers upload files to Dropbox by just providing a URL, without having to download the file first. – Dropbox Developer Blog
My first thought when I read this was: this is useful on iOS.
An example: I wanted to download Gruber’s Phil Schiller interview to my Dropbox for archiving while browsing on my iPad. Vimeo has a download option on their website, but due to restrictions in iOS this means: download the file in Safari first, wait for it to download completely (5GB!!), and then upload that same file via the Dropbox Extension hoping the thing doesn’t timeout or run out of RAM. In short, doing this is quasi impossible on iOS. and doing this over 3G is just insane, you’ll run out of data before the video has even downloaded completely.
Using the API
To solve this issue and using the new API I came across the following idea: what if I could somehow copy the link of a large file I want to download on iOS and pass it to a service that would trigger a download on Dropbox’s servers without any further user interaction.
**This is what I came up with:**I hosted a file on my webserver that uses PHP to CURL a POST string to Dropbox’s servers. I surf to that webpage with a link that contains two parameters: $download_link and $token.
I then created a Workflow action that stores your Dropbox API token as a variable (local on device for security reasons), and captures a link in the clipboard, HTTP encodes it, and stores it as a second variable. The Workflow then generates an url that passes these two strings to my custom webpage, which than submits that data to Dropbox. A few moments later the file appears in your Dropbox folder.
The Webpage
I hosted the following code on my webhost:
- Parse https://domain.com?link=(link you want to download)&token=(your Dropbox API token)
- Uses the last part of the download link ($end) as a filename.
- It tells Dropbox to save the file to /Inbox
//Parse URL
$download\_link = $\_GET\[‘link’\];
$access\_token = $\_GET\[‘token’\];
//Set POST variables
$end = end((explode(‘/’, $download\_link)));
$url = “https://api.dropbox.com/1/save\_url/auto/Inbox/”.$end;
$data = array(‘url’ => $download\_link);
$headers = array(‘Authorization: Bearer ‘ . $access\_token);
//Open connection
$ch = curl\_init();
curl\_setopt($ch,CURLOPT\_URL, $url);
curl\_setopt($ch, CURLOPT\_HTTPHEADER, $headers);
curl\_setopt($ch,CURLOPT\_POST, true);
curl\_setopt($ch,CURLOPT\_POSTFIELDS, $data);
//Execute post and close connection.
$result = curl\_exec($ch);
curl\_close($ch);
?>
Dropbox Token
You can get a temporary Dropbox token by following the steps described on this Dropbox page. It’s a temporary token for personal use only (it maps to your Dropbox), but for this project it serves its purpose.
Testing with a generated access tokenIf you’d like to test out the Dropbox APIs quickly using your own Dropbox account, you can generate an access token from your newly created app in the App Console by pressing the button that says “Generate” in the OAuth 2 section of your app settings page. Keep in mind that this is only for your own account and you’ll need to use the standard OAuth flow to obtain access tokens for other users.
Workflow
Since I want to use this script to send very large files from iOS to Dropbox without needing to download the files first, I needed an easy way to generate the correct link and open it in Safari. Which means: Workflow!
The workflow works as follows:
- First I created a text-field that stores my Dropbox token, and I save it as a variable Token.
- Then I get the clipboard from my device and HTTP Encode it so I can use it as a parameter value in the link I need to generate.
- Finally I generate the complete link https://domain.com?link=(link you want to download)&token=(your Dropbox API token).
- And I open the link in Safari which shows you status message.
The Result
Combining it all together gives me a workflow like this:
- Go to a webpage and copy a download link to a large file. (A 950MB WWDC movie in this case)
- Open Workflow and open the select the script.
- Safari will immediately open with a
{pending}
message, this means it’s uploading. You can close Safari and do something else. - A few moments later the file shows up in Dropbox. (took around 5 minutes)
In conclusion
This workflow will save me a lot of time but there are a few things I would love to improve. Currently this process needs access to a custom webpage to run the Curl command. It would be better if I could just call Curl POST from within Workflow or by triggering another app via an URL- scheme.
Secondly the webpage currently shows the first response you get after doing the POST command, which is a message. It’s a rather ugly result. There are some API calls to ask for the status until you get an message, but doing this would make the workflow a lot more complex.
And finally I’d like to expand the workflow so it searches for downloadable links on a webpage, offers them in a list so you can pick one and upload it without needing to manually copy the download link.