0

Hey I haven't been able to find any information this but what I'm trying to achieve is using an express/node rest API that would use Dropbox JS SDK to send files to different applications mainly PHP, is this possible? I've been toying around with it and not sure I'm on the best path

API code

router.post('/', async (req, res) => {


    var dbx = new Dropbox({ accessToken: '<key>', fetch: fetch });
    dbx.filesDownload({path: "/large_vid.mp4"})
        .then(function (resp) {
            let data = resp[Object.keys(resp)[11]];
            res.status(201).send(data);
        })
        .catch(function (errs) {
            console.log(errs);
            res.status(203).send(errs);
        });
});


module.exports = router;

PHP code

      $ch = curl_init();

      curl_setopt($ch, CURLOPT_URL, $url);

      curl_setopt($ch, CURLOPT_POST, true);
      curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
            'Content-Type: application/json',                                                                                
            'Content-Length: ' . strlen($postData))                                                                       
        );

      curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

      $response = base64(curl_exec($ch));


Ancesteral
  • 109
  • 9
  • 1.) firstly, study up on promise chaining https://javascript.info/promise-chaining 2.) secondly, the PHP application(s) need to have an API for receiving file content. your problem has less to do with the dropbox code posted, more to do with the unknown PHP application code. – SteamDev Jul 17 '19 at 19:10
  • Hey @SteamDev Thanks for the response, read through that and it helped a lot I reposted some more code snippets, I'm wondering if you could point me in the direction of what I might have to do on the PHP end to accept the file? Also what information from the dropbox would I be sending back to the PHP end? – Ancesteral Jul 17 '19 at 20:32
  • If all you’re looking to do is initiate a file download, you can probably do that with express alone, without need for PHP. Have a look at http://expressjs.com/en/api.html#res.download. – SteamDev Jul 17 '19 at 20:43
  • Hey @SteamDev Thank you so much for your help on this, I looked into that a bit but kept erroring out "TypeError: Cannot read property 'download' of undefined" What specifically from dropbox do I have to pass into that? – Ancesteral Jul 17 '19 at 21:20
  • @Ancesteral first you’ll have to save the buffer from the dropbox `fileBinary` to a file on your system (see https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback). Then you’ll have to pass the path to that file to the `res.download()` method. – SteamDev Jul 18 '19 at 01:02
  • @SteamDev That worked perfectly! Thank you so much for pointing me in the right direction and all the help. The last problem I have to solve is displaying that file into a page, for example, a video if I set header('Content-Type: video/mp4'); and echo the results of the API call it'll display the video no problem, any idea how to get that into a video tag in html? – Ancesteral Jul 18 '19 at 17:19
  • @Ancesteral i think you’re asking about data uri as a source? take a look at this http://www.iandevlin.com/blog/2012/09/html5/html5-media-and-data-uri/. on the server side you’d need to convert your file buffer into a base64 encoded string, then use that string in the html – SteamDev Jul 18 '19 at 19:26
  • Hey @StreamDev That worked perfectly! Thank you so much one final question I promise so in the case of streaming a video file back this way ( I've updated my code snippets above) for a 30mb 720p video it takes about 10 seconds for everything to complete and load up, is there anyway to speed this up? – Ancesteral Jul 21 '19 at 05:39

0 Answers0