Skip to content

Commit

Permalink
Mobile: Simplify Dropbox sync workarond (#10415)
Browse files Browse the repository at this point in the history
  • Loading branch information
personalizedrefrigerator committed May 12, 2024
1 parent ca8fd8d commit 3312bd2
Showing 1 changed file with 13 additions and 28 deletions.
41 changes: 13 additions & 28 deletions packages/lib/file-api-driver-dropbox.js
@@ -1,9 +1,6 @@
const time = require('./time').default;
const shim = require('./shim').default;
const JoplinError = require('./JoplinError').default;
const Logger = require('@joplin/utils/Logger').default;

const logger = Logger.create('file-api-driver-dropbox');

class FileApiDriverDropbox {
constructor(api) {
Expand Down Expand Up @@ -100,14 +97,14 @@ class FileApiDriverDropbox {
}
}

async list(path, options) {
async list(path) {
let response = await this.api().exec('POST', 'files/list_folder', {
path: this.makePath_(path),
});

let output = this.metadataToStats_(response.entries);

while (response.has_more && !options?.firstPageOnly) {
while (response.has_more) {
response = await this.api().exec('POST', 'files/list_folder/continue', {
cursor: response.cursor,
});
Expand All @@ -117,7 +114,7 @@ class FileApiDriverDropbox {

return {
items: output,
hasMore: !!response.has_more,
hasMore: false,
context: { cursor: response.cursor },
};
}
Expand All @@ -135,12 +132,12 @@ class FileApiDriverDropbox {
// 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) => {
const fetchPath = (method, path, extraHeaders) => {
return this.api().exec(
method,
'files/download',
null,
{ 'Dropbox-API-Arg': JSON.stringify({ path: this.makePath_(path) }) },
{ 'Dropbox-API-Arg': JSON.stringify({ path: this.makePath_(path) }), ...extraHeaders },
options,
);
};
Expand All @@ -149,26 +146,14 @@ class FileApiDriverDropbox {
if (!needsFetchWorkaround) {
response = await fetchPath('POST', path);
} else {
try {
response = await fetchPath('GET', path);
} catch (error) {
logger.warn('Request to files/download failed. Retrying with workaround. Error: ', 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('', { firstPageOnly: true });
const files = items.filter(item => !item.isDir && item.path !== path);

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

response = await fetchPath('GET', path);
}
// Use a random If-None-Match value to prevent React Native from using the cache.
// Passing "cache: no-store" doesn't seem to be sufficient, so If-None-Match is set to a value
// that will never match the ETag.
//
// Something similar is done for WebDAV.
//
// See https://github.com/laurent22/joplin/issues/10396
response = await fetchPath('GET', path, { 'If-None-Match': `JoplinIgnore-${Math.floor(Math.random() * 100000)}` });
}
return response;
} catch (error) {
Expand Down

0 comments on commit 3312bd2

Please sign in to comment.