0

Using dropbox you can create a shortcut by dragging and dropping a URL into your Dropbox folder. This will be saved like this: example of a url in dropbox

Using the /2/files/download HTTP API from dropbox will return an XHR response that looks something like this: raw data returned from dropbox

How do you parse this response so that you can grab only the URL and make that a clickable link?

astanley86
  • 313
  • 1
  • 2
  • 11
  • [Cross-linking for reference: https://www.dropboxforum.com/t5/API-support/Get-the-URL-path-from-a-link-bookmark-url-file/m-p/199000#M9218 ] – Greg Dec 21 '16 at 23:52

1 Answers1

0

Here is what needs to go into an Angular 1 factory. To use this, you would just call the downloadFile function from a controller and provide the path to the file in your dropbox account.

 function downloadFile(filePath) {
            if (!filePath) {
                console.error('Cannot download file because no file was specified.');
                return;
            }
            return $q(function(fulfill, reject) {
                $http({
                    url: 'https://content.dropboxapi.com/2/files/download',
                    method: 'POST',
                    headers: {
                        'Authorization': 'Bearer {{access-token-goes-here}}',
                        'Dropbox-API-Arg': `{"path": "${filePath}"}`
                    },
                    responseType: 'blob'
                }).then(
                    results => {
                        // data received from dropbox is binary data saved as a blob
                        // The FileReader object lets web applications asynchronously read the contents of files
                        // https://developer.mozilla.org/en-US/docs/Web/API/FileReader
                        var fileReader = new FileReader();
                         // function will run after successfully reading the file
                        fileReader.onload = function() {
                            var string = this.result; // store the file contents
                            string = encodeURI(string); // get rid of the paragraph return characters
                            var endPosition = string.indexOf('%0D%0A', 32); // find the end of the URL, startPosition is 32
                            var actualURL = string.substring(32, endPosition); // grab only the characters between start and end positions
                            fulfill(actualURL);
                        };
                        fileReader.readAsText(results.data);                           
                    },
                    error => reject(error));
            });
        }
astanley86
  • 313
  • 1
  • 2
  • 11
  • By the way, it may be a good idea to parse the data fully, as opposed to relying on the desired URL data starting at position 32, since it looks like there are other things that may potentially be included. E.g., see this unofficial guide: http://www.lyberty.com/encyc/articles/tech/dot_url_format_-_an_unofficial_guide.html – Greg Dec 21 '16 at 23:58