@huyooo/file-explorer-core 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,492 @@
1
+ import { Stats } from 'node:fs';
2
+
3
+ /**
4
+ * 文件类型枚举
5
+ */
6
+ declare enum FileType {
7
+ FOLDER = "folder",
8
+ FILE = "file",
9
+ IMAGE = "image",
10
+ VIDEO = "video",
11
+ MUSIC = "music",
12
+ DOCUMENT = "document",
13
+ CODE = "code",
14
+ TEXT = "text",
15
+ ARCHIVE = "archive",
16
+ APPLICATION = "application",
17
+ UNKNOWN = "unknown"
18
+ }
19
+ /**
20
+ * 文件系统项
21
+ */
22
+ interface FileItem {
23
+ /** 唯一标识(通常是完整路径) */
24
+ id: string;
25
+ /** 文件名 */
26
+ name: string;
27
+ /** 文件类型 */
28
+ type: FileType;
29
+ /** 文件大小(格式化后的字符串) */
30
+ size?: string;
31
+ /** 修改日期(格式化后的字符串) */
32
+ dateModified?: string;
33
+ /** 文件 URL(用于加载) */
34
+ url?: string;
35
+ /** 缩略图 URL */
36
+ thumbnailUrl?: string;
37
+ /** 子项(仅文件夹) */
38
+ children?: FileItem[];
39
+ }
40
+ /**
41
+ * 文件信息
42
+ */
43
+ interface FileInfo {
44
+ path: string;
45
+ name: string;
46
+ size: number;
47
+ isFile: boolean;
48
+ isDirectory: boolean;
49
+ createdAt: Date;
50
+ updatedAt: Date;
51
+ extension?: string;
52
+ }
53
+ /**
54
+ * 操作结果
55
+ */
56
+ interface OperationResult<T = void> {
57
+ success: boolean;
58
+ data?: T;
59
+ error?: string;
60
+ message?: string;
61
+ }
62
+ /**
63
+ * 系统路径 ID
64
+ */
65
+ type SystemPathId = 'desktop' | 'documents' | 'downloads' | 'pictures' | 'music' | 'videos' | 'applications' | 'home' | 'root';
66
+ /**
67
+ * 平台适配器接口
68
+ * 用于处理平台特定的操作(如 Electron 的 shell.trashItem)
69
+ */
70
+ interface PlatformAdapter {
71
+ /** 删除文件到回收站 */
72
+ trashItem?: (path: string) => Promise<void>;
73
+ /** 打开文件 */
74
+ openPath?: (path: string) => Promise<void>;
75
+ /** 使用指定应用打开 */
76
+ openWith?: (path: string, appPath: string) => Promise<void>;
77
+ }
78
+ /**
79
+ * 文件操作选项
80
+ */
81
+ interface FileOperationOptions {
82
+ /** 平台适配器 */
83
+ adapter?: PlatformAdapter;
84
+ /** 是否自动重命名(当目标存在时) */
85
+ autoRename?: boolean;
86
+ }
87
+
88
+ /**
89
+ * 根据文件路径和状态获取文件类型
90
+ */
91
+ declare function getFileType(filePath: string, stats: Stats): FileType;
92
+ /**
93
+ * 判断是否为媒体文件
94
+ */
95
+ declare function isMediaFile(type: FileType): boolean;
96
+ /**
97
+ * 判断是否为可预览的文件
98
+ */
99
+ declare function isPreviewable(type: FileType): boolean;
100
+
101
+ /**
102
+ * 格式化文件大小
103
+ */
104
+ declare function formatFileSize(bytes: number): string;
105
+ /**
106
+ * 格式化日期
107
+ */
108
+ declare function formatDate(date: Date): string;
109
+ /**
110
+ * 格式化日期时间
111
+ */
112
+ declare function formatDateTime(date: Date): string;
113
+
114
+ /**
115
+ * URL 编码器(可由外部注入)
116
+ */
117
+ type UrlEncoder = (filePath: string) => string;
118
+ /**
119
+ * 读取目录配置
120
+ */
121
+ interface ReadDirectoryOptions {
122
+ /** URL 编码器 */
123
+ urlEncoder?: UrlEncoder;
124
+ /** 是否包含隐藏文件 */
125
+ includeHidden?: boolean;
126
+ /** 缩略图 URL 获取器(同步获取缓存的缩略图,没有则返回 null 并异步生成) */
127
+ getThumbnailUrl?: (filePath: string) => Promise<string | null>;
128
+ }
129
+ /**
130
+ * 读取目录内容
131
+ */
132
+ declare function readDirectory(dirPath: string, options?: ReadDirectoryOptions): Promise<FileItem[]>;
133
+ /**
134
+ * 读取文件内容
135
+ */
136
+ declare function readFileContent(filePath: string): Promise<string>;
137
+ /**
138
+ * 读取图片为 Base64
139
+ */
140
+ declare function readImageAsBase64(imagePath: string): Promise<{
141
+ success: boolean;
142
+ base64?: string;
143
+ mimeType?: string;
144
+ error?: string;
145
+ }>;
146
+
147
+ /**
148
+ * 写入文件内容
149
+ */
150
+ declare function writeFileContent(filePath: string, content: string): Promise<OperationResult>;
151
+ /**
152
+ * 创建文件夹(自动处理同名文件夹)
153
+ */
154
+ declare function createFolder(folderPath: string): Promise<OperationResult<{
155
+ finalPath: string;
156
+ }>>;
157
+ /**
158
+ * 创建文件(自动处理同名文件)
159
+ */
160
+ declare function createFile(filePath: string, content?: string): Promise<OperationResult<{
161
+ finalPath: string;
162
+ }>>;
163
+
164
+ /**
165
+ * 删除文件选项
166
+ */
167
+ interface DeleteOptions {
168
+ /** 平台适配器(用于移到回收站) */
169
+ adapter?: PlatformAdapter;
170
+ /** 是否使用回收站(默认 true) */
171
+ useTrash?: boolean;
172
+ /** 删除后回调(用于清理缩略图等) */
173
+ onDeleted?: (path: string) => void;
174
+ }
175
+ /**
176
+ * 删除文件/文件夹
177
+ *
178
+ * 如果提供了 adapter.trashItem,则移到回收站
179
+ * 否则直接删除(危险操作)
180
+ */
181
+ declare function deleteFiles(paths: string[], options?: DeleteOptions): Promise<OperationResult>;
182
+
183
+ /**
184
+ * 重命名选项
185
+ */
186
+ interface RenameOptions {
187
+ /** 重命名后回调 */
188
+ onRenamed?: (oldPath: string, newPath: string) => void;
189
+ }
190
+ /**
191
+ * 重命名文件/文件夹
192
+ */
193
+ declare function renameFile(oldPath: string, newPath: string, options?: RenameOptions): Promise<OperationResult>;
194
+
195
+ /**
196
+ * 复制文件到目标目录
197
+ */
198
+ declare function copyFiles(sourcePaths: string[], targetDir: string): Promise<OperationResult<{
199
+ copiedPaths: string[];
200
+ }>>;
201
+ /**
202
+ * 移动文件到目标目录
203
+ */
204
+ declare function moveFiles(sourcePaths: string[], targetDir: string): Promise<OperationResult<{
205
+ movedPaths: string[];
206
+ }>>;
207
+
208
+ /**
209
+ * 获取文件信息
210
+ */
211
+ declare function getFileInfo(filePath: string): Promise<OperationResult<FileInfo>>;
212
+ /**
213
+ * 检查文件/目录是否存在
214
+ */
215
+ declare function exists(filePath: string): Promise<boolean>;
216
+ /**
217
+ * 检查是否为目录
218
+ */
219
+ declare function isDirectory(filePath: string): Promise<boolean>;
220
+
221
+ /**
222
+ * 获取系统路径
223
+ */
224
+ declare function getSystemPath(pathId: SystemPathId): string | null;
225
+ /**
226
+ * 获取所有系统路径
227
+ */
228
+ declare function getAllSystemPaths(): Record<SystemPathId, string | null>;
229
+ /**
230
+ * 获取用户主目录
231
+ */
232
+ declare function getHomeDirectory(): string;
233
+ /**
234
+ * 获取当前平台
235
+ */
236
+ declare function getPlatform(): NodeJS.Platform;
237
+
238
+ /**
239
+ * 使用 fdir 快速搜索文件和文件夹
240
+ * fdir 可以在 1 秒内扫描 100 万个文件
241
+ *
242
+ * @param searchPath 搜索路径
243
+ * @param pattern 搜索模式(支持 * 通配符,忽略大小写)
244
+ * @param maxDepth 最大递归深度
245
+ */
246
+ declare function searchFiles(searchPath: string, pattern?: string, maxDepth?: number): Promise<string[]>;
247
+ /**
248
+ * 流式搜索文件(逐层搜索,边搜边返回)
249
+ *
250
+ * @param searchPath 搜索路径
251
+ * @param pattern 搜索模式
252
+ * @param onResults 找到结果时的回调
253
+ * @param maxResults 最大结果数
254
+ */
255
+ declare function searchFilesStream(searchPath: string, pattern: string, onResults: (paths: string[], done: boolean) => void, maxResults?: number): Promise<void>;
256
+ /**
257
+ * 同步搜索(用于小目录)
258
+ */
259
+ declare function searchFilesSync(searchPath: string, pattern?: string, maxDepth?: number): string[];
260
+
261
+ /**
262
+ * 计算文件的快速 hash(使用文件大小和修改时间)
263
+ * 这比计算完整文件 hash 快得多,适合用于缓存判断
264
+ */
265
+ declare function getFileHash(filePath: string): Promise<string>;
266
+ /**
267
+ * 批量计算文件 hash
268
+ */
269
+ declare function getFileHashes(filePaths: string[]): Promise<Map<string, string>>;
270
+
271
+ /**
272
+ * 剪贴板文件操作接口
273
+ * 由于 clipboard-files 是 native 模块,由使用者在 Electron 主进程中注入
274
+ */
275
+ interface ClipboardAdapter {
276
+ writeFiles: (paths: string[]) => void;
277
+ readFiles: () => string[];
278
+ }
279
+ /**
280
+ * 复制文件到剪贴板
281
+ */
282
+ declare function copyFilesToClipboard(filePaths: string[], clipboard: ClipboardAdapter): Promise<OperationResult>;
283
+ /**
284
+ * 从剪贴板读取文件路径
285
+ */
286
+ declare function getClipboardFiles(clipboard: ClipboardAdapter): OperationResult<{
287
+ files: string[];
288
+ }>;
289
+ /**
290
+ * 粘贴文件到目标目录
291
+ */
292
+ declare function pasteFiles(targetDir: string, sourcePaths: string[]): Promise<OperationResult<{
293
+ pastedPaths: string[];
294
+ }>>;
295
+
296
+ /**
297
+ * 应用程序图标获取(macOS)
298
+ *
299
+ * 纯 Node.js 实现,使用 sips 命令转换 icns 为 PNG
300
+ */
301
+ /**
302
+ * 获取应用程序图标(仅 macOS)
303
+ *
304
+ * 使用 sips 命令将 .icns 转换为 PNG,返回 base64 data URL
305
+ */
306
+ declare function getApplicationIcon(appPath: string): Promise<string | null>;
307
+
308
+ /**
309
+ * 缩略图数据库接口
310
+ */
311
+ interface ThumbnailDatabase {
312
+ /** 获取缓存目录 */
313
+ getCacheDir(): string;
314
+ /** 快速查询缩略图(只用路径和修改时间) */
315
+ getThumbnailPathFast(filePath: string, mtime: number): string | null;
316
+ /** 获取缩略图路径(如果缓存有效) */
317
+ getThumbnailPath(filePath: string, fileHash: string, mtime: number): string | null;
318
+ /** 保存缩略图信息 */
319
+ saveThumbnail(filePath: string, fileHash: string, mtime: number, thumbnailPath: string): void;
320
+ /** 删除缩略图记录 */
321
+ deleteThumbnail(filePath: string): void;
322
+ /** 清理旧缩略图 */
323
+ cleanupOldThumbnails(): void;
324
+ }
325
+ /**
326
+ * 图片处理器接口
327
+ */
328
+ interface ImageProcessor {
329
+ /** 缩放图片 */
330
+ resize(filePath: string, outputPath: string, size: number): Promise<void>;
331
+ }
332
+ /**
333
+ * 视频处理器接口
334
+ */
335
+ interface VideoProcessor {
336
+ /** 截取视频帧 */
337
+ screenshot(filePath: string, outputPath: string, timestamp: string, size: string): Promise<void>;
338
+ }
339
+ /**
340
+ * 缩略图服务配置
341
+ */
342
+ interface ThumbnailServiceOptions {
343
+ /** 数据库实例 */
344
+ database: ThumbnailDatabase;
345
+ /** 图片处理器(可选) */
346
+ imageProcessor?: ImageProcessor;
347
+ /** 视频处理器(可选) */
348
+ videoProcessor?: VideoProcessor;
349
+ /** URL 编码器(将本地路径转为 URL) */
350
+ urlEncoder?: (filePath: string) => string;
351
+ /** 应用图标获取器(macOS) */
352
+ getApplicationIcon?: (appPath: string) => Promise<string | null>;
353
+ }
354
+
355
+ /**
356
+ * 缩略图服务
357
+ */
358
+ declare class ThumbnailService {
359
+ private database;
360
+ private imageProcessor;
361
+ private videoProcessor;
362
+ private urlEncoder;
363
+ private getApplicationIcon;
364
+ constructor(options: ThumbnailServiceOptions);
365
+ /**
366
+ * 获取缓存的缩略图 URL(不生成新的)
367
+ */
368
+ getCachedThumbnailUrl(filePath: string): Promise<string | null>;
369
+ /**
370
+ * 获取缩略图 URL(如果没有缓存则生成)
371
+ */
372
+ getThumbnailUrl(filePath: string): Promise<string | null>;
373
+ /**
374
+ * 生成缩略图
375
+ */
376
+ generateThumbnail(filePath: string, fileHash: string, mtime: number): Promise<string | null>;
377
+ /**
378
+ * 批量生成缩略图
379
+ */
380
+ generateThumbnailsBatch(files: Array<{
381
+ path: string;
382
+ hash: string;
383
+ mtime: number;
384
+ }>): Promise<void>;
385
+ /**
386
+ * 删除缩略图
387
+ */
388
+ deleteThumbnail(filePath: string): void;
389
+ /**
390
+ * 清理旧缩略图
391
+ */
392
+ cleanupOldThumbnails(): void;
393
+ }
394
+ /**
395
+ * 初始化缩略图服务
396
+ */
397
+ declare function initThumbnailService(options: ThumbnailServiceOptions): ThumbnailService;
398
+ /**
399
+ * 获取缩略图服务实例
400
+ */
401
+ declare function getThumbnailService(): ThumbnailService | null;
402
+
403
+ /**
404
+ * SQLite 缩略图数据库实现
405
+ *
406
+ * 需要安装 better-sqlite3:npm install better-sqlite3
407
+ */
408
+
409
+ /**
410
+ * SQLite 缩略图数据库实现
411
+ */
412
+ declare class SqliteThumbnailDatabase implements ThumbnailDatabase {
413
+ private db;
414
+ private cacheDir;
415
+ constructor(userDataPath: string);
416
+ /**
417
+ * 初始化数据库
418
+ */
419
+ init(): void;
420
+ getCacheDir(): string;
421
+ /**
422
+ * 快速查询缩略图(用路径和修改时间,不需要哈希)
423
+ * 比计算文件哈希快很多
424
+ */
425
+ getThumbnailPathFast(filePath: string, mtime: number): string | null;
426
+ getThumbnailPath(filePath: string, fileHash: string, mtime: number): string | null;
427
+ saveThumbnail(filePath: string, fileHash: string, mtime: number, thumbnailPath: string): void;
428
+ deleteThumbnail(filePath: string): void;
429
+ cleanupOldThumbnails(): void;
430
+ close(): void;
431
+ }
432
+ /**
433
+ * 创建 SQLite 缩略图数据库
434
+ */
435
+ declare function createSqliteThumbnailDatabase(userDataPath: string): SqliteThumbnailDatabase;
436
+ /**
437
+ * 获取缩略图数据库实例
438
+ */
439
+ declare function getSqliteThumbnailDatabase(): SqliteThumbnailDatabase | null;
440
+
441
+ /**
442
+ * 缩略图处理器工厂函数
443
+ *
444
+ * 需要安装:
445
+ * - sharp: npm install sharp
446
+ * - ffmpeg-static: npm install ffmpeg-static
447
+ */
448
+
449
+ /**
450
+ * Sharp 类型定义(宽松类型,兼容 sharp 模块)
451
+ */
452
+ interface SharpInstance {
453
+ resize(width: number, height: number, options?: {
454
+ fit?: string;
455
+ withoutEnlargement?: boolean;
456
+ }): SharpInstance;
457
+ jpeg(options?: {
458
+ quality?: number;
459
+ }): SharpInstance;
460
+ toFile(outputPath: string): Promise<unknown>;
461
+ }
462
+ type SharpModule = (input: string, options?: any) => SharpInstance;
463
+ /**
464
+ * 创建 Sharp 图片处理器
465
+ *
466
+ * @example
467
+ * ```ts
468
+ * import sharp from 'sharp';
469
+ * import { createSharpImageProcessor } from '@huyooo/file-explorer-core';
470
+ *
471
+ * const imageProcessor = createSharpImageProcessor(sharp);
472
+ * ```
473
+ */
474
+ declare function createSharpImageProcessor(sharp: SharpModule): ImageProcessor;
475
+ /**
476
+ * 创建 FFmpeg 视频处理器(使用 child_process 直接调用 ffmpeg)
477
+ *
478
+ * @param ffmpegPath - ffmpeg 可执行文件路径(来自 ffmpeg-static)
479
+ *
480
+ * @example
481
+ * ```ts
482
+ * import ffmpegStatic from 'ffmpeg-static';
483
+ * import { createFfmpegVideoProcessor } from '@huyooo/file-explorer-core';
484
+ *
485
+ * const videoProcessor = ffmpegStatic
486
+ * ? createFfmpegVideoProcessor(ffmpegStatic)
487
+ * : undefined;
488
+ * ```
489
+ */
490
+ declare function createFfmpegVideoProcessor(ffmpegPath: string): VideoProcessor;
491
+
492
+ export { type ClipboardAdapter, type DeleteOptions, type FileInfo, type FileItem, type FileOperationOptions, FileType, type ImageProcessor, type OperationResult, type PlatformAdapter, type ReadDirectoryOptions, type RenameOptions, SqliteThumbnailDatabase, type SystemPathId, type ThumbnailDatabase, ThumbnailService, type ThumbnailServiceOptions, type UrlEncoder, type VideoProcessor, copyFiles, copyFilesToClipboard, createFfmpegVideoProcessor, createFile, createFolder, createSharpImageProcessor, createSqliteThumbnailDatabase, deleteFiles, exists, formatDate, formatDateTime, formatFileSize, getAllSystemPaths, getApplicationIcon, getClipboardFiles, getFileHash, getFileHashes, getFileInfo, getFileType, getHomeDirectory, getPlatform, getSqliteThumbnailDatabase, getSystemPath, getThumbnailService, initThumbnailService, isDirectory, isMediaFile, isPreviewable, moveFiles, pasteFiles, readDirectory, readFileContent, readImageAsBase64, renameFile, searchFiles, searchFilesStream, searchFilesSync, writeFileContent };