abstract getSyncPlan

This commit is contained in:
fyears 2021-11-07 00:39:57 +08:00
parent d3b812c8fa
commit 254790cd61
2 changed files with 51 additions and 22 deletions

View File

@ -22,7 +22,7 @@ import {
} from "./localdb";
import type { SyncStatusType } from "./sync";
import { ensembleMixedStates, getOperation, doActualSync } from "./sync";
import { getSyncPlan, doActualSync } from "./sync";
import { DEFAULT_S3_CONFIG, getS3Client, listFromRemote, S3Config } from "./s3";
interface SaveRemotePluginSettings {
@ -87,19 +87,14 @@ export default class SaveRemotePlugin extends Plugin {
new Notice("4/6 Starting to generate sync plan.");
this.syncStatus = "generating_plan";
const mixedStates = await ensembleMixedStates(
const syncPlan = await getSyncPlan(
remoteRsp.Contents,
local,
localHistory,
this.db,
this.settings.password
);
for (const [key, val] of Object.entries(mixedStates)) {
getOperation(val, true);
}
console.log(mixedStates);
console.log(syncPlan.mixedStates); // for debugging
// The operations above are read only and kind of safe.
// The operations below begins to write or delete (!!!) something.
@ -112,7 +107,7 @@ export default class SaveRemotePlugin extends Plugin {
this.settings.s3,
this.db,
this.app.vault,
mixedStates,
syncPlan,
this.settings.password
);
@ -262,5 +257,7 @@ class SaveRemoteSettingTab extends PluginSettingTab {
await this.plugin.saveSettings();
})
);
containerEl.createEl("h2", { text: "Debug" });
}
}

View File

@ -16,9 +16,18 @@ import {
deleteFromRemote,
downloadFromRemote,
} from "./s3";
import { mkdirpInVault } from "./misc";
import { mkdirpInVault, SUPPORTED_SERVICES_TYPE } from "./misc";
import { decryptBase32ToString, encryptStringToBase32 } from "./encrypt";
export type SyncStatusType =
| "idle"
| "preparing"
| "getting_remote_meta"
| "getting_local_meta"
| "generating_plan"
| "syncing"
| "finish";
type DecisionType =
| "undecided"
| "unknown"
@ -31,15 +40,6 @@ type DecisionType =
| "mkdirplocal"
| "skip";
export type SyncStatusType =
| "idle"
| "preparing"
| "getting_remote_meta"
| "getting_local_meta"
| "generating_plan"
| "syncing"
| "finish";
interface FileOrFolderMixedState {
key: string;
exist_local?: boolean;
@ -55,7 +55,13 @@ interface FileOrFolderMixedState {
remote_encrypted_key?: string;
}
export const ensembleMixedStates = async (
interface SyncPlanType {
ts: number;
remoteType: SUPPORTED_SERVICES_TYPE;
mixedStates: Record<string, FileOrFolderMixedState>;
}
const ensembleMixedStates = async (
remote: S3ObjectType[],
local: TAbstractFile[],
deleteHistory: FileFolderHistoryRecord[],
@ -173,7 +179,7 @@ export const ensembleMixedStates = async (
return results;
};
export const getOperation = (
const getOperation = (
origRecord: FileOrFolderMixedState,
inplace: boolean = false
) => {
@ -289,14 +295,40 @@ export const getOperation = (
return r;
};
export const getSyncPlan = async (
remote: S3ObjectType[],
local: TAbstractFile[],
deleteHistory: FileFolderHistoryRecord[],
db: lf.DatabaseConnection,
password: string = ""
) => {
const mixedStates = await ensembleMixedStates(
remote,
local,
deleteHistory,
db,
password
);
for (const [key, val] of Object.entries(mixedStates)) {
getOperation(val, true);
}
const plan = {
ts: Date.now(),
remoteType: "s3",
mixedStates: mixedStates,
} as SyncPlanType;
return plan;
};
export const doActualSync = async (
s3Client: S3Client,
s3Config: S3Config,
db: lf.DatabaseConnection,
vault: Vault,
keyStates: Record<string, FileOrFolderMixedState>,
syncPlan: SyncPlanType,
password: string = ""
) => {
const keyStates = syncPlan.mixedStates;
await Promise.all(
Object.entries(keyStates)
.sort((k, v) => -(k as string).length)