mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-30 00:29:46 +08:00
feat(core): import progress & perf (#15197)
#### PR Dependency Tree * **PR #15197** 👈 This tree was auto-generated by [Charcoal](https://github.com/danerwilliams/charcoal) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added a new import pipeline with “plan then commit” batch handling for Markdown, Notion HTML, Obsidian, and Bear backups. * Enabled native import sessions with progress, cancellation, and batch-by-batch committing (including assets, folders, icons, and tags). * Added web preflight limits for ZIP and multi-file imports. * **Bug Fixes** * Improved import error/warning reporting and continued processing when some items fail. * Strengthened snapshot-based file/directory picking to preserve paths. * **Chores** * Updated project packaging/configuration for the new import workflow. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
@@ -3,10 +3,14 @@ import type { MessagePort } from 'node:worker_threads';
|
||||
|
||||
import type { EventBasedChannel } from 'async-call-rpc';
|
||||
import { AsyncCall } from 'async-call-rpc';
|
||||
import { ipcRenderer } from 'electron';
|
||||
import { ipcRenderer, webUtils } from 'electron';
|
||||
import { Subject } from 'rxjs';
|
||||
import { z } from 'zod';
|
||||
|
||||
import type {
|
||||
CreateImportSessionFromSourceOptions,
|
||||
NativeImportBrowserSource,
|
||||
} from '../shared/import';
|
||||
import {
|
||||
AFFINE_API_CHANNEL_NAME,
|
||||
AFFINE_EVENT_CHANNEL_NAME,
|
||||
@@ -275,9 +279,61 @@ function getHelperAPIs() {
|
||||
const mainAPIs = getMainAPIs();
|
||||
const helperAPIs = getHelperAPIs();
|
||||
|
||||
type DirectoryImportFile = File & { webkitRelativePath?: string };
|
||||
|
||||
function filePathFromFile(file: File) {
|
||||
return webUtils.getPathForFile(file);
|
||||
}
|
||||
|
||||
function directoryPathFromFiles(files: File[]) {
|
||||
const first = files.find(
|
||||
(file): file is DirectoryImportFile =>
|
||||
!!(file as DirectoryImportFile).webkitRelativePath
|
||||
);
|
||||
if (!first) return null;
|
||||
const filePath = filePathFromFile(first);
|
||||
if (!filePath) return null;
|
||||
const relativePath = first.webkitRelativePath;
|
||||
if (!relativePath) return null;
|
||||
const relativeParts = relativePath.split('/');
|
||||
let rootPath = filePath.replaceAll('\\', '/');
|
||||
for (let i = relativeParts.length - 1; i > 0; i--) {
|
||||
const part = relativeParts[i];
|
||||
if (part && rootPath.endsWith(`/${part}`)) {
|
||||
rootPath = rootPath.slice(0, -part.length - 1);
|
||||
}
|
||||
}
|
||||
return rootPath || null;
|
||||
}
|
||||
|
||||
function resolveNativeImportSource(source: NativeImportBrowserSource) {
|
||||
if (source.kind === 'file') {
|
||||
const path = filePathFromFile(source.file);
|
||||
return path ? { kind: 'filePath', path } : null;
|
||||
}
|
||||
const path = directoryPathFromFiles(source.files);
|
||||
return path ? { kind: 'directoryPath', path } : null;
|
||||
}
|
||||
|
||||
export const apis = {
|
||||
...mainAPIs.apis,
|
||||
...helperAPIs.apis,
|
||||
import: {
|
||||
...mainAPIs.apis.import,
|
||||
createImportSessionFromSource(
|
||||
options: CreateImportSessionFromSourceOptions
|
||||
) {
|
||||
const source = resolveNativeImportSource(options.source);
|
||||
if (!source) {
|
||||
throw new Error('Native import requires a local file source');
|
||||
}
|
||||
return mainAPIs.apis.import.createImportSession({
|
||||
format: options.format,
|
||||
source,
|
||||
batchLimits: options.batchLimits,
|
||||
});
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export const events = {
|
||||
|
||||
Reference in New Issue
Block a user