diff --git a/src/baseTypes.ts b/src/baseTypes.ts index 2a55e4d..6401e20 100644 --- a/src/baseTypes.ts +++ b/src/baseTypes.ts @@ -48,10 +48,10 @@ export interface DropboxConfig { export type WebdavAuthType = "digest" | "basic"; export type WebdavDepthType = - | "auto" - | "auto_unknown" - | "auto_1" - | "auto_infinity" + | "auto" // deprecated on 20240116 + | "auto_unknown" // deprecated on 20240116 + | "auto_1" // deprecated on 20240116 + | "auto_infinity" // deprecated on 20240116 | "manual_1" | "manual_infinity"; @@ -60,9 +60,14 @@ export interface WebdavConfig { username: string; password: string; authType: WebdavAuthType; - manualRecursive: boolean; // deprecated in 0.3.6, use depth + depth?: WebdavDepthType; remoteBaseDir?: string; + + /** + * @deprecated + */ + manualRecursive: boolean; // deprecated in 0.3.6, use depth } export interface OnedriveConfig { diff --git a/src/langs/en.json b/src/langs/en.json index 8e5843e..d28c77d 100644 --- a/src/langs/en.json +++ b/src/langs/en.json @@ -224,8 +224,7 @@ "settings_webdav_auth": "Auth Type", "settings_webdav_auth_desc": "If no password, this option would be ignored.", "settings_webdav_depth": "Depth Header Sent To Servers", - "settings_webdav_depth_desc": "Webdav servers should be configured to allow requests with header Depth being '1' or 'Infinity'. The plugin needs to know this info. If you are not sure what's this, choose \"auto\".", - "settings_webdav_depth_auto": "auto detect", + "settings_webdav_depth_desc": "Webdav servers should be configured to allow requests with header Depth being '1' or 'Infinity'. If you are not sure what's this, choose \"depth='1'\". If you are sure your server supports depth='infinity', please choose that to get way better performance.", "settings_webdav_depth_1": "only supports depth='1'", "settings_webdav_depth_inf": "supports depth='infinity'", "settings_webdav_connect_succ": "Great! The webdav server can be accessed.", diff --git a/src/langs/zh_cn.json b/src/langs/zh_cn.json index b82669c..6c4635b 100644 --- a/src/langs/zh_cn.json +++ b/src/langs/zh_cn.json @@ -224,8 +224,7 @@ "settings_webdav_auth": "鉴权类型", "settings_webdav_auth_desc": "如果不填写密码,本设置会被忽略。", "settings_webdav_depth": "发送到服务器的 Depth header", - "settings_webdav_depth_desc": "Webdav 服务器会被设为接收 Depth header 为“1”或“Infinity”请求。本插件需要得知此信息。如果您不清楚如何设置,可以选择“自动检测”", - "settings_webdav_depth_auto": "自动检测", + "settings_webdav_depth_desc": "Webdav 服务器会被设为接收 Depth header 为“1”或“Infinity”请求。如果您不清楚如何设置,可以选择 depth='1'。如果那你确认服务器支持 depth='infinity',请选择它,会获得更加好的性能。", "settings_webdav_depth_1": "只支持 depth='1'", "settings_webdav_depth_inf": "支持 depth='infinity'", "settings_webdav_connect_succ": "很好!可以连接上 Webdav 服务器。", diff --git a/src/langs/zh_tw.json b/src/langs/zh_tw.json index 1de32ae..8c4928a 100644 --- a/src/langs/zh_tw.json +++ b/src/langs/zh_tw.json @@ -224,8 +224,7 @@ "settings_webdav_auth": "鑑權型別", "settings_webdav_auth_desc": "如果不填寫密碼,本設定會被忽略。", "settings_webdav_depth": "傳送到伺服器的 Depth header", - "settings_webdav_depth_desc": "Webdav 伺服器會被設為接收 Depth header 為“1”或“Infinity”請求。本外掛需要得知此資訊。如果您不清楚如何設定,可以選擇“自動檢測”", - "settings_webdav_depth_auto": "自動檢測", + "settings_webdav_depth_desc": "Webdav 伺服器會被設為接收 Depth header 為“1”或“Infinity”請求。如果您不清楚如何設定,可以選擇 depth='1'。如果那你確認伺服器支援 depth='infinity',請選擇它,會獲得更加好的效能。", "settings_webdav_depth_1": "只支援 depth='1'", "settings_webdav_depth_inf": "支援 depth='infinity'", "settings_webdav_connect_succ": "很好!可以連線上 Webdav 伺服器。", diff --git a/src/main.ts b/src/main.ts index 6ef868e..5c94007 100644 --- a/src/main.ts +++ b/src/main.ts @@ -66,7 +66,6 @@ import type { LangType, LangTypeAndAuto, TransItemType } from "./i18n"; import { DeletionOnRemote, MetadataOnRemote } from "./metadataOnRemote"; import { SyncAlgoV2Modal } from "./syncAlgoV2Notice"; -import { applyPresetRulesInplace } from "./presetRules"; import { applyLogWriterInplace, log } from "./moreOnLog"; import AggregateError from "aggregate-error"; @@ -443,7 +442,6 @@ export default class RemotelySavePlugin extends Plugin { this.currSyncMsg = ""; await this.loadSettings(); - await this.checkIfPresetRulesFollowed(); // lang should be load early, but after settings this.i18n = new I18n(this.settings.lang!, async (lang: LangTypeAndAuto) => { @@ -868,10 +866,18 @@ export default class RemotelySavePlugin extends Plugin { this.settings.onedrive.remoteBaseDir = ""; } if (this.settings.webdav.manualRecursive === undefined) { - this.settings.webdav.manualRecursive = false; + this.settings.webdav.manualRecursive = true; } - if (this.settings.webdav.depth === undefined) { - this.settings.webdav.depth = "auto_unknown"; + if ( + this.settings.webdav.depth === undefined || + this.settings.webdav.depth === "auto" || + this.settings.webdav.depth === "auto_1" || + this.settings.webdav.depth === "auto_infinity" || + this.settings.webdav.depth === "auto_unknown" + ) { + // auto is deprecated as of 20240116 + this.settings.webdav.depth = "manual_1"; + this.settings.webdav.manualRecursive = true; } if (this.settings.webdav.remoteBaseDir === undefined) { this.settings.webdav.remoteBaseDir = ""; @@ -910,13 +916,6 @@ export default class RemotelySavePlugin extends Plugin { await this.saveSettings(); } - async checkIfPresetRulesFollowed() { - const res = applyPresetRulesInplace(this.settings); - if (res.changed) { - await this.saveSettings(); - } - } - async saveSettings() { await this.saveData(normalConfigToMessy(this.settings)); } diff --git a/src/presetRules.ts b/src/presetRules.ts deleted file mode 100644 index 4e3cbec..0000000 --- a/src/presetRules.ts +++ /dev/null @@ -1,69 +0,0 @@ -import type { - RemotelySavePluginSettings, - WebdavConfig, - WebdavDepthType, -} from "./baseTypes"; - -const RULES = { - webdav: { - depth: [ - { - url: "^https://(.+).teracloud.jp/.+", - depth: "auto_1", - manualRecursive: true, - }, - { - url: "^https://dav.jianguoyun.com/dav/", - depth: "auto_1", - manualRecursive: true, - }, - ], - }, -}; - -export const applyWebdavPresetRulesInplace = ( - webdav: Partial | undefined -) => { - if (webdav === undefined) { - return { - changed: false, - webdav: webdav, - }; - } - for (const { url, depth, manualRecursive } of RULES.webdav.depth) { - if ( - webdav.address !== undefined && - new RegExp(url).test(webdav.address) && - webdav.depth !== undefined && - webdav.depth.startsWith("auto_") && - webdav.depth !== depth - ) { - webdav.depth = depth as WebdavDepthType; - webdav.manualRecursive = manualRecursive; - return { - changed: true, - webdav: webdav, - }; - } - } - return { - changed: false, - webdav: webdav, - }; -}; - -export const applyPresetRulesInplace = ( - settings: RemotelySavePluginSettings | undefined -) => { - if (settings === undefined) { - return { - changed: false, - settings: settings, - }; - } - const webdavRes = applyWebdavPresetRulesInplace(settings.webdav); - return { - changed: webdavRes.changed, - settings: settings, - }; -}; diff --git a/src/remoteForWebdav.ts b/src/remoteForWebdav.ts index e0b30cd..20fffa3 100644 --- a/src/remoteForWebdav.ts +++ b/src/remoteForWebdav.ts @@ -166,8 +166,8 @@ export const DEFAULT_WEBDAV_CONFIG = { username: "", password: "", authType: "basic", - manualRecursive: false, - depth: "auto_unknown", + manualRecursive: true, + depth: "manual_1", remoteBaseDir: "", } as WebdavConfig; @@ -279,56 +279,19 @@ export class WrappedWebdavClient { } // adjust depth parameter - if (this.webdavConfig.depth === "auto_unknown") { - let testPassed = false; - try { - const res = await this.client.customRequest(`/${this.remoteBaseDir}/`, { - method: "PROPFIND", - headers: { - Depth: "infinity", - Accept: "text/plain,application/xml", - }, - // responseType: "text", - } as any); - if (res.status === 403) { - throw Error("not support Infinity, get 403"); - } else { - testPassed = true; - this.webdavConfig.depth = "auto_infinity"; - this.webdavConfig.manualRecursive = false; - } - } catch (error) { - testPassed = false; - } - if (!testPassed) { - try { - const res = await this.client.customRequest( - `/${this.remoteBaseDir}/`, - { - method: "PROPFIND", - headers: { - Depth: "1", - Accept: "text/plain,application/xml", - }, - // responseType: "text", - } as any - ); - testPassed = true; - this.webdavConfig.depth = "auto_1"; - this.webdavConfig.manualRecursive = true; - } catch (error) { - testPassed = false; - } - } - if (testPassed) { - // the depth option has been changed - // save the setting - if (this.saveUpdatedConfigFunc !== undefined) { - await this.saveUpdatedConfigFunc(); - log.info( - `webdav depth="auto_unknown" is changed to ${this.webdavConfig.depth}` - ); - } + if ( + this.webdavConfig.depth === "auto" || + this.webdavConfig.depth === "auto_1" || + this.webdavConfig.depth === "auto_infinity" || + this.webdavConfig.depth === "auto_unknown" + ) { + this.webdavConfig.depth = "manual_1"; + this.webdavConfig.manualRecursive = true; + if (this.saveUpdatedConfigFunc !== undefined) { + await this.saveUpdatedConfigFunc(); + log.info( + `webdav depth="auto_???" is changed to ${this.webdavConfig.depth}` + ); } } }; @@ -453,7 +416,10 @@ export const listAllFromRemote = async (client: WrappedWebdavClient) => { let contents = [] as FileStat[]; if ( + client.webdavConfig.depth === "auto" || + client.webdavConfig.depth === "auto_unknown" || client.webdavConfig.depth === "auto_1" || + client.webdavConfig.depth === "auto_infinity" /* don't trust auto now */ || client.webdavConfig.depth === "manual_1" ) { // the remote doesn't support infinity propfind, diff --git a/src/settings.ts b/src/settings.ts index 153ed74..9ca530d 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -43,7 +43,6 @@ import { import { messyConfigToNormal } from "./configPersist"; import type { TransItemType } from "./i18n"; import { checkHasSpecialCharForDir } from "./misc"; -import { applyWebdavPresetRulesInplace } from "./presetRules"; import { applyLogWriterInplace, @@ -1462,16 +1461,16 @@ export class RemotelySaveSettingTab extends PluginSettingTab { .setValue(this.plugin.settings.webdav.address) .onChange(async (value) => { this.plugin.settings.webdav.address = value.trim(); + // deprecate auto on 20240116, force to manual_1 if ( + this.plugin.settings.webdav.depth === "auto" || this.plugin.settings.webdav.depth === "auto_1" || - this.plugin.settings.webdav.depth === "auto_infinity" + this.plugin.settings.webdav.depth === "auto_infinity" || + this.plugin.settings.webdav.depth === "auto_unknown" ) { - this.plugin.settings.webdav.depth = "auto_unknown"; + this.plugin.settings.webdav.depth = "manual_1"; } - // TODO: any more elegant way? - applyWebdavPresetRulesInplace(this.plugin.settings.webdav); - // normally saved await this.plugin.saveSettings(); }) @@ -1487,11 +1486,14 @@ export class RemotelySaveSettingTab extends PluginSettingTab { .setValue(this.plugin.settings.webdav.username) .onChange(async (value) => { this.plugin.settings.webdav.username = value.trim(); + // deprecate auto on 20240116, force to manual_1 if ( + this.plugin.settings.webdav.depth === "auto" || this.plugin.settings.webdav.depth === "auto_1" || - this.plugin.settings.webdav.depth === "auto_infinity" + this.plugin.settings.webdav.depth === "auto_infinity" || + this.plugin.settings.webdav.depth === "auto_unknown" ) { - this.plugin.settings.webdav.depth = "auto_unknown"; + this.plugin.settings.webdav.depth = "manual_1"; } await this.plugin.saveSettings(); }); @@ -1507,11 +1509,14 @@ export class RemotelySaveSettingTab extends PluginSettingTab { .setValue(this.plugin.settings.webdav.password) .onChange(async (value) => { this.plugin.settings.webdav.password = value.trim(); + // deprecate auto on 20240116, force to manual_1 if ( + this.plugin.settings.webdav.depth === "auto" || this.plugin.settings.webdav.depth === "auto_1" || - this.plugin.settings.webdav.depth === "auto_infinity" + this.plugin.settings.webdav.depth === "auto_infinity" || + this.plugin.settings.webdav.depth === "auto_unknown" ) { - this.plugin.settings.webdav.depth = "auto_unknown"; + this.plugin.settings.webdav.depth = "manual_1"; } await this.plugin.saveSettings(); }); @@ -1544,41 +1549,23 @@ export class RemotelySaveSettingTab extends PluginSettingTab { .setName(t("settings_webdav_depth")) .setDesc(t("settings_webdav_depth_desc")) .addDropdown((dropdown) => { - dropdown.addOption("auto", t("settings_webdav_depth_auto")); dropdown.addOption("manual_1", t("settings_webdav_depth_1")); dropdown.addOption("manual_infinity", t("settings_webdav_depth_inf")); - let initVal: WebdavDepthType = "auto"; - const autoOptions: Set = new Set([ - "auto_unknown", - "auto_1", - "auto_infinity", - ]); - if (autoOptions.has(this.plugin.settings.webdav.depth as any)) { - initVal = "auto"; - } else { - initVal = this.plugin.settings.webdav.depth || "auto"; - } + dropdown + .setValue(this.plugin.settings.webdav.depth || "manual_1") + .onChange(async (val) => { + if (val === "manual_1") { + this.plugin.settings.webdav.depth = "manual_1"; + this.plugin.settings.webdav.manualRecursive = true; + } else if (val === "manual_infinity") { + this.plugin.settings.webdav.depth = "manual_infinity"; + this.plugin.settings.webdav.manualRecursive = false; + } - type DepthOption = "auto" | "manual_1" | "manual_infinity"; - dropdown.setValue(initVal).onChange(async (val) => { - if (val === "auto") { - this.plugin.settings.webdav.depth = "auto_unknown"; - this.plugin.settings.webdav.manualRecursive = false; - } else if (val === "manual_1") { - this.plugin.settings.webdav.depth = "manual_1"; - this.plugin.settings.webdav.manualRecursive = true; - } else if (val === "manual_infinity") { - this.plugin.settings.webdav.depth = "manual_infinity"; - this.plugin.settings.webdav.manualRecursive = false; - } - - // TODO: any more elegant way? - applyWebdavPresetRulesInplace(this.plugin.settings.webdav); - - // normally save - await this.plugin.saveSettings(); - }); + // normally save + await this.plugin.saveSettings(); + }); }); let newWebdavRemoteBaseDir = diff --git a/tests/presetRules.test.ts b/tests/presetRules.test.ts deleted file mode 100644 index 9505f3b..0000000 --- a/tests/presetRules.test.ts +++ /dev/null @@ -1,111 +0,0 @@ -import { expect } from "chai"; -import type { WebdavConfig } from "../src/baseTypes"; -import { applyWebdavPresetRulesInplace } from "../src/presetRules"; - -describe("Preset rules tests", () => { - it("should check undefined correctly", () => { - let x: Partial | undefined = undefined; - const y = applyWebdavPresetRulesInplace(x); - expect(y.webdav === undefined); - expect(!y.changed); - }); - - it("should check empty object", () => { - let x: Partial | undefined = {}; - const y = applyWebdavPresetRulesInplace(x); - expect(y.webdav).deep.equals({}); - expect(!y.changed); - }); - - it("should modify depths correctly", () => { - let x: Partial = { - address: "https://example.teracloud.jp/dav/", - depth: "auto_unknown", - }; - let y = applyWebdavPresetRulesInplace(x); - expect(x.depth === "auto_1"); - expect(y.changed); - - x = { - address: "https://example.teracloud.jp/dav/example", - depth: "auto_unknown", - }; - y = applyWebdavPresetRulesInplace(x); - expect(x.depth === "auto_1"); - expect(y.changed); - - x = { - address: "https://dav.jianguoyun.com/dav/", - depth: "auto_unknown", - }; - y = applyWebdavPresetRulesInplace(x); - expect(x.depth === "auto_1"); - expect(y.changed); - - x = { - address: "https://dav.jianguoyun.com/dav/", - depth: "auto_infinity", - }; - y = applyWebdavPresetRulesInplace(x); - expect(x.depth === "auto_1"); - expect(y.changed); - }); - - it("should not modify depths if depths is set automatically correctly", () => { - let x: Partial = { - address: "https://dav.jianguoyun.com/dav/", - depth: "auto_1", - }; - let y = applyWebdavPresetRulesInplace(x); - expect(x.depth === "auto_1"); - expect(!y.changed); - }); - - it("should not modify depths if depths have been set manually", () => { - let x: Partial = { - address: "https://example.teracloud.jp/dav/", - depth: "manual_infinity", - }; - let y = applyWebdavPresetRulesInplace(x); - expect(x.depth === "manual_infinity"); - expect(!y.changed); - - x = { - address: "https://example.teracloud.jp/dav/example", - depth: "manual_1", - }; - y = applyWebdavPresetRulesInplace(x); - expect(x.depth === "manual_1"); - expect(!y.changed); - }); - - it("should not modify depths when urls are not in preset rules", () => { - let x: Partial = { - address: "https://teracloud.jp/dav/", - depth: "auto_unknown", - }; - applyWebdavPresetRulesInplace(x); - expect(x.depth === "auto_unknown"); - - x = { - address: "https://dav.jianguoyun.com/dav_example", - depth: "auto_unknown", - }; - applyWebdavPresetRulesInplace(x); - expect(x.depth === "auto_unknown"); - - x = { - address: "", - depth: "auto_unknown", - }; - applyWebdavPresetRulesInplace(x); - expect(x.depth === "auto_unknown"); - - x = { - address: "https://dav.jianguoyun.com/dav/", - depth: "what" as any, - }; - applyWebdavPresetRulesInplace(x); - expect(x.depth === ("what" as any)); - }); -});