mirror of
https://github.com/remotely-save/remotely-save.git
synced 2024-06-07 21:10:45 +00:00
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;
|
||||
serviceType: SUPPORTED_SERVICES_TYPE;
|
||||
currLogLevel?: string;
|
||||
vaultRandomID?: string;
|
||||
autoRunEveryMilliseconds?: number;
|
||||
initRunAfterMilliseconds?: number;
|
||||
agreeToUploadExtraMetadata?: boolean;
|
||||
@ -74,6 +73,11 @@ export interface RemotelySavePluginSettings {
|
||||
syncConfigDir?: boolean;
|
||||
syncUnderscoreItems?: boolean;
|
||||
lang?: LangTypeAndAuto;
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
vaultRandomID?: string;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
import * as origLog from "loglevel";
|
||||
import { nanoid } from "nanoid";
|
||||
const log = origLog.getLogger("rs-default");
|
||||
|
||||
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_SYNC_MAPPING = "syncmetadatahistory";
|
||||
export const DEFAULT_SYNC_PLANS_HISTORY = "syncplanshistory";
|
||||
export const DEFAULT_TBL_VAULT_RANDOM_ID_MAPPING = "vaultrandomidmapping";
|
||||
|
||||
export interface FileFolderHistoryRecord {
|
||||
key: string;
|
||||
@ -54,6 +56,7 @@ export interface InternalDBs {
|
||||
fileHistoryTbl: LocalForage;
|
||||
syncMappingTbl: LocalForage;
|
||||
syncPlansTbl: LocalForage;
|
||||
vaultRandomIDMappingTbl: LocalForage;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -183,7 +186,10 @@ const migrateDBs = async (
|
||||
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 = {
|
||||
versionTbl: localforage.createInstance({
|
||||
name: DEFAULT_DB_NAME,
|
||||
@ -201,8 +207,41 @@ export const prepareDBs = async (vaultRandomID: string) => {
|
||||
name: DEFAULT_DB_NAME,
|
||||
storeName: DEFAULT_SYNC_PLANS_HISTORY,
|
||||
}),
|
||||
vaultRandomIDMappingTbl: localforage.createInstance({
|
||||
name: DEFAULT_DB_NAME,
|
||||
storeName: DEFAULT_TBL_VAULT_RANDOM_ID_MAPPING,
|
||||
}),
|
||||
} 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");
|
||||
if (originalVersion === null) {
|
||||
log.debug(
|
||||
@ -224,7 +263,10 @@ export const prepareDBs = async (vaultRandomID: string) => {
|
||||
}
|
||||
|
||||
log.info("db connected");
|
||||
return db;
|
||||
return {
|
||||
db: db,
|
||||
vaultRandomID: vaultRandomID,
|
||||
};
|
||||
};
|
||||
|
||||
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 { nanoid } from "nanoid";
|
||||
import { createElement, RotateCcw, RefreshCcw } from "lucide";
|
||||
import type { RemotelySavePluginSettings } from "./baseTypes";
|
||||
import {
|
||||
@ -54,7 +61,7 @@ const DEFAULT_SETTINGS: RemotelySavePluginSettings = {
|
||||
password: "",
|
||||
serviceType: "s3",
|
||||
currLogLevel: "info",
|
||||
vaultRandomID: "",
|
||||
// vaultRandomID: "", // deprecated
|
||||
autoRunEveryMilliseconds: -1,
|
||||
initRunAfterMilliseconds: -1,
|
||||
agreeToUploadExtraMetadata: false,
|
||||
@ -104,6 +111,7 @@ export default class RemotelySavePlugin extends Plugin {
|
||||
syncRibbon?: HTMLElement;
|
||||
autoRunIntervalID?: number;
|
||||
i18n: I18n;
|
||||
vaultRandomID: string;
|
||||
|
||||
async syncRun(triggerSource: SyncTriggerSourceType = "manual") {
|
||||
const t = (x: TransItemType, vars?: any) => {
|
||||
@ -216,7 +224,7 @@ export default class RemotelySavePlugin extends Plugin {
|
||||
const { remoteStates, metadataFile } = await parseRemoteItems(
|
||||
remoteRsp.Contents,
|
||||
this.db,
|
||||
this.settings.vaultRandomID,
|
||||
this.vaultRandomID,
|
||||
client.serviceType,
|
||||
this.settings.password
|
||||
);
|
||||
@ -236,7 +244,7 @@ export default class RemotelySavePlugin extends Plugin {
|
||||
const local = this.app.vault.getAllLoadedFiles();
|
||||
const localHistory = await loadFileHistoryTableByVault(
|
||||
this.db,
|
||||
this.settings.vaultRandomID
|
||||
this.vaultRandomID
|
||||
);
|
||||
let localConfigDirContents: ObsConfigDirFileType[] = undefined;
|
||||
if (this.settings.syncConfigDir) {
|
||||
@ -270,11 +278,7 @@ export default class RemotelySavePlugin extends Plugin {
|
||||
);
|
||||
log.info(plan.mixedStates); // for debugging
|
||||
if (triggerSource !== "dry") {
|
||||
await insertSyncPlanRecordByVault(
|
||||
this.db,
|
||||
plan,
|
||||
this.settings.vaultRandomID
|
||||
);
|
||||
await insertSyncPlanRecordByVault(this.db, plan, this.vaultRandomID);
|
||||
}
|
||||
|
||||
// The operations above are almost read only and kind of safe.
|
||||
@ -291,7 +295,7 @@ export default class RemotelySavePlugin extends Plugin {
|
||||
await doActualSync(
|
||||
client,
|
||||
this.db,
|
||||
this.settings.vaultRandomID,
|
||||
this.vaultRandomID,
|
||||
this.app.vault,
|
||||
plan,
|
||||
sortedKeys,
|
||||
@ -384,13 +388,23 @@ export default class RemotelySavePlugin extends Plugin {
|
||||
}
|
||||
|
||||
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
|
||||
this.tryToAddIgnoreFile();
|
||||
|
||||
const vaultBasePath = this.getVaultBasePath();
|
||||
|
||||
try {
|
||||
await this.prepareDB();
|
||||
await this.prepareDBAndVaultRandomID(
|
||||
vaultBasePath,
|
||||
vaultRandomIDFromOldConfigFile
|
||||
);
|
||||
} catch (err) {
|
||||
new Notice(err.message, 10 * 1000);
|
||||
throw err;
|
||||
@ -403,7 +417,7 @@ export default class RemotelySavePlugin extends Plugin {
|
||||
await insertDeleteRecordByVault(
|
||||
this.db,
|
||||
fileOrFolder,
|
||||
this.settings.vaultRandomID
|
||||
this.vaultRandomID
|
||||
);
|
||||
})
|
||||
);
|
||||
@ -414,7 +428,7 @@ export default class RemotelySavePlugin extends Plugin {
|
||||
this.db,
|
||||
fileOrFolder,
|
||||
oldPath,
|
||||
this.settings.vaultRandomID
|
||||
this.vaultRandomID
|
||||
);
|
||||
})
|
||||
);
|
||||
@ -757,14 +771,20 @@ export default class RemotelySavePlugin extends Plugin {
|
||||
}
|
||||
}
|
||||
|
||||
async checkIfVaultIDAssigned() {
|
||||
if (
|
||||
this.settings.vaultRandomID === undefined ||
|
||||
this.settings.vaultRandomID === ""
|
||||
) {
|
||||
this.settings.vaultRandomID = nanoid();
|
||||
async getVaultRandomIDFromOldConfigFile() {
|
||||
let vaultRandomID = "";
|
||||
if (this.settings.vaultRandomID !== undefined) {
|
||||
// In old version, the vault id is saved in data.json
|
||||
// But we want to store it in localForage later
|
||||
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();
|
||||
}
|
||||
return vaultRandomID;
|
||||
}
|
||||
|
||||
async trash(x: string) {
|
||||
@ -773,8 +793,26 @@ export default class RemotelySavePlugin extends Plugin {
|
||||
}
|
||||
}
|
||||
|
||||
async prepareDB() {
|
||||
this.db = await prepareDBs(this.settings.vaultRandomID);
|
||||
getVaultBasePath() {
|
||||
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() {
|
||||
|
@ -1479,7 +1479,7 @@ export class RemotelySaveSettingTab extends PluginSettingTab {
|
||||
await exportVaultSyncPlansToFiles(
|
||||
this.plugin.db,
|
||||
this.app.vault,
|
||||
this.plugin.settings.vaultRandomID
|
||||
this.plugin.vaultRandomID
|
||||
);
|
||||
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");
|
||||
new Setting(dbsResetDiv)
|
||||
.setName(t("settings_resetcache"))
|
||||
|
Loading…
Reference in New Issue
Block a user