1require('isomorphic-fetch');
2const Apify = require('apify');
3const request = require('request-promise');
4const Dropbox = require('dropbox').Dropbox;
5
6Apify.main(async () => {
7 const input = await Apify.getValue('INPUT');
8
9 if(!input.accessToken){throw new Error('Missing "accessToken" attribute in INPUT!');}
10 if(!input.filePath){throw new Error('Missing "filePath" attribute in INPUT!');}
11 if(!input.fileUrl && !input.fileContents && !input.fileBase64){
12 throw new Error('Missing "fileUrl", "fileContents" or "fileBase64" attribute in INPUT!');
13 }
14
15 const fileData = input.fileUrl ? (await request({
16 uri: input.fileUrl,
17 encoding: null
18 })) : (input.fileContents || Buffer.from(input.fileBase64, 'base64'));
19
20 const dbx = new Dropbox({ accessToken: input.accessToken });
21
22 let lastError;
23 for (let i = 0; i < 4; i++) {
24 try {
25 const response = await dbx.filesUpload({path: input.filePath, contents: fileData, mode: 'overwrite'});
26 console.log(response)
27 return response;
28 } catch (e) {
29 lastError = e;
30 console.log(e)
31 await Apify.utils.sleep((i + 1) * 600);
32 }
33 }
34 throw lastError;
35});