1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-21 09:38:01 +02:00

Mobile: Simplify Dropbox sync workarond (#10415)

This commit is contained in:
Henry Heino 2024-05-12 02:01:02 -07:00 committed by GitHub
parent ca8fd8d7ae
commit 3312bd27c9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,9 +1,6 @@
const time = require('./time').default; const time = require('./time').default;
const shim = require('./shim').default; const shim = require('./shim').default;
const JoplinError = require('./JoplinError').default; const JoplinError = require('./JoplinError').default;
const Logger = require('@joplin/utils/Logger').default;
const logger = Logger.create('file-api-driver-dropbox');
class FileApiDriverDropbox { class FileApiDriverDropbox {
constructor(api) { constructor(api) {
@ -100,14 +97,14 @@ class FileApiDriverDropbox {
} }
} }
async list(path, options) { async list(path) {
let response = await this.api().exec('POST', 'files/list_folder', { let response = await this.api().exec('POST', 'files/list_folder', {
path: this.makePath_(path), path: this.makePath_(path),
}); });
let output = this.metadataToStats_(response.entries); 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', { response = await this.api().exec('POST', 'files/list_folder/continue', {
cursor: response.cursor, cursor: response.cursor,
}); });
@ -117,7 +114,7 @@ class FileApiDriverDropbox {
return { return {
items: output, items: output,
hasMore: !!response.has_more, hasMore: false,
context: { cursor: response.cursor }, context: { cursor: response.cursor },
}; };
} }
@ -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 // 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 needsFetchWorkaround = shim.mobilePlatform() === 'ios';
const fetchPath = (method, path) => { const fetchPath = (method, path, extraHeaders) => {
return this.api().exec( return this.api().exec(
method, method,
'files/download', 'files/download',
null, null,
{ 'Dropbox-API-Arg': JSON.stringify({ path: this.makePath_(path) }) }, { 'Dropbox-API-Arg': JSON.stringify({ path: this.makePath_(path) }), ...extraHeaders },
options, options,
); );
}; };
@ -149,26 +146,14 @@ class FileApiDriverDropbox {
if (!needsFetchWorkaround) { if (!needsFetchWorkaround) {
response = await fetchPath('POST', path); response = await fetchPath('POST', path);
} else { } else {
try { // Use a random If-None-Match value to prevent React Native from using the cache.
response = await fetchPath('GET', path); // Passing "cache: no-store" doesn't seem to be sufficient, so If-None-Match is set to a value
} catch (error) { // that will never match the ETag.
logger.warn('Request to files/download failed. Retrying with workaround. Error: ', error); //
// May 2024: Sending a GET request to files/download sometimes fails // Something similar is done for WebDAV.
// 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
// response = await fetchPath('GET', path, { 'If-None-Match': `JoplinIgnore-${Math.floor(Math.random() * 100000)}` });
// 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);
}
} }
return response; return response;
} catch (error) { } catch (error) {