some logic to move away vaultRandomID from data.json
This commit is contained in:
parent
712979fb0d
commit
83c3aac317
@ -66,7 +66,6 @@ export interface RemotelySavePluginSettings {
|
|||||||
password: string;
|
password: string;
|
||||||
serviceType: SUPPORTED_SERVICES_TYPE;
|
serviceType: SUPPORTED_SERVICES_TYPE;
|
||||||
currLogLevel?: string;
|
currLogLevel?: string;
|
||||||
vaultRandomID?: string;
|
|
||||||
autoRunEveryMilliseconds?: number;
|
autoRunEveryMilliseconds?: number;
|
||||||
initRunAfterMilliseconds?: number;
|
initRunAfterMilliseconds?: number;
|
||||||
agreeToUploadExtraMetadata?: boolean;
|
agreeToUploadExtraMetadata?: boolean;
|
||||||
@ -74,6 +73,11 @@ export interface RemotelySavePluginSettings {
|
|||||||
syncConfigDir?: boolean;
|
syncConfigDir?: boolean;
|
||||||
syncUnderscoreItems?: boolean;
|
syncUnderscoreItems?: boolean;
|
||||||
lang?: LangTypeAndAuto;
|
lang?: LangTypeAndAuto;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
|
vaultRandomID?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface RemoteItem {
|
export interface RemoteItem {
|
||||||
|
|||||||
@ -1 +1 @@
|
|||||||
Subproject commit 5e740813d52a06a7555d067af45e719db23f48aa
|
Subproject commit 230fee440e72736f7582372cbf9dc4ff648457de
|
||||||
@ -7,6 +7,7 @@ import type { SyncPlanType } from "./sync";
|
|||||||
export type LocalForage = typeof localforage;
|
export type LocalForage = typeof localforage;
|
||||||
|
|
||||||
import * as origLog from "loglevel";
|
import * as origLog from "loglevel";
|
||||||
|
import { nanoid } from "nanoid";
|
||||||
const log = origLog.getLogger("rs-default");
|
const log = origLog.getLogger("rs-default");
|
||||||
|
|
||||||
const DB_VERSION_NUMBER_IN_HISTORY = [20211114, 20220108, 20220326];
|
const DB_VERSION_NUMBER_IN_HISTORY = [20211114, 20220108, 20220326];
|
||||||
@ -16,6 +17,7 @@ export const DEFAULT_TBL_VERSION = "schemaversion";
|
|||||||
export const DEFAULT_TBL_FILE_HISTORY = "filefolderoperationhistory";
|
export const DEFAULT_TBL_FILE_HISTORY = "filefolderoperationhistory";
|
||||||
export const DEFAULT_TBL_SYNC_MAPPING = "syncmetadatahistory";
|
export const DEFAULT_TBL_SYNC_MAPPING = "syncmetadatahistory";
|
||||||
export const DEFAULT_SYNC_PLANS_HISTORY = "syncplanshistory";
|
export const DEFAULT_SYNC_PLANS_HISTORY = "syncplanshistory";
|
||||||
|
export const DEFAULT_TBL_VAULT_RANDOM_ID_MAPPING = "vaultrandomidmapping";
|
||||||
|
|
||||||
export interface FileFolderHistoryRecord {
|
export interface FileFolderHistoryRecord {
|
||||||
key: string;
|
key: string;
|
||||||
@ -54,6 +56,7 @@ export interface InternalDBs {
|
|||||||
fileHistoryTbl: LocalForage;
|
fileHistoryTbl: LocalForage;
|
||||||
syncMappingTbl: LocalForage;
|
syncMappingTbl: LocalForage;
|
||||||
syncPlansTbl: LocalForage;
|
syncPlansTbl: LocalForage;
|
||||||
|
vaultRandomIDMappingTbl: LocalForage;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -183,7 +186,10 @@ const migrateDBs = async (
|
|||||||
throw Error(`not supported internal db changes from ${oldVer} to ${newVer}`);
|
throw Error(`not supported internal db changes from ${oldVer} to ${newVer}`);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const prepareDBs = async (vaultRandomID: string) => {
|
export const prepareDBs = async (
|
||||||
|
vaultBasePath: string,
|
||||||
|
vaultRandomIDFromOldConfigFile: string
|
||||||
|
) => {
|
||||||
const db = {
|
const db = {
|
||||||
versionTbl: localforage.createInstance({
|
versionTbl: localforage.createInstance({
|
||||||
name: DEFAULT_DB_NAME,
|
name: DEFAULT_DB_NAME,
|
||||||
@ -201,8 +207,41 @@ export const prepareDBs = async (vaultRandomID: string) => {
|
|||||||
name: DEFAULT_DB_NAME,
|
name: DEFAULT_DB_NAME,
|
||||||
storeName: DEFAULT_SYNC_PLANS_HISTORY,
|
storeName: DEFAULT_SYNC_PLANS_HISTORY,
|
||||||
}),
|
}),
|
||||||
|
vaultRandomIDMappingTbl: localforage.createInstance({
|
||||||
|
name: DEFAULT_DB_NAME,
|
||||||
|
storeName: DEFAULT_TBL_VAULT_RANDOM_ID_MAPPING,
|
||||||
|
}),
|
||||||
} as InternalDBs;
|
} as InternalDBs;
|
||||||
|
|
||||||
|
// try to get vaultRandomID firstly
|
||||||
|
let vaultRandomID = "";
|
||||||
|
const vaultRandomIDInDB: string | null =
|
||||||
|
await db.vaultRandomIDMappingTbl.getItem(`path2id\t${vaultBasePath}`);
|
||||||
|
if (vaultRandomIDInDB === null) {
|
||||||
|
if (vaultRandomIDFromOldConfigFile !== "") {
|
||||||
|
// reuse the old config id
|
||||||
|
vaultRandomID = vaultRandomIDFromOldConfigFile;
|
||||||
|
} else {
|
||||||
|
// no old config id, we create a random one
|
||||||
|
vaultRandomID = nanoid();
|
||||||
|
}
|
||||||
|
// save the id back
|
||||||
|
await db.vaultRandomIDMappingTbl.setItem(
|
||||||
|
`path2id\t${vaultBasePath}`,
|
||||||
|
vaultRandomID
|
||||||
|
);
|
||||||
|
await db.vaultRandomIDMappingTbl.setItem(
|
||||||
|
`id2path\t${vaultRandomID}`,
|
||||||
|
vaultBasePath
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
vaultRandomID = vaultRandomIDInDB;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (vaultRandomID === "") {
|
||||||
|
throw Error("no vaultRandomID found or generated");
|
||||||
|
}
|
||||||
|
|
||||||
const originalVersion: number | null = await db.versionTbl.getItem("version");
|
const originalVersion: number | null = await db.versionTbl.getItem("version");
|
||||||
if (originalVersion === null) {
|
if (originalVersion === null) {
|
||||||
log.debug(
|
log.debug(
|
||||||
@ -224,7 +263,10 @@ export const prepareDBs = async (vaultRandomID: string) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.info("db connected");
|
log.info("db connected");
|
||||||
return db;
|
return {
|
||||||
|
db: db,
|
||||||
|
vaultRandomID: vaultRandomID,
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const destroyDBs = async () => {
|
export const destroyDBs = async () => {
|
||||||
|
|||||||
84
src/main.ts
84
src/main.ts
@ -1,6 +1,13 @@
|
|||||||
import { Modal, Notice, Plugin, Setting, addIcon, setIcon } from "obsidian";
|
import {
|
||||||
|
Modal,
|
||||||
|
Notice,
|
||||||
|
Plugin,
|
||||||
|
Setting,
|
||||||
|
addIcon,
|
||||||
|
setIcon,
|
||||||
|
FileSystemAdapter,
|
||||||
|
} from "obsidian";
|
||||||
import cloneDeep from "lodash/cloneDeep";
|
import cloneDeep from "lodash/cloneDeep";
|
||||||
import { nanoid } from "nanoid";
|
|
||||||
import { createElement, RotateCcw, RefreshCcw } from "lucide";
|
import { createElement, RotateCcw, RefreshCcw } from "lucide";
|
||||||
import type { RemotelySavePluginSettings } from "./baseTypes";
|
import type { RemotelySavePluginSettings } from "./baseTypes";
|
||||||
import {
|
import {
|
||||||
@ -54,7 +61,7 @@ const DEFAULT_SETTINGS: RemotelySavePluginSettings = {
|
|||||||
password: "",
|
password: "",
|
||||||
serviceType: "s3",
|
serviceType: "s3",
|
||||||
currLogLevel: "info",
|
currLogLevel: "info",
|
||||||
vaultRandomID: "",
|
// vaultRandomID: "", // deprecated
|
||||||
autoRunEveryMilliseconds: -1,
|
autoRunEveryMilliseconds: -1,
|
||||||
initRunAfterMilliseconds: -1,
|
initRunAfterMilliseconds: -1,
|
||||||
agreeToUploadExtraMetadata: false,
|
agreeToUploadExtraMetadata: false,
|
||||||
@ -104,6 +111,7 @@ export default class RemotelySavePlugin extends Plugin {
|
|||||||
syncRibbon?: HTMLElement;
|
syncRibbon?: HTMLElement;
|
||||||
autoRunIntervalID?: number;
|
autoRunIntervalID?: number;
|
||||||
i18n: I18n;
|
i18n: I18n;
|
||||||
|
vaultRandomID: string;
|
||||||
|
|
||||||
async syncRun(triggerSource: SyncTriggerSourceType = "manual") {
|
async syncRun(triggerSource: SyncTriggerSourceType = "manual") {
|
||||||
const t = (x: TransItemType, vars?: any) => {
|
const t = (x: TransItemType, vars?: any) => {
|
||||||
@ -216,7 +224,7 @@ export default class RemotelySavePlugin extends Plugin {
|
|||||||
const { remoteStates, metadataFile } = await parseRemoteItems(
|
const { remoteStates, metadataFile } = await parseRemoteItems(
|
||||||
remoteRsp.Contents,
|
remoteRsp.Contents,
|
||||||
this.db,
|
this.db,
|
||||||
this.settings.vaultRandomID,
|
this.vaultRandomID,
|
||||||
client.serviceType,
|
client.serviceType,
|
||||||
this.settings.password
|
this.settings.password
|
||||||
);
|
);
|
||||||
@ -236,7 +244,7 @@ export default class RemotelySavePlugin extends Plugin {
|
|||||||
const local = this.app.vault.getAllLoadedFiles();
|
const local = this.app.vault.getAllLoadedFiles();
|
||||||
const localHistory = await loadFileHistoryTableByVault(
|
const localHistory = await loadFileHistoryTableByVault(
|
||||||
this.db,
|
this.db,
|
||||||
this.settings.vaultRandomID
|
this.vaultRandomID
|
||||||
);
|
);
|
||||||
let localConfigDirContents: ObsConfigDirFileType[] = undefined;
|
let localConfigDirContents: ObsConfigDirFileType[] = undefined;
|
||||||
if (this.settings.syncConfigDir) {
|
if (this.settings.syncConfigDir) {
|
||||||
@ -270,11 +278,7 @@ export default class RemotelySavePlugin extends Plugin {
|
|||||||
);
|
);
|
||||||
log.info(plan.mixedStates); // for debugging
|
log.info(plan.mixedStates); // for debugging
|
||||||
if (triggerSource !== "dry") {
|
if (triggerSource !== "dry") {
|
||||||
await insertSyncPlanRecordByVault(
|
await insertSyncPlanRecordByVault(this.db, plan, this.vaultRandomID);
|
||||||
this.db,
|
|
||||||
plan,
|
|
||||||
this.settings.vaultRandomID
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// The operations above are almost read only and kind of safe.
|
// The operations above are almost read only and kind of safe.
|
||||||
@ -291,7 +295,7 @@ export default class RemotelySavePlugin extends Plugin {
|
|||||||
await doActualSync(
|
await doActualSync(
|
||||||
client,
|
client,
|
||||||
this.db,
|
this.db,
|
||||||
this.settings.vaultRandomID,
|
this.vaultRandomID,
|
||||||
this.app.vault,
|
this.app.vault,
|
||||||
plan,
|
plan,
|
||||||
sortedKeys,
|
sortedKeys,
|
||||||
@ -384,13 +388,23 @@ export default class RemotelySavePlugin extends Plugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
await this.checkIfOauthExpires();
|
await this.checkIfOauthExpires();
|
||||||
await this.checkIfVaultIDAssigned(); // MUST before prepareDB()
|
|
||||||
|
// MUST before prepareDB()
|
||||||
|
// And, it's also possible to be an empty string,
|
||||||
|
// which means the vaultRandomID is read from db later!
|
||||||
|
const vaultRandomIDFromOldConfigFile =
|
||||||
|
await this.getVaultRandomIDFromOldConfigFile();
|
||||||
|
|
||||||
// no need to await this
|
// no need to await this
|
||||||
this.tryToAddIgnoreFile();
|
this.tryToAddIgnoreFile();
|
||||||
|
|
||||||
|
const vaultBasePath = this.getVaultBasePath();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await this.prepareDB();
|
await this.prepareDBAndVaultRandomID(
|
||||||
|
vaultBasePath,
|
||||||
|
vaultRandomIDFromOldConfigFile
|
||||||
|
);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
new Notice(err.message, 10 * 1000);
|
new Notice(err.message, 10 * 1000);
|
||||||
throw err;
|
throw err;
|
||||||
@ -403,7 +417,7 @@ export default class RemotelySavePlugin extends Plugin {
|
|||||||
await insertDeleteRecordByVault(
|
await insertDeleteRecordByVault(
|
||||||
this.db,
|
this.db,
|
||||||
fileOrFolder,
|
fileOrFolder,
|
||||||
this.settings.vaultRandomID
|
this.vaultRandomID
|
||||||
);
|
);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
@ -414,7 +428,7 @@ export default class RemotelySavePlugin extends Plugin {
|
|||||||
this.db,
|
this.db,
|
||||||
fileOrFolder,
|
fileOrFolder,
|
||||||
oldPath,
|
oldPath,
|
||||||
this.settings.vaultRandomID
|
this.vaultRandomID
|
||||||
);
|
);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
@ -757,14 +771,20 @@ export default class RemotelySavePlugin extends Plugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async checkIfVaultIDAssigned() {
|
async getVaultRandomIDFromOldConfigFile() {
|
||||||
if (
|
let vaultRandomID = "";
|
||||||
this.settings.vaultRandomID === undefined ||
|
if (this.settings.vaultRandomID !== undefined) {
|
||||||
this.settings.vaultRandomID === ""
|
// In old version, the vault id is saved in data.json
|
||||||
) {
|
// But we want to store it in localForage later
|
||||||
this.settings.vaultRandomID = nanoid();
|
if (this.settings.vaultRandomID !== "") {
|
||||||
|
// a real string was assigned before
|
||||||
|
vaultRandomID = this.settings.vaultRandomID;
|
||||||
|
}
|
||||||
|
log.debug("vaultRandomID is no longer saved in data.json");
|
||||||
|
delete this.settings.vaultRandomID;
|
||||||
await this.saveSettings();
|
await this.saveSettings();
|
||||||
}
|
}
|
||||||
|
return vaultRandomID;
|
||||||
}
|
}
|
||||||
|
|
||||||
async trash(x: string) {
|
async trash(x: string) {
|
||||||
@ -773,8 +793,26 @@ export default class RemotelySavePlugin extends Plugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async prepareDB() {
|
getVaultBasePath() {
|
||||||
this.db = await prepareDBs(this.settings.vaultRandomID);
|
if (this.app.vault.adapter instanceof FileSystemAdapter) {
|
||||||
|
// in desktop
|
||||||
|
return this.app.vault.adapter.getBasePath().split("?")[0];
|
||||||
|
} else {
|
||||||
|
// in mobile
|
||||||
|
return this.app.vault.adapter.getResourcePath("").split("?")[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async prepareDBAndVaultRandomID(
|
||||||
|
vaultBasePath: string,
|
||||||
|
vaultRandomIDFromOldConfigFile: string
|
||||||
|
) {
|
||||||
|
const { db, vaultRandomID } = await prepareDBs(
|
||||||
|
vaultBasePath,
|
||||||
|
vaultRandomIDFromOldConfigFile
|
||||||
|
);
|
||||||
|
this.db = db;
|
||||||
|
this.vaultRandomID = vaultRandomID;
|
||||||
}
|
}
|
||||||
|
|
||||||
enableAutoSyncIfSet() {
|
enableAutoSyncIfSet() {
|
||||||
|
|||||||
@ -1479,7 +1479,7 @@ export class RemotelySaveSettingTab extends PluginSettingTab {
|
|||||||
await exportVaultSyncPlansToFiles(
|
await exportVaultSyncPlansToFiles(
|
||||||
this.plugin.db,
|
this.plugin.db,
|
||||||
this.app.vault,
|
this.app.vault,
|
||||||
this.plugin.settings.vaultRandomID
|
this.plugin.vaultRandomID
|
||||||
);
|
);
|
||||||
new Notice(t("settings_syncplans_notice"));
|
new Notice(t("settings_syncplans_notice"));
|
||||||
});
|
});
|
||||||
@ -1507,6 +1507,18 @@ export class RemotelySaveSettingTab extends PluginSettingTab {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const outputCurrBasePathVaultIDDiv = debugDiv.createDiv("div");
|
||||||
|
new Setting(outputCurrBasePathVaultIDDiv)
|
||||||
|
.setName(t("settings_outputbasepathvaultid"))
|
||||||
|
.setDesc(t("settings_outputbasepathvaultid_desc"))
|
||||||
|
.addButton(async (button) => {
|
||||||
|
button.setButtonText(t("settings_outputbasepathvaultid_button"));
|
||||||
|
button.onClick(async () => {
|
||||||
|
new Notice(this.plugin.getVaultBasePath());
|
||||||
|
new Notice(this.plugin.vaultRandomID);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
const dbsResetDiv = debugDiv.createEl("div");
|
const dbsResetDiv = debugDiv.createEl("div");
|
||||||
new Setting(dbsResetDiv)
|
new Setting(dbsResetDiv)
|
||||||
.setName(t("settings_resetcache"))
|
.setName(t("settings_resetcache"))
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user