@huyooo/file-explorer-core 0.4.5 → 0.4.10

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.
package/dist/index.d.cts DELETED
@@ -1,949 +0,0 @@
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
- * 统一处理 app:// 协议的编码和解码
92
- *
93
- * 协议格式: app://file/path/to/file.jpg
94
- * - scheme: app
95
- * - host: file
96
- * - path: /path/to/file.jpg(路径段编码,保留 /)
97
- */
98
- /** 协议前缀 */
99
- declare const APP_PROTOCOL_SCHEME = "app";
100
- declare const APP_PROTOCOL_HOST = "file";
101
- declare const APP_PROTOCOL_PREFIX = "app://file";
102
- /**
103
- * 编码文件路径为 app:// 协议 URL
104
- * 分段编码路径,保留 / 分隔符,符合 URL 标准
105
- *
106
- * @param filePath - 文件的绝对路径,如 /Users/name/file.jpg
107
- * @returns app:// 协议 URL,如 app://file/Users/name/file.jpg
108
- *
109
- * @example
110
- * encodeFileUrl('/Users/name/my file.jpg')
111
- * // => 'app://file/Users/name/my%20file.jpg'
112
- */
113
- declare function encodeFileUrl(filePath: string): string;
114
- /**
115
- * 解码 app:// 协议 URL 为文件路径
116
- *
117
- * @param url - app:// 协议 URL
118
- * @returns 文件的绝对路径
119
- *
120
- * @example
121
- * decodeFileUrl('app://file/Users/name/my%20file.jpg')
122
- * // => '/Users/name/my file.jpg'
123
- */
124
- declare function decodeFileUrl(url: string): string;
125
- /**
126
- * 检查 URL 是否为 app:// 协议
127
- */
128
- declare function isAppProtocolUrl(url: string): boolean;
129
-
130
- /**
131
- * 根据文件路径和状态获取文件类型
132
- */
133
- declare function getFileType(filePath: string, stats: Stats): FileType;
134
- /**
135
- * 判断是否为媒体文件
136
- */
137
- declare function isMediaFile(type: FileType): boolean;
138
- /**
139
- * 判断是否为可预览的文件
140
- */
141
- declare function isPreviewable(type: FileType): boolean;
142
-
143
- /**
144
- * 格式化文件大小
145
- */
146
- declare function formatFileSize(bytes: number): string;
147
- /**
148
- * 格式化日期
149
- */
150
- declare function formatDate(date: Date): string;
151
- /**
152
- * 格式化日期时间
153
- */
154
- declare function formatDateTime(date: Date): string;
155
-
156
- /**
157
- * URL 编码器(可由外部注入)
158
- */
159
- type UrlEncoder = (filePath: string) => string;
160
- /**
161
- * 读取目录配置
162
- */
163
- interface ReadDirectoryOptions {
164
- /** URL 编码器 */
165
- urlEncoder?: UrlEncoder;
166
- /** 是否包含隐藏文件 */
167
- includeHidden?: boolean;
168
- /** 缩略图 URL 获取器(同步获取缓存的缩略图,没有则返回 null 并异步生成) */
169
- getThumbnailUrl?: (filePath: string) => Promise<string | null>;
170
- }
171
- /**
172
- * 读取目录内容
173
- */
174
- declare function readDirectory(dirPath: string, options?: ReadDirectoryOptions): Promise<FileItem[]>;
175
- /**
176
- * 读取文件内容
177
- */
178
- declare function readFileContent(filePath: string): Promise<string>;
179
- /**
180
- * 读取图片为 Base64
181
- */
182
- declare function readImageAsBase64(imagePath: string): Promise<{
183
- success: boolean;
184
- base64?: string;
185
- mimeType?: string;
186
- error?: string;
187
- }>;
188
-
189
- /**
190
- * 写入文件内容
191
- */
192
- declare function writeFileContent(filePath: string, content: string): Promise<OperationResult>;
193
- /**
194
- * 创建文件夹(自动处理同名文件夹)
195
- */
196
- declare function createFolder(folderPath: string): Promise<OperationResult<{
197
- finalPath: string;
198
- }>>;
199
- /**
200
- * 创建文件(自动处理同名文件)
201
- */
202
- declare function createFile(filePath: string, content?: string): Promise<OperationResult<{
203
- finalPath: string;
204
- }>>;
205
-
206
- /**
207
- * 删除文件选项
208
- */
209
- interface DeleteOptions {
210
- /** 平台适配器(用于移到回收站) */
211
- adapter?: PlatformAdapter;
212
- /** 是否使用回收站(默认 true) */
213
- useTrash?: boolean;
214
- /** 删除后回调(用于清理缩略图等) */
215
- onDeleted?: (path: string) => void;
216
- }
217
- /**
218
- * 删除文件/文件夹
219
- *
220
- * 如果提供了 adapter.trashItem,则移到回收站
221
- * 否则直接删除(危险操作)
222
- */
223
- declare function deleteFiles(paths: string[], options?: DeleteOptions): Promise<OperationResult>;
224
-
225
- /**
226
- * 重命名选项
227
- */
228
- interface RenameOptions {
229
- /** 重命名后回调 */
230
- onRenamed?: (oldPath: string, newPath: string) => void;
231
- }
232
- /**
233
- * 重命名文件/文件夹
234
- */
235
- declare function renameFile(oldPath: string, newPath: string, options?: RenameOptions): Promise<OperationResult>;
236
-
237
- /**
238
- * 复制文件到目标目录
239
- */
240
- declare function copyFiles(sourcePaths: string[], targetDir: string): Promise<OperationResult<{
241
- copiedPaths: string[];
242
- }>>;
243
- /**
244
- * 移动文件到目标目录
245
- */
246
- declare function moveFiles(sourcePaths: string[], targetDir: string): Promise<OperationResult<{
247
- movedPaths: string[];
248
- }>>;
249
-
250
- /**
251
- * 获取文件信息
252
- */
253
- declare function getFileInfo(filePath: string): Promise<OperationResult<FileInfo>>;
254
- /**
255
- * 检查文件/目录是否存在
256
- */
257
- declare function exists(filePath: string): Promise<boolean>;
258
- /**
259
- * 检查是否为目录
260
- */
261
- declare function isDirectory(filePath: string): Promise<boolean>;
262
-
263
- /**
264
- * Shell 操作
265
- *
266
- * 系统级操作:显示简介、在新窗口打开等
267
- */
268
- /**
269
- * 显示文件/文件夹的系统属性窗口
270
- *
271
- * - macOS: 打开 Finder 的"显示简介"窗口
272
- * - Windows: 打开"属性"窗口
273
- * - Linux: 尝试使用文件管理器的属性功能
274
- */
275
- declare function showFileInfo(filePath: string): Promise<{
276
- success: boolean;
277
- error?: string;
278
- }>;
279
- /**
280
- * 在系统文件管理器中显示文件(选中状态)
281
- *
282
- * 注意:这个功能在 Electron 中可以用 shell.showItemInFolder 实现
283
- * 这里提供一个纯 Node.js 的替代实现
284
- */
285
- declare function revealInFileManager(filePath: string): Promise<{
286
- success: boolean;
287
- error?: string;
288
- }>;
289
- /**
290
- * 在终端中打开目录
291
- * macOS: 优先 iTerm2,回退到 Terminal.app
292
- */
293
- declare function openInTerminal(dirPath: string): Promise<{
294
- success: boolean;
295
- error?: string;
296
- }>;
297
- /**
298
- * 通过 Cursor 打开
299
- * 优先 Cursor,回退到 VSCode
300
- */
301
- declare function openInEditor(targetPath: string): Promise<{
302
- success: boolean;
303
- error?: string;
304
- }>;
305
-
306
- /**
307
- * 压缩/解压操作
308
- *
309
- * 支持格式:zip, tar, tar.gz (tgz), tar.bz2
310
- */
311
- /** 压缩格式 */
312
- type CompressFormat = 'zip' | 'tar' | 'tgz' | 'tarbz2';
313
- /** 压缩级别 */
314
- type CompressLevel = 'fast' | 'normal' | 'best';
315
- /** 压缩选项 */
316
- interface CompressOptions {
317
- /** 输出格式 */
318
- format: CompressFormat;
319
- /** 压缩级别 */
320
- level?: CompressLevel;
321
- /** 输出文件名(不含路径) */
322
- outputName: string;
323
- /** 输出目录 */
324
- outputDir: string;
325
- /** 压缩后删除源文件 */
326
- deleteSource?: boolean;
327
- }
328
- /** 解压选项 */
329
- interface ExtractOptions {
330
- /** 目标目录 */
331
- targetDir: string;
332
- /** 解压后删除压缩包 */
333
- deleteArchive?: boolean;
334
- }
335
- /** 进度信息 */
336
- interface CompressProgress {
337
- /** 当前文件 */
338
- currentFile: string;
339
- /** 已处理文件数 */
340
- processedCount: number;
341
- /** 总文件数 */
342
- totalCount: number;
343
- /** 进度百分比 */
344
- percent: number;
345
- }
346
- /** 操作结果 */
347
- interface CompressResult {
348
- success: boolean;
349
- /** 输出路径 */
350
- outputPath?: string;
351
- error?: string;
352
- }
353
- /** 进度回调 */
354
- type ProgressCallback$1 = (progress: CompressProgress) => void;
355
- /**
356
- * 根据文件扩展名检测格式
357
- */
358
- declare function detectArchiveFormat(filePath: string): CompressFormat | null;
359
- /**
360
- * 判断文件是否为支持的压缩文件
361
- */
362
- declare function isArchiveFile(filePath: string): boolean;
363
- /**
364
- * 压缩文件/目录
365
- */
366
- declare function compressFiles(sources: string[], options: CompressOptions, onProgress?: ProgressCallback$1): Promise<CompressResult>;
367
- /**
368
- * 解压文件
369
- */
370
- declare function extractArchive(archivePath: string, options: ExtractOptions, onProgress?: ProgressCallback$1): Promise<CompressResult>;
371
-
372
- /**
373
- * 获取系统路径
374
- */
375
- declare function getSystemPath(pathId: SystemPathId): string | null;
376
- /**
377
- * 获取所有系统路径
378
- */
379
- declare function getAllSystemPaths(): Record<SystemPathId, string | null>;
380
- /**
381
- * 获取用户主目录
382
- */
383
- declare function getHomeDirectory(): string;
384
- /**
385
- * 获取当前平台
386
- */
387
- declare function getPlatform(): NodeJS.Platform;
388
-
389
- /**
390
- * 使用 fdir 快速搜索文件和文件夹
391
- * fdir 可以在 1 秒内扫描 100 万个文件
392
- *
393
- * @param searchPath 搜索路径
394
- * @param pattern 搜索模式(支持 * 通配符,忽略大小写)
395
- * @param maxDepth 最大递归深度
396
- */
397
- declare function searchFiles(searchPath: string, pattern?: string, maxDepth?: number): Promise<string[]>;
398
- /**
399
- * 流式搜索文件(逐层搜索,边搜边返回)
400
- *
401
- * @param searchPath 搜索路径
402
- * @param pattern 搜索模式
403
- * @param onResults 找到结果时的回调
404
- * @param maxResults 最大结果数
405
- */
406
- declare function searchFilesStream(searchPath: string, pattern: string, onResults: (paths: string[], done: boolean) => void, maxResults?: number): Promise<void>;
407
- /**
408
- * 同步搜索(用于小目录)
409
- */
410
- declare function searchFilesSync(searchPath: string, pattern?: string, maxDepth?: number): string[];
411
-
412
- /**
413
- * 计算文件的快速 hash(使用文件大小和修改时间)
414
- * 使用 xxHash64 算法,专为非加密场景设计,性能极快
415
- * 这比计算完整文件 hash 快得多,适合用于缓存判断
416
- *
417
- * @param filePath - 文件路径
418
- * @param stats - 可选的文件状态(如果已获取,避免重复调用 stat)
419
- */
420
- declare function getFileHash(filePath: string, stats?: Stats): Promise<string>;
421
-
422
- /**
423
- * 剪贴板文件操作接口
424
- * 由于 clipboard-files 是 native 模块,由使用者在 Electron 主进程中注入
425
- */
426
- interface ClipboardAdapter {
427
- writeFiles: (paths: string[]) => void;
428
- readFiles: () => string[];
429
- }
430
- /**
431
- * 复制文件到剪贴板
432
- */
433
- declare function copyFilesToClipboard(filePaths: string[], clipboard: ClipboardAdapter): Promise<OperationResult>;
434
- /**
435
- * 从剪贴板读取文件路径
436
- */
437
- declare function getClipboardFiles(clipboard: ClipboardAdapter): OperationResult<{
438
- files: string[];
439
- }>;
440
- /**
441
- * 粘贴文件到目标目录
442
- */
443
- declare function pasteFiles(targetDir: string, sourcePaths: string[]): Promise<OperationResult<{
444
- pastedPaths: string[];
445
- }>>;
446
-
447
- /**
448
- * 应用程序图标获取(macOS)
449
- *
450
- * 纯 Node.js 实现,使用 sips 命令转换 icns 为 PNG
451
- */
452
- /**
453
- * 获取应用程序图标(仅 macOS)
454
- *
455
- * 使用 sips 命令将 .icns 转换为 PNG,返回 base64 data URL
456
- */
457
- declare function getApplicationIcon(appPath: string): Promise<string | null>;
458
-
459
- /**
460
- * 文件系统监听服务
461
- *
462
- * 监听目录变化,当文件增删改时触发回调
463
- */
464
- /** 文件变化类型 */
465
- type WatchEventType = 'add' | 'change' | 'remove' | 'rename';
466
- /** 文件变化事件 */
467
- interface WatchEvent {
468
- type: WatchEventType;
469
- path: string;
470
- filename: string;
471
- }
472
- /** 监听回调 */
473
- type WatchCallback = (event: WatchEvent) => void;
474
- /** 监听器实例 */
475
- interface Watcher {
476
- /** 停止监听 */
477
- close: () => void;
478
- /** 监听的目录路径 */
479
- path: string;
480
- }
481
- /**
482
- * 监听目录变化
483
- *
484
- * @param dirPath 要监听的目录路径
485
- * @param callback 变化回调
486
- * @returns Watcher 实例
487
- */
488
- declare function watchDirectory(dirPath: string, callback: WatchCallback): Watcher;
489
- /**
490
- * 监听管理器
491
- * 用于管理多个监听器,确保同一目录只有一个监听器
492
- */
493
- declare class WatchManager {
494
- private watchers;
495
- private callbacks;
496
- /**
497
- * 开始监听目录
498
- */
499
- watch(dirPath: string, callback: WatchCallback): () => void;
500
- /**
501
- * 停止监听
502
- */
503
- private unwatch;
504
- /**
505
- * 关闭所有监听器
506
- */
507
- closeAll(): void;
508
- }
509
- /**
510
- * 获取全局监听管理器
511
- */
512
- declare function getWatchManager(): WatchManager;
513
-
514
- /**
515
- * 缩略图数据库接口
516
- */
517
- interface ThumbnailDatabase {
518
- /** 获取缓存目录 */
519
- getCacheDir(): string;
520
- /** 快速查询缩略图(只用路径和修改时间) */
521
- getThumbnailPathFast(filePath: string, mtime: number): string | null;
522
- /** 获取缩略图路径(如果缓存有效) */
523
- getThumbnailPath(filePath: string, fileHash: string, mtime: number): string | null;
524
- /** 保存缩略图信息 */
525
- saveThumbnail(filePath: string, fileHash: string, mtime: number, thumbnailPath: string): void;
526
- /** 删除缩略图记录 */
527
- deleteThumbnail(filePath: string): void;
528
- /** 清理旧缩略图 */
529
- cleanupOldThumbnails(): void;
530
- }
531
- /**
532
- * 图片处理器接口
533
- */
534
- interface ImageProcessor {
535
- /** 缩放图片 */
536
- resize(filePath: string, outputPath: string, size: number): Promise<void>;
537
- }
538
- /**
539
- * 视频处理器接口
540
- */
541
- interface VideoProcessor {
542
- /** 截取视频帧 */
543
- screenshot(filePath: string, outputPath: string, timestamp: string, size: string): Promise<void>;
544
- }
545
- /**
546
- * 缩略图服务配置
547
- */
548
- interface ThumbnailServiceOptions {
549
- /** 数据库实例 */
550
- database: ThumbnailDatabase;
551
- /** 图片处理器(可选) */
552
- imageProcessor?: ImageProcessor;
553
- /** 视频处理器(可选) */
554
- videoProcessor?: VideoProcessor;
555
- /** URL 编码器(将本地路径转为 URL) */
556
- urlEncoder?: (filePath: string) => string;
557
- /** 应用图标获取器(macOS) */
558
- getApplicationIcon?: (appPath: string) => Promise<string | null>;
559
- }
560
-
561
- /**
562
- * 缩略图服务
563
- */
564
- declare class ThumbnailService {
565
- private database;
566
- private imageProcessor;
567
- private videoProcessor;
568
- private urlEncoder;
569
- private getApplicationIcon;
570
- constructor(options: ThumbnailServiceOptions);
571
- /**
572
- * 获取缓存的缩略图 URL(不生成新的)
573
- */
574
- getCachedThumbnailUrl(filePath: string): Promise<string | null>;
575
- /**
576
- * 获取缩略图 URL(如果没有缓存则生成)
577
- */
578
- getThumbnailUrl(filePath: string): Promise<string | null>;
579
- /**
580
- * 生成缩略图
581
- */
582
- generateThumbnail(filePath: string, fileHash: string, mtime: number): Promise<string | null>;
583
- /**
584
- * 批量生成缩略图(带并发限制,避免资源耗尽)
585
- */
586
- generateThumbnailsBatch(files: Array<{
587
- path: string;
588
- hash: string;
589
- mtime: number;
590
- }>, concurrency?: number): Promise<void>;
591
- /**
592
- * 删除缩略图
593
- */
594
- deleteThumbnail(filePath: string): void;
595
- /**
596
- * 清理旧缩略图
597
- */
598
- cleanupOldThumbnails(): void;
599
- }
600
- /**
601
- * 初始化缩略图服务
602
- */
603
- declare function initThumbnailService(options: ThumbnailServiceOptions): ThumbnailService;
604
- /**
605
- * 获取缩略图服务实例
606
- */
607
- declare function getThumbnailService(): ThumbnailService | null;
608
-
609
- /**
610
- * SQLite 缩略图数据库实现
611
- *
612
- * 需要安装 better-sqlite3:npm install better-sqlite3
613
- */
614
-
615
- interface SqliteThumbnailDatabaseOptions {
616
- /**
617
- * 缩略图存储目录(绝对路径)。如果提供,则优先使用该目录。
618
- * 适用于将本库嵌入到其他应用时,自定义缩略图缓存位置。
619
- */
620
- thumbnailDir?: string;
621
- /**
622
- * 缩略图子目录名(相对 userDataPath)。默认:`thumbnails`
623
- * 仅在未提供 thumbnailDir 时生效。
624
- */
625
- dirName?: string;
626
- /**
627
- * 缩略图数据库文件名(默认:`thumbnails.db`)
628
- * 仅在未提供 dbPath 时生效。
629
- */
630
- dbFileName?: string;
631
- /**
632
- * 缩略图数据库文件路径(绝对路径)。如果提供,则优先使用该路径。
633
- * 注意:缩略图 jpg 仍然会写入 thumbnailDir(若未显式提供,则使用 dbPath 所在目录)。
634
- */
635
- dbPath?: string;
636
- }
637
- /**
638
- * SQLite 缩略图数据库实现
639
- */
640
- declare class SqliteThumbnailDatabase implements ThumbnailDatabase {
641
- private db;
642
- private cacheDir;
643
- private dbPath;
644
- constructor(userDataPath: string, options?: SqliteThumbnailDatabaseOptions);
645
- /**
646
- * 初始化数据库
647
- */
648
- init(): void;
649
- getCacheDir(): string;
650
- /**
651
- * 快速查询缩略图(用路径和修改时间,不需要哈希)
652
- * 比计算文件哈希快很多
653
- */
654
- getThumbnailPathFast(filePath: string, mtime: number): string | null;
655
- getThumbnailPath(filePath: string, fileHash: string, mtime: number): string | null;
656
- saveThumbnail(filePath: string, fileHash: string, mtime: number, thumbnailPath: string): void;
657
- deleteThumbnail(filePath: string): void;
658
- cleanupOldThumbnails(): void;
659
- close(): void;
660
- }
661
- /**
662
- * 创建 SQLite 缩略图数据库
663
- */
664
- /**
665
- * 创建 SQLite 缩略图数据库(统一入口:仅支持 options 对象)
666
- * 说明:不再支持“传 dbPath 当作参数”的用法;宿主需要显式传入 userDataPath(以及可选的目录/文件覆盖)。
667
- */
668
- declare function createSqliteThumbnailDatabase(options: {
669
- userDataPath: string;
670
- } & SqliteThumbnailDatabaseOptions): SqliteThumbnailDatabase;
671
- /**
672
- * 获取缩略图数据库实例
673
- */
674
- declare function getSqliteThumbnailDatabase(): SqliteThumbnailDatabase | null;
675
- /**
676
- * 关闭缩略图数据库(应在应用退出时调用)
677
- */
678
- declare function closeThumbnailDatabase(): void;
679
-
680
- /**
681
- * 缩略图处理器工厂函数
682
- *
683
- * 需要安装:
684
- * - sharp: npm install sharp
685
- * - ffmpeg-static: npm install ffmpeg-static
686
- */
687
-
688
- /**
689
- * Sharp 类型定义(宽松类型,兼容 sharp 模块)
690
- */
691
- interface SharpInstance {
692
- resize(options?: {
693
- width?: number;
694
- height?: number;
695
- withoutEnlargement?: boolean;
696
- }): SharpInstance;
697
- jpeg(options?: {
698
- quality?: number;
699
- optimiseCoding?: boolean;
700
- }): SharpInstance;
701
- toFile(outputPath: string): Promise<unknown>;
702
- }
703
- type SharpModule = (input: string, options?: any) => SharpInstance;
704
- /**
705
- * 创建 Sharp 图片处理器
706
- *
707
- * @example
708
- * ```ts
709
- * import sharp from 'sharp';
710
- * import { createSharpImageProcessor } from '@huyooo/file-explorer-core';
711
- *
712
- * const imageProcessor = createSharpImageProcessor(sharp);
713
- * ```
714
- */
715
- declare function createSharpImageProcessor(sharp: SharpModule): ImageProcessor;
716
- /**
717
- * 创建 FFmpeg 视频处理器(使用 child_process 直接调用 ffmpeg)
718
- *
719
- * @param ffmpegPath - ffmpeg 可执行文件路径(来自 ffmpeg-static)
720
- *
721
- * @example
722
- * ```ts
723
- * import ffmpegStatic from 'ffmpeg-static';
724
- * import { createFfmpegVideoProcessor } from '@huyooo/file-explorer-core';
725
- *
726
- * const videoProcessor = ffmpegStatic
727
- * ? createFfmpegVideoProcessor(ffmpegStatic)
728
- * : undefined;
729
- * ```
730
- */
731
- declare function createFfmpegVideoProcessor(ffmpegPath: string): VideoProcessor;
732
-
733
- /**
734
- * 媒体类型
735
- */
736
- type MediaType = 'video' | 'audio';
737
- /**
738
- * 转码方式
739
- * - direct: 直接播放,无需转换
740
- * - remux: 转封装,只改变容器格式,不重新编码(快速)
741
- * - transcode: 转码,重新编码(较慢)
742
- */
743
- type TranscodeMethod = 'direct' | 'remux' | 'transcode';
744
- /**
745
- * 转码状态
746
- */
747
- type TranscodeStatus = 'idle' | 'checking' | 'transcoding' | 'ready' | 'error';
748
- /**
749
- * 媒体格式信息
750
- */
751
- interface MediaFormatInfo {
752
- /** 媒体类型 */
753
- type: MediaType;
754
- /** 容器格式 (mp4, mkv, avi...) */
755
- container: string;
756
- /** 视频编码 (h264, h265, vp9...) */
757
- videoCodec?: string;
758
- /** 音频编码 (aac, mp3, flac...) */
759
- audioCodec?: string;
760
- /** 时长(秒) */
761
- duration?: number;
762
- /** 宽度 */
763
- width?: number;
764
- /** 高度 */
765
- height?: number;
766
- /** 比特率 */
767
- bitrate?: number;
768
- }
769
- /**
770
- * 转码检测结果
771
- */
772
- interface TranscodeInfo {
773
- /** 媒体类型 */
774
- type: MediaType;
775
- /** 是否需要转码 */
776
- needsTranscode: boolean;
777
- /** 转码方式 */
778
- method: TranscodeMethod;
779
- /** 预估时间(秒),仅转码时有值 */
780
- estimatedTime?: number;
781
- /** 原始格式信息 */
782
- formatInfo?: MediaFormatInfo;
783
- /** 目标格式 */
784
- targetFormat?: string;
785
- }
786
- /**
787
- * 转码进度
788
- */
789
- interface TranscodeProgress {
790
- /** 进度百分比 0-100 */
791
- percent: number;
792
- /** 当前处理时间(秒) */
793
- time?: number;
794
- /** 总时长(秒) */
795
- duration?: number;
796
- /** 速度(如 "2.5x") */
797
- speed?: string;
798
- }
799
- /**
800
- * 转码结果
801
- */
802
- interface TranscodeResult {
803
- /** 是否成功 */
804
- success: boolean;
805
- /** 可播放的 URL(成功时) */
806
- url?: string;
807
- /** 输出文件路径(成功时) */
808
- outputPath?: string;
809
- /** 错误信息(失败时) */
810
- error?: string;
811
- }
812
- /**
813
- * 媒体元数据
814
- */
815
- interface MediaMetadata {
816
- /** 文件路径 */
817
- filePath: string;
818
- /** 媒体类型 */
819
- type: MediaType;
820
- /** 时长(秒) */
821
- duration: number;
822
- /** 格式信息 */
823
- format: MediaFormatInfo;
824
- /** 标题 */
825
- title?: string;
826
- /** 艺术家 */
827
- artist?: string;
828
- /** 专辑 */
829
- album?: string;
830
- /** 年份 */
831
- year?: string;
832
- }
833
- /**
834
- * 进度回调函数
835
- */
836
- type ProgressCallback = (progress: TranscodeProgress) => void;
837
- /**
838
- * 媒体服务配置
839
- */
840
- interface MediaServiceOptions {
841
- /** ffmpeg 路径 */
842
- ffmpegPath: string;
843
- /** ffprobe 路径(可选,默认与 ffmpeg 同目录) */
844
- ffprobePath?: string;
845
- /** 临时文件目录 */
846
- tempDir?: string;
847
- /** URL 编码器 */
848
- urlEncoder?: (filePath: string) => string;
849
- }
850
-
851
- /**
852
- * 媒体格式检测
853
- *
854
- * 判断媒体文件是否需要转码,以及使用何种转码方式
855
- */
856
-
857
- /**
858
- * 从文件扩展名判断媒体类型
859
- */
860
- declare function getMediaTypeByExtension(filePath: string): MediaType | null;
861
- /**
862
- * 使用 ffprobe 获取媒体格式信息
863
- */
864
- declare function getMediaFormat(filePath: string, ffprobePath: string): Promise<MediaFormatInfo | null>;
865
- /**
866
- * 检测媒体文件的转码需求
867
- */
868
- declare function detectTranscodeNeeds(filePath: string, ffprobePath: string): Promise<TranscodeInfo>;
869
-
870
- /**
871
- * 媒体转码器
872
- *
873
- * 使用 ffmpeg 进行媒体格式转换
874
- */
875
-
876
- /**
877
- * 执行媒体转码
878
- */
879
- declare function transcodeMedia(ffmpegPath: string, inputPath: string, transcodeInfo: TranscodeInfo, tempDir?: string, onProgress?: ProgressCallback): Promise<TranscodeResult>;
880
- /**
881
- * 清理临时转码文件
882
- */
883
- declare function cleanupTranscodedFile(filePath: string): Promise<void>;
884
- /**
885
- * 清理所有临时转码文件
886
- */
887
- declare function cleanupAllTranscodedFiles(tempDir?: string): Promise<void>;
888
-
889
- /**
890
- * 统一媒体服务
891
- *
892
- * 提供媒体格式检测、转码、元数据获取等功能
893
- */
894
-
895
- /**
896
- * 媒体服务类
897
- */
898
- declare class MediaService {
899
- private ffmpegPath;
900
- private ffprobePath;
901
- private tempDir?;
902
- private urlEncoder?;
903
- private transcodeInfoCache;
904
- private transcodedFiles;
905
- constructor(options: MediaServiceOptions);
906
- /**
907
- * 检测文件是否需要转码
908
- */
909
- needsTranscode(filePath: string): Promise<TranscodeInfo>;
910
- /**
911
- * 执行转码并返回可播放的 URL
912
- */
913
- transcode(filePath: string, onProgress?: ProgressCallback): Promise<TranscodeResult>;
914
- /**
915
- * 获取媒体元数据
916
- */
917
- getMetadata(filePath: string): Promise<MediaMetadata | null>;
918
- /**
919
- * 获取可播放的 URL
920
- * 如果文件需要转码,则执行转码;否则直接返回文件 URL
921
- */
922
- getPlayableUrl(filePath: string, onProgress?: ProgressCallback): Promise<string | null>;
923
- /**
924
- * 清理指定文件的转码缓存
925
- */
926
- cleanupFile(filePath: string): Promise<void>;
927
- /**
928
- * 清理所有转码缓存
929
- */
930
- cleanup(): Promise<void>;
931
- /**
932
- * 清除缓存(不删除文件)
933
- */
934
- clearCache(): void;
935
- }
936
- /**
937
- * 初始化媒体服务
938
- */
939
- declare function initMediaService(options: MediaServiceOptions): MediaService;
940
- /**
941
- * 获取媒体服务实例
942
- */
943
- declare function getMediaService(): MediaService | null;
944
- /**
945
- * 创建媒体服务(不设为全局实例)
946
- */
947
- declare function createMediaService(options: MediaServiceOptions): MediaService;
948
-
949
- export { APP_PROTOCOL_HOST, APP_PROTOCOL_PREFIX, APP_PROTOCOL_SCHEME, type ClipboardAdapter, type CompressFormat, type CompressLevel, type CompressOptions, type CompressProgress, type CompressResult, type DeleteOptions, type ExtractOptions, type FileInfo, type FileItem, type FileOperationOptions, FileType, type ImageProcessor, type MediaFormatInfo, type MediaMetadata, type ProgressCallback as MediaProgressCallback, MediaService, type MediaServiceOptions, type MediaType, type OperationResult, type PlatformAdapter, type ProgressCallback$1 as ProgressCallback, type ReadDirectoryOptions, type RenameOptions, SqliteThumbnailDatabase, type SystemPathId, type ThumbnailDatabase, ThumbnailService, type ThumbnailServiceOptions, type TranscodeInfo, type TranscodeMethod, type TranscodeProgress, type TranscodeResult, type TranscodeStatus, type UrlEncoder, type VideoProcessor, type WatchCallback, type WatchEvent, type WatchEventType, WatchManager, type Watcher, cleanupAllTranscodedFiles, cleanupTranscodedFile, closeThumbnailDatabase, compressFiles, copyFiles, copyFilesToClipboard, createFfmpegVideoProcessor, createFile, createFolder, createMediaService, createSharpImageProcessor, createSqliteThumbnailDatabase, decodeFileUrl, deleteFiles, detectArchiveFormat, detectTranscodeNeeds, encodeFileUrl, exists, extractArchive, formatDate, formatDateTime, formatFileSize, getAllSystemPaths, getApplicationIcon, getClipboardFiles, getFileHash, getFileInfo, getFileType, getHomeDirectory, getMediaFormat, getMediaService, getMediaTypeByExtension, getPlatform, getSqliteThumbnailDatabase, getSystemPath, getThumbnailService, getWatchManager, initMediaService, initThumbnailService, isAppProtocolUrl, isArchiveFile, isDirectory, isMediaFile, isPreviewable, moveFiles, openInEditor, openInTerminal, pasteFiles, readDirectory, readFileContent, readImageAsBase64, renameFile, revealInFileManager, searchFiles, searchFilesStream, searchFilesSync, showFileInfo, transcodeMedia, watchDirectory, writeFileContent };