optimize webdav

This commit is contained in:
fyears 2024-05-19 17:03:14 +08:00
parent cb779fc7bf
commit 3d7c4d2a4a
2 changed files with 71 additions and 28 deletions

View File

@ -4,8 +4,9 @@ import { getReasonPhrase } from "http-status-codes/build/cjs/utils-functions";
import chunk from "lodash/chunk"; import chunk from "lodash/chunk";
import cloneDeep from "lodash/cloneDeep"; import cloneDeep from "lodash/cloneDeep";
import flatten from "lodash/flatten"; import flatten from "lodash/flatten";
import isString from "lodash/isString";
import { nanoid } from "nanoid"; import { nanoid } from "nanoid";
import { Platform, requestUrl } from "obsidian"; import { Platform, type RequestUrlParam, requestUrl } from "obsidian";
import type { import type {
FileStat, FileStat,
RequestOptionsWithState, RequestOptionsWithState,
@ -16,7 +17,7 @@ import type {
import type { Entity, WebdavConfig } from "./baseTypes"; import type { Entity, WebdavConfig } from "./baseTypes";
import { VALID_REQURL } from "./baseTypesObs"; import { VALID_REQURL } from "./baseTypesObs";
import { FakeFs } from "./fsAll"; import { FakeFs } from "./fsAll";
import { bufferToArrayBuffer } from "./misc"; import { bufferToArrayBuffer, delay } from "./misc";
/** /**
* https://stackoverflow.com/questions/32850898/how-to-check-if-a-string-has-any-non-iso-8859-1-characters-with-javascript * https://stackoverflow.com/questions/32850898/how-to-check-if-a-string-has-any-non-iso-8859-1-characters-with-javascript
@ -62,14 +63,29 @@ if (VALID_REQURL) {
// console.debug(`headers: ${JSON.stringify(retractedHeaders, null, 2)}`); // console.debug(`headers: ${JSON.stringify(retractedHeaders, null, 2)}`);
// console.debug(`reqContentType: ${reqContentType}`); // console.debug(`reqContentType: ${reqContentType}`);
let r = await requestUrl({ const p: RequestUrlParam = {
url: options.url, url: options.url,
method: options.method, method: options.method,
body: options.data as string | ArrayBuffer, // body: options.data as string | ArrayBuffer,
headers: transformedHeaders, headers: transformedHeaders,
contentType: reqContentType, contentType: reqContentType,
throw: false, throw: false,
}); };
if (
options.data === undefined ||
options.data === null ||
isString(options.data)
) {
p.body = options.data;
} else {
if (typeof (options.data as any).transfer === "function") {
p.body = (options.data as any).transfer();
} else {
p.body = options.data as ArrayBuffer;
}
}
let r = await requestUrl(p);
if ( if (
r.status === 401 && r.status === 401 &&
@ -83,14 +99,8 @@ if (VALID_REQURL) {
// if a folder doesn't exist without slash, the servers return 401 instead of 404 // if a folder doesn't exist without slash, the servers return 401 instead of 404
// here is a dirty hack that works // here is a dirty hack that works
console.debug(`so we have 401, try appending request url with slash`); console.debug(`so we have 401, try appending request url with slash`);
r = await requestUrl({ p.url = `${options.url}/`;
url: `${options.url}/`, r = await requestUrl(p);
method: options.method,
body: options.data as string | ArrayBuffer,
headers: transformedHeaders,
contentType: reqContentType,
throw: false,
});
} }
// console.debug(`after request:`); // console.debug(`after request:`);
@ -539,7 +549,7 @@ export class FakeFsWebdav extends FakeFs {
} }
const destUrl = `${this.webdavConfig.address}/${encodeURI(key)}`; const destUrl = `${this.webdavConfig.address}/${encodeURI(key)}`;
console.debug(`destUrl=${destUrl}`); console.debug(`destUrl=${destUrl}`);
const tmpFolder = `_${key}-${nanoid()}`; const tmpFolder = `${key}-${nanoid()}`;
console.debug(`tmpFolder=${tmpFolder}`); console.debug(`tmpFolder=${tmpFolder}`);
const tmpFolderUrl = `${this.webdavConfig.address}/${encodeURI(tmpFolder)}`; const tmpFolderUrl = `${this.webdavConfig.address}/${encodeURI(tmpFolder)}`;
console.debug(`tmpFolderUrl=${tmpFolderUrl}`); console.debug(`tmpFolderUrl=${tmpFolderUrl}`);
@ -598,23 +608,47 @@ export class FakeFsWebdav extends FakeFs {
`while assembling chunks of nextcloud, some errors occur but we ignore them:` `while assembling chunks of nextcloud, some errors occur but we ignore them:`
); );
console.error(e); console.error(e);
// wait for a few time!
await delay(1000);
} }
// TODO: setting X-OC-Mtime // TODO: setting X-OC-Mtime
// wait for anything broken??
await delay(1000);
// clean up!
console.debug(`try to clean up`);
try {
// tmpFileIdx -= 1;
// do {
// const tmpFileName = `${tmpFileIdx}`.padStart(5, "0");
// const tmpFileNameWithFolder = `${tmpFolder}/${tmpFileName}`;
// await this.client.deleteFile(tmpFileNameWithFolder);
// tmpFileIdx -= 1;
// } while (tmpFileIdx > 0);
await this.client.deleteFile(tmpFolder);
console.debug(`finish clean up`);
} catch (e) {
console.error(
`while cleaning chunks of nextcloud, some errors occur but we ignore them:`
);
console.error(e);
}
// stat // stat
console.debug(`before stat key=${key}`);
const k = await this._statFromRoot(key); const k = await this._statFromRoot(key);
console.debug(`after stat`);
if (k.sizeRaw !== content.byteLength) { if (k.sizeRaw !== content.byteLength) {
// we failed! // we failed!
this.isNextcloud = false; // give up next time! this.isNextcloud = false; // give up next time!
throw Error(`unable to upload file ${key} by chunks to nextcloud`); const err = `unable to upload file ${key} by chunks to nextcloud`;
} console.error(err);
throw Error(err);
// clean up!
try {
await this.client.deleteFile(tmpFolder);
} catch (e) {
// the folde might exist or not, still ignore the error
} }
console.debug(`after stat, k=${JSON.stringify(k, null, 2)}`);
return k; return k;
} }

View File

@ -950,12 +950,21 @@ async function copyFile(
} }
// console.debug(`copyFile: about to start right.writeFile`); // console.debug(`copyFile: about to start right.writeFile`);
return await right.writeFile( if (typeof (content as any).transfer === "function") {
key, return await right.writeFile(
content, key,
statsLeft.mtimeCli, (content as any).transfer(),
statsLeft.mtimeCli /* TODO */ statsLeft.mtimeCli,
); statsLeft.mtimeCli /* TODO */
);
} else {
return await right.writeFile(
key,
content,
statsLeft.mtimeCli,
statsLeft.mtimeCli /* TODO */
);
}
} }
async function copyFileOrFolder( async function copyFileOrFolder(