mirror of
https://github.com/remotely-save/remotely-save.git
synced 2024-06-07 21:10:45 +00:00
add preset rules for webdav
This commit is contained in:
parent
429063ebd1
commit
41073d1576
@ -51,6 +51,7 @@ import type { LangType, LangTypeAndAuto, TransItemType } from "./i18n";
|
||||
import * as origLog from "loglevel";
|
||||
import { DeletionOnRemote, MetadataOnRemote } from "./metadataOnRemote";
|
||||
import { SyncAlgoV2Modal } from "./syncAlgoV2Notice";
|
||||
import { applyPresetRulesInplace } from "./presetRules";
|
||||
const log = origLog.getLogger("rs-default");
|
||||
|
||||
const DEFAULT_SETTINGS: RemotelySavePluginSettings = {
|
||||
@ -373,6 +374,7 @@ 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) => {
|
||||
@ -706,6 +708,13 @@ export default class RemotelySavePlugin extends Plugin {
|
||||
}
|
||||
}
|
||||
|
||||
async checkIfPresetRulesFollowed() {
|
||||
const res = applyPresetRulesInplace(this.settings);
|
||||
if (res.changed) {
|
||||
await this.saveSettings();
|
||||
}
|
||||
}
|
||||
|
||||
async saveSettings() {
|
||||
await this.saveData(normalConfigToMessy(this.settings));
|
||||
}
|
||||
|
69
src/presetRules.ts
Normal file
69
src/presetRules.ts
Normal file
@ -0,0 +1,69 @@
|
||||
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<WebdavConfig> | 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,
|
||||
};
|
||||
};
|
@ -39,6 +39,7 @@ import type { TransItemType } from "./i18n";
|
||||
|
||||
import * as origLog from "loglevel";
|
||||
import { checkHasSpecialCharForDir } from "./misc";
|
||||
import { applyWebdavPresetRulesInplace } from "./presetRules";
|
||||
const log = origLog.getLogger("rs-default");
|
||||
|
||||
class PasswordModal extends Modal {
|
||||
@ -1323,6 +1324,11 @@ export class RemotelySaveSettingTab extends PluginSettingTab {
|
||||
) {
|
||||
this.plugin.settings.webdav.depth = "auto_unknown";
|
||||
}
|
||||
|
||||
// TODO: any more elegant way?
|
||||
applyWebdavPresetRulesInplace(this.plugin.settings.webdav);
|
||||
|
||||
// normally saved
|
||||
await this.plugin.saveSettings();
|
||||
})
|
||||
);
|
||||
@ -1420,6 +1426,11 @@ export class RemotelySaveSettingTab extends PluginSettingTab {
|
||||
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();
|
||||
});
|
||||
});
|
||||
|
111
tests/presetRules.test.ts
Normal file
111
tests/presetRules.test.ts
Normal file
@ -0,0 +1,111 @@
|
||||
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<WebdavConfig> | undefined = undefined;
|
||||
const y = applyWebdavPresetRulesInplace(x);
|
||||
expect(y.webdav === undefined);
|
||||
expect(!y.changed);
|
||||
});
|
||||
|
||||
it("should check empty object", () => {
|
||||
let x: Partial<WebdavConfig> | undefined = {};
|
||||
const y = applyWebdavPresetRulesInplace(x);
|
||||
expect(y.webdav).deep.equals({});
|
||||
expect(!y.changed);
|
||||
});
|
||||
|
||||
it("should modify depths correctly", () => {
|
||||
let x: Partial<WebdavConfig> = {
|
||||
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<WebdavConfig> = {
|
||||
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<WebdavConfig> = {
|
||||
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<WebdavConfig> = {
|
||||
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));
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user