Skip to content

Commit

Permalink
Mobile: Fixes #10396: Fix Dropbox sync (#10400)
Browse files Browse the repository at this point in the history
  • Loading branch information
personalizedrefrigerator authored May 7, 2024
1 parent a1a06dd commit d6480e5
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions packages/lib/file-api-driver-dropbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,42 @@ class FileApiDriverDropbox {
// support POST requests with an empty body:
//
// https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Error-1017-quot-cannot-parse-response-quot/td-p/589595
const needsFetchWorkaround = shim.mobilePlatform() === 'ios';

const fetchPath = (method, path) => {
return this.api().exec(
method,
'files/download',
null,
{ 'Dropbox-API-Arg': JSON.stringify({ path: this.makePath_(path) }) },
options,
);
};

let response;
if (!needsFetchWorkaround) {
response = await fetchPath('POST', path);
} else {
try {
response = await fetchPath('GET', path);
} catch (_error) {
// May 2024: Sending a GET request to files/download sometimes fails
// until another file is requested. Because POST requests with empty bodies don't work on iOS,
// we send a request for a different file, then re-request the original.
//
// See https://github.com/laurent22/joplin/issues/10396

// This workaround requires that the file we request exist.
const { items } = await this.list();
const files = items.filter(item => !item.isDir && item.path !== path);

if (files.length > 0) {
await fetchPath('GET', files[0].path);
}

const response = await this.api().exec(
'GET',
'files/download',
null,
{
'Dropbox-API-Arg': JSON.stringify({ path: this.makePath_(path) }),
},
options,
);
response = await fetchPath('GET', path);
}
}
return response;
} catch (error) {
if (this.hasErrorCode_(error, 'not_found')) {
Expand Down

0 comments on commit d6480e5

Please sign in to comment.