fix(electron): export and import (#9767)

This commit is contained in:
forehalo
2025-01-20 08:48:03 +00:00
parent 2e18ae59e3
commit cb53baca89
26 changed files with 332 additions and 453 deletions
@@ -1,27 +1,63 @@
import path from 'node:path';
import { parseUniversalId } from '@affine/nbstore';
import fs from 'fs-extra';
import { logger } from '../logger';
import { ensureSQLiteDB } from '../nbstore/v1/ensure-db';
import { getDocStoragePool } from '../nbstore';
import { ensureSQLiteDisconnected } from '../nbstore/v1/ensure-db';
import type { WorkspaceMeta } from '../type';
import {
getDeletedWorkspacesBasePath,
getSpaceDBPath,
getWorkspaceBasePathV1,
getWorkspaceMeta,
} from './meta';
export async function deleteWorkspace(id: string) {
const basePath = await getWorkspaceBasePathV1('workspace', id);
async function deleteWorkspaceV1(workspaceId: string) {
try {
await ensureSQLiteDisconnected('workspace', workspaceId);
const basePath = await getWorkspaceBasePathV1('workspace', workspaceId);
await fs.rmdir(basePath, { recursive: true });
} catch (error) {
logger.error('deleteWorkspaceV1', error);
}
}
/**
* Permanently delete the workspace data
*/
export async function deleteWorkspace(universalId: string) {
const { peer, type, id } = parseUniversalId(universalId);
await deleteWorkspaceV1(id);
const dbPath = await getSpaceDBPath(peer, type, id);
try {
await getDocStoragePool().disconnect(universalId);
await fs.rmdir(path.dirname(dbPath), { recursive: true });
} catch (e) {
logger.error('deleteWorkspace', e);
}
}
/**
* Move the workspace folder to `deleted-workspaces`
* At the same time, permanently delete the v1 workspace folder if it's id exists in nbstore,
* because trashing always happens after full sync from v1 to nbstore.
*/
export async function trashWorkspace(universalId: string) {
const { peer, type, id } = parseUniversalId(universalId);
await deleteWorkspaceV1(id);
const dbPath = await getSpaceDBPath(peer, type, id);
const movedPath = path.join(await getDeletedWorkspacesBasePath(), `${id}`);
try {
const db = await ensureSQLiteDB('workspace', id);
await db.destroy();
return await fs.move(basePath, movedPath, {
await getDocStoragePool().disconnect(universalId);
return await fs.move(path.dirname(dbPath), movedPath, {
overwrite: true,
});
} catch (error) {
logger.error('deleteWorkspace', error);
logger.error('trashWorkspace', error);
}
}
@@ -1,5 +1,5 @@
import type { MainEventRegister } from '../type';
import { deleteWorkspace } from './handlers';
import { deleteWorkspace, trashWorkspace } from './handlers';
export * from './handlers';
export * from './subjects';
@@ -7,5 +7,6 @@ export * from './subjects';
export const workspaceEvents = {} as Record<string, MainEventRegister>;
export const workspaceHandlers = {
delete: async (id: string) => deleteWorkspace(id),
delete: deleteWorkspace,
moveToTrash: trashWorkspace,
};
@@ -1,10 +1,8 @@
import path from 'node:path';
import type { SpaceType } from '@affine/nbstore';
import fs from 'fs-extra';
import { type SpaceType } from '@affine/nbstore';
import { isWindows } from '../../shared/utils';
import { logger } from '../logger';
import { mainRPC } from '../main-rpc';
import type { WorkspaceMeta } from '../type';
@@ -94,31 +92,10 @@ export async function getWorkspaceMeta(
spaceType: SpaceType,
workspaceId: string
): Promise<WorkspaceMeta> {
try {
const basePath = await getWorkspaceBasePathV1(spaceType, workspaceId);
const metaPath = await getWorkspaceMetaPath(spaceType, workspaceId);
if (
!(await fs
.access(metaPath)
.then(() => true)
.catch(() => false))
) {
await fs.ensureDir(basePath);
const dbPath = await getWorkspaceDBPath(spaceType, workspaceId);
// create one if not exists
const meta = {
id: workspaceId,
mainDBPath: dbPath,
type: spaceType,
};
await fs.writeJSON(metaPath, meta);
return meta;
} else {
const meta = await fs.readJSON(metaPath);
return meta;
}
} catch (err) {
logger.error('getWorkspaceMeta failed', err);
throw err;
}
const dbPath = await getWorkspaceDBPath(spaceType, workspaceId);
return {
mainDBPath: dbPath,
id: workspaceId,
};
}