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:
parent
ca8fd8d7ae
commit
3312bd27c9
@ -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) {
|
||||
@ -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,
|
||||
});
|
||||
@ -117,7 +114,7 @@ class FileApiDriverDropbox {
|
||||
|
||||
return {
|
||||
items: output,
|
||||
hasMore: !!response.has_more,
|
||||
hasMore: false,
|
||||
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
|
||||
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,
|
||||
);
|
||||
};
|
||||
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user