@qhr123/sa2kit 0.1.1 → 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,519 @@
1
+ /**
2
+ * 通用文件服务类型定义
3
+ *
4
+ * 定义了文件存储、上传、下载等核心接口和类型
5
+ */
6
+ /** 文件存储类型 */
7
+ type StorageType = 'local' | 'aliyun-oss' | 'aws-s3' | 'qcloud-cos';
8
+ /** CDN提供者类型 */
9
+ type CDNType = 'none' | 'aliyun-cdn' | 'aws-cloudfront' | 'qcloud-cdn';
10
+ /** 文件处理类型 */
11
+ type ProcessorType = 'image' | 'audio' | 'video' | 'document';
12
+ /** 文件上传状态 */
13
+ type UploadStatus = 'pending' | 'uploading' | 'processing' | 'completed' | 'failed';
14
+ /** 访问权限类型 */
15
+ type AccessPermission = 'public' | 'private' | 'authenticated' | 'owner-only';
16
+ /** 文件元数据基础接口 */
17
+ interface FileMetadata {
18
+ /** 文件ID */
19
+ id: string;
20
+ /** 原始文件名 */
21
+ originalName: string;
22
+ /** 存储文件名 */
23
+ storageName: string;
24
+ /** 文件大小(字节) */
25
+ size: number;
26
+ /** MIME类型 */
27
+ mimeType: string;
28
+ /** 文件扩展名 */
29
+ extension: string;
30
+ /** 文件哈希值 */
31
+ hash?: string;
32
+ /** 上传时间 */
33
+ uploadTime: Date;
34
+ /** 访问权限 */
35
+ permission: AccessPermission;
36
+ /** 上传者ID */
37
+ uploaderId: string;
38
+ /** 模块标识 */
39
+ moduleId: string;
40
+ /** 业务标识 */
41
+ businessId?: string;
42
+ /** 存储提供者 */
43
+ storageProvider: StorageType;
44
+ /** 存储路径 */
45
+ storagePath: string;
46
+ /** CDN URL */
47
+ cdnUrl?: string;
48
+ /** 访问次数 */
49
+ accessCount: number;
50
+ /** 最后访问时间 */
51
+ lastAccessTime?: Date;
52
+ /** 过期时间 */
53
+ expiresAt?: Date;
54
+ /** 自定义元数据 */
55
+ metadata?: Record<string, any>;
56
+ }
57
+ /** 上传文件信息(客户端使用) */
58
+ interface UploadFileInfo {
59
+ /** 文件对象 */
60
+ file: File;
61
+ /** 模块标识 */
62
+ moduleId: string;
63
+ /** 业务标识 */
64
+ businessId?: string;
65
+ /** 访问权限 */
66
+ permission?: AccessPermission;
67
+ /** 自定义存储路径 */
68
+ customPath?: string;
69
+ /** 自定义元数据 */
70
+ metadata?: Record<string, any>;
71
+ /** 是否需要处理 */
72
+ needsProcessing?: boolean;
73
+ /** 处理选项 */
74
+ processingOptions?: ProcessingOptions;
75
+ }
76
+ /** 文件处理选项 */
77
+ interface ProcessingOptions {
78
+ /** 处理器类型 */
79
+ type: ProcessorType;
80
+ /** 处理参数 */
81
+ params?: Record<string, any>;
82
+ }
83
+ /** 上传进度 */
84
+ interface UploadProgress {
85
+ /** 文件ID */
86
+ fileId: string;
87
+ /** 上传状态 */
88
+ status: UploadStatus;
89
+ /** 进度百分比(0-100) */
90
+ progress: number;
91
+ /** 已上传字节数 */
92
+ uploadedBytes: number;
93
+ /** 总字节数 */
94
+ totalBytes: number;
95
+ /** 上传速度(字节/秒) */
96
+ speed: number;
97
+ /** 剩余时间(秒) */
98
+ remainingTime: number;
99
+ /** 错误信息 */
100
+ error?: string;
101
+ }
102
+ /** 上传结果 */
103
+ interface UploadResult {
104
+ /** 是否成功 */
105
+ success: boolean;
106
+ /** 文件元数据 */
107
+ file?: FileMetadata;
108
+ /** 文件访问URL */
109
+ url?: string;
110
+ /** 错误信息 */
111
+ error?: string;
112
+ }
113
+ /** 文件查询选项 */
114
+ interface FileQueryOptions {
115
+ /** 模块标识 */
116
+ moduleId?: string;
117
+ /** 业务标识 */
118
+ businessId?: string;
119
+ /** 上传者ID */
120
+ uploaderId?: string;
121
+ /** MIME类型过滤 */
122
+ mimeType?: string;
123
+ /** 最小文件大小 */
124
+ minSize?: number;
125
+ /** 最大文件大小 */
126
+ maxSize?: number;
127
+ /** 开始时间 */
128
+ startTime?: Date;
129
+ /** 结束时间 */
130
+ endTime?: Date;
131
+ /** 搜索关键词 */
132
+ keyword?: string;
133
+ /** 标签 */
134
+ tags?: string[];
135
+ /** 排序字段 */
136
+ sortBy?: string;
137
+ /** 排序方向 */
138
+ sortOrder?: 'asc' | 'desc';
139
+ /** 页码 */
140
+ page?: number;
141
+ /** 每页数量 */
142
+ pageSize?: number;
143
+ }
144
+ /** 分页结果 */
145
+ interface PaginatedResult<T> {
146
+ /** 数据项 */
147
+ items: T[];
148
+ /** 总数 */
149
+ total: number;
150
+ /** 当前页码 */
151
+ page: number;
152
+ /** 每页数量 */
153
+ pageSize: number;
154
+ /** 总页数 */
155
+ totalPages: number;
156
+ /** 是否有下一页 */
157
+ hasNext: boolean;
158
+ /** 是否有上一页 */
159
+ hasPrev: boolean;
160
+ }
161
+ /** 批量操作结果 */
162
+ interface BatchOperationResult {
163
+ /** 成功数量 */
164
+ successCount: number;
165
+ /** 失败数量 */
166
+ failureCount: number;
167
+ /** 失败详情 */
168
+ failures: Array<{
169
+ fileId: string;
170
+ error: string;
171
+ }>;
172
+ }
173
+ /** 文件事件类型 */
174
+ type FileEventType = 'upload:start' | 'upload:progress' | 'upload:complete' | 'upload:error' | 'download:start' | 'download:complete' | 'download:error' | 'delete:complete' | 'processing:start' | 'processing:complete' | 'processing:error';
175
+ /** 文件事件 */
176
+ interface FileEvent {
177
+ /** 事件类型 */
178
+ type: FileEventType;
179
+ /** 文件ID */
180
+ fileId: string;
181
+ /** 事件时间 */
182
+ timestamp: Date;
183
+ /** 事件数据 */
184
+ data?: Record<string, any>;
185
+ /** 错误信息 */
186
+ error?: string;
187
+ }
188
+ /** 文件事件监听器 */
189
+ type FileEventListener = (event: FileEvent) => void | Promise<void>;
190
+ /** 文件服务基础异常 */
191
+ declare class FileServiceError extends Error {
192
+ readonly code: string;
193
+ readonly details?: Record<string, any> | undefined;
194
+ constructor(message: string, code: string, details?: Record<string, any> | undefined);
195
+ }
196
+ /** 文件上传错误 */
197
+ declare class FileUploadError extends FileServiceError {
198
+ constructor(message: string, details?: Record<string, any>);
199
+ }
200
+ /** 文件处理错误 */
201
+ declare class FileProcessingError extends FileServiceError {
202
+ constructor(message: string, details?: Record<string, any>);
203
+ }
204
+ /** 存储提供者错误 */
205
+ declare class StorageProviderError extends FileServiceError {
206
+ constructor(message: string, details?: Record<string, any>);
207
+ }
208
+ /** CDN提供者错误 */
209
+ declare class CDNProviderError extends FileServiceError {
210
+ constructor(message: string, details?: Record<string, any>);
211
+ }
212
+
213
+ /**
214
+ * 通用文件服务客户端SDK
215
+ *
216
+ * 提供文件上传、下载、查询等功能的客户端接口
217
+ */
218
+
219
+ interface UniversalFileClientConfig {
220
+ /** API基础URL */
221
+ baseUrl?: string;
222
+ /** 请求超时时间(毫秒) */
223
+ timeout?: number;
224
+ /** 上传超时时间(毫秒) */
225
+ uploadTimeout?: number;
226
+ /** 自定义请求头 */
227
+ headers?: Record<string, string>;
228
+ }
229
+ /**
230
+ * 通用文件服务客户端
231
+ */
232
+ declare class UniversalFileClient {
233
+ private config;
234
+ constructor(config?: UniversalFileClientConfig);
235
+ /**
236
+ * 上传文件
237
+ */
238
+ uploadFile(fileInfo: UploadFileInfo, onProgress?: (progress: UploadProgress) => void): Promise<FileMetadata>;
239
+ /**
240
+ * 获取文件访问URL
241
+ */
242
+ getFileUrl(fileId: string, expiresIn?: number): Promise<string>;
243
+ /**
244
+ * 获取文件元数据
245
+ */
246
+ getFileMetadata(fileId: string): Promise<FileMetadata>;
247
+ /**
248
+ * 查询文件列表
249
+ */
250
+ queryFiles(options: FileQueryOptions): Promise<PaginatedResult<FileMetadata>>;
251
+ /**
252
+ * 删除文件
253
+ */
254
+ deleteFile(fileId: string): Promise<void>;
255
+ /**
256
+ * 批量删除文件
257
+ */
258
+ batchDeleteFiles(fileIds: string[]): Promise<BatchOperationResult>;
259
+ /**
260
+ * 获取上传进度
261
+ */
262
+ getUploadProgress(fileId: string): Promise<UploadProgress>;
263
+ /**
264
+ * 获取请求头
265
+ */
266
+ private getHeaders;
267
+ /**
268
+ * 带超时的fetch请求
269
+ */
270
+ private fetchWithTimeout;
271
+ /**
272
+ * 转换API返回的文件元数据
273
+ */
274
+ private transformFileMetadataFromAPI;
275
+ }
276
+ /**
277
+ * 默认客户端实例
278
+ */
279
+ declare const universalFileClient: UniversalFileClient;
280
+ /**
281
+ * 创建自定义客户端实例
282
+ */
283
+ declare function createFileClient(config?: UniversalFileClientConfig): UniversalFileClient;
284
+
285
+ /**
286
+ * 通用文件服务常量定义
287
+ */
288
+ /** 模块版本 */
289
+ declare const UNIVERSAL_FILE_VERSION = "1.0.0";
290
+ /** 模块名称 */
291
+ declare const UNIVERSAL_FILE_NAME = "@qhr123/sa2kit/universalFile";
292
+ /** 默认最大文件大小(字节) - 100MB */
293
+ declare const DEFAULT_MAX_FILE_SIZE: number;
294
+ /** 默认最大图片大小(字节) - 10MB */
295
+ declare const DEFAULT_MAX_IMAGE_SIZE: number;
296
+ /** 默认最大视频大小(字节) - 500MB */
297
+ declare const DEFAULT_MAX_VIDEO_SIZE: number;
298
+ /** 默认最大音频大小(字节) - 50MB */
299
+ declare const DEFAULT_MAX_AUDIO_SIZE: number;
300
+ /** 默认最大文档大小(字节) - 20MB */
301
+ declare const DEFAULT_MAX_DOCUMENT_SIZE: number;
302
+ /** 图片MIME类型 */
303
+ declare const IMAGE_MIME_TYPES: string[];
304
+ /** 视频MIME类型 */
305
+ declare const VIDEO_MIME_TYPES: string[];
306
+ /** 音频MIME类型 */
307
+ declare const AUDIO_MIME_TYPES: string[];
308
+ /** 文档MIME类型 */
309
+ declare const DOCUMENT_MIME_TYPES: string[];
310
+ /** 所有支持的MIME类型 */
311
+ declare const ALL_SUPPORTED_MIME_TYPES: string[];
312
+ /** 图片扩展名 */
313
+ declare const IMAGE_EXTENSIONS: string[];
314
+ /** 视频扩展名 */
315
+ declare const VIDEO_EXTENSIONS: string[];
316
+ /** 音频扩展名 */
317
+ declare const AUDIO_EXTENSIONS: string[];
318
+ /** 文档扩展名 */
319
+ declare const DOCUMENT_EXTENSIONS: string[];
320
+ /** API基础路径 */
321
+ declare const API_BASE_PATH = "/api/universal-file";
322
+ /** API端点 */
323
+ declare const API_ENDPOINTS: {
324
+ /** 上传文件 */
325
+ readonly UPLOAD: "/api/universal-file/upload";
326
+ /** 获取文件URL */
327
+ readonly GET_URL: (fileId: string) => string;
328
+ /** 获取文件元数据 */
329
+ readonly GET_METADATA: (fileId: string) => string;
330
+ /** 删除文件 */
331
+ readonly DELETE: (fileId: string) => string;
332
+ /** 查询文件列表 */
333
+ readonly QUERY: "/api/universal-file/files";
334
+ /** 批量删除 */
335
+ readonly BATCH_DELETE: "/api/universal-file/files/batch-delete";
336
+ /** 获取上传进度 */
337
+ readonly GET_PROGRESS: (fileId: string) => string;
338
+ };
339
+ /** 错误代码 */
340
+ declare const ERROR_CODES: {
341
+ /** 文件上传错误 */
342
+ readonly FILE_UPLOAD_ERROR: "FILE_UPLOAD_ERROR";
343
+ /** 文件大小超限 */
344
+ readonly FILE_SIZE_EXCEEDED: "FILE_SIZE_EXCEEDED";
345
+ /** 文件类型不支持 */
346
+ readonly FILE_TYPE_NOT_SUPPORTED: "FILE_TYPE_NOT_SUPPORTED";
347
+ /** 文件不存在 */
348
+ readonly FILE_NOT_FOUND: "FILE_NOT_FOUND";
349
+ /** 文件处理错误 */
350
+ readonly FILE_PROCESSING_ERROR: "FILE_PROCESSING_ERROR";
351
+ /** 存储提供者错误 */
352
+ readonly STORAGE_PROVIDER_ERROR: "STORAGE_PROVIDER_ERROR";
353
+ /** CDN提供者错误 */
354
+ readonly CDN_PROVIDER_ERROR: "CDN_PROVIDER_ERROR";
355
+ /** 网络错误 */
356
+ readonly NETWORK_ERROR: "NETWORK_ERROR";
357
+ /** 超时错误 */
358
+ readonly TIMEOUT_ERROR: "TIMEOUT_ERROR";
359
+ /** 未授权 */
360
+ readonly UNAUTHORIZED: "UNAUTHORIZED";
361
+ /** 权限不足 */
362
+ readonly FORBIDDEN: "FORBIDDEN";
363
+ /** 服务器错误 */
364
+ readonly SERVER_ERROR: "SERVER_ERROR";
365
+ };
366
+ /** 默认分页大小 */
367
+ declare const DEFAULT_PAGE_SIZE = 20;
368
+ /** 默认请求超时时间(毫秒) - 30秒 */
369
+ declare const DEFAULT_REQUEST_TIMEOUT = 30000;
370
+ /** 默认上传超时时间(毫秒) - 5分钟 */
371
+ declare const DEFAULT_UPLOAD_TIMEOUT = 300000;
372
+ /** 默认分片上传大小(字节) - 5MB */
373
+ declare const DEFAULT_CHUNK_SIZE: number;
374
+
375
+ /**
376
+ * 通用文件服务工具函数
377
+ */
378
+ /**
379
+ * 格式化文件大小
380
+ */
381
+ declare function formatFileSize(bytes: number): string;
382
+ /**
383
+ * 解析文件大小字符串
384
+ */
385
+ declare function parseFileSize(sizeStr: string): number;
386
+ /**
387
+ * 获取文件扩展名
388
+ */
389
+ declare function getFileExtension(fileName: string): string;
390
+ /**
391
+ * 根据文件名获取MIME类型
392
+ */
393
+ declare function getMimeTypeFromFileName(fileName: string): string;
394
+ /**
395
+ * 检查MIME类型是否支持
396
+ */
397
+ declare function isMimeTypeSupported(mimeType: string, allowedTypes?: string[]): boolean;
398
+ /**
399
+ * 判断是否为图片类型
400
+ */
401
+ declare function isImageFile(mimeType: string): boolean;
402
+ /**
403
+ * 判断是否为视频类型
404
+ */
405
+ declare function isVideoFile(mimeType: string): boolean;
406
+ /**
407
+ * 判断是否为音频类型
408
+ */
409
+ declare function isAudioFile(mimeType: string): boolean;
410
+ /**
411
+ * 判断是否为文档类型
412
+ */
413
+ declare function isDocumentFile(mimeType: string): boolean;
414
+ /**
415
+ * 获取文件类型类别
416
+ */
417
+ declare function getFileCategory(mimeType: string): 'image' | 'video' | 'audio' | 'document' | 'other';
418
+ /**
419
+ * 验证文件名是否合法
420
+ */
421
+ declare function validateFileName(fileName: string): boolean;
422
+ /**
423
+ * 清理文件名,移除非法字符
424
+ */
425
+ declare function sanitizeFileName(fileName: string): string;
426
+ /**
427
+ * 生成唯一文件名
428
+ */
429
+ declare function generateUniqueFileName(originalName: string, fileId: string): string;
430
+ /**
431
+ * 生成存储路径
432
+ */
433
+ declare function generateStoragePath(moduleId: string, fileName: string, options?: {
434
+ businessId?: string;
435
+ useDate?: boolean;
436
+ customPrefix?: string;
437
+ }): string;
438
+ /**
439
+ * 解析存储路径
440
+ */
441
+ declare function parseStoragePath(path: string): {
442
+ moduleId?: string;
443
+ businessId?: string;
444
+ year?: string;
445
+ month?: string;
446
+ day?: string;
447
+ fileName: string;
448
+ };
449
+ /**
450
+ * 验证文件大小
451
+ */
452
+ declare function validateFileSize(file: File, maxSize: number): {
453
+ valid: boolean;
454
+ error?: string;
455
+ };
456
+ /**
457
+ * 验证文件类型
458
+ */
459
+ declare function validateFileType(file: File, allowedTypes?: string[]): {
460
+ valid: boolean;
461
+ error?: string;
462
+ };
463
+ /**
464
+ * 验证文件
465
+ */
466
+ declare function validateFile(file: File, options?: {
467
+ maxSize?: number;
468
+ allowedTypes?: string[];
469
+ }): {
470
+ valid: boolean;
471
+ errors: string[];
472
+ };
473
+ /**
474
+ * 构建查询字符串
475
+ */
476
+ declare function buildQueryString(params: Record<string, any>): string;
477
+ /**
478
+ * 解析URL查询参数
479
+ */
480
+ declare function parseQueryString(url: string): Record<string, string>;
481
+ /**
482
+ * 计算上传进度
483
+ */
484
+ declare function calculateProgress(uploadedBytes: number, totalBytes: number): number;
485
+ /**
486
+ * 计算上传速度
487
+ */
488
+ declare function calculateSpeed(uploadedBytes: number, elapsedTime: number): number;
489
+ /**
490
+ * 计算剩余时间
491
+ */
492
+ declare function calculateRemainingTime(uploadedBytes: number, totalBytes: number, speed: number): number;
493
+ /**
494
+ * 创建文件错误对象
495
+ */
496
+ declare function createFileError(code: string, message: string, details?: Record<string, any>): {
497
+ code: string;
498
+ message: string;
499
+ details?: Record<string, any>;
500
+ timestamp: Date;
501
+ };
502
+ /**
503
+ * 格式化错误消息
504
+ */
505
+ declare function formatErrorMessage(error: unknown): string;
506
+ /**
507
+ * 读取文件为Base64
508
+ */
509
+ declare function readFileAsBase64(file: File): Promise<string>;
510
+ /**
511
+ * 读取文件为ArrayBuffer
512
+ */
513
+ declare function readFileAsArrayBuffer(file: File): Promise<ArrayBuffer>;
514
+ /**
515
+ * 读取文件为文本
516
+ */
517
+ declare function readFileAsText(file: File, encoding?: string): Promise<string>;
518
+
519
+ export { ALL_SUPPORTED_MIME_TYPES, API_BASE_PATH, API_ENDPOINTS, AUDIO_EXTENSIONS, AUDIO_MIME_TYPES, type AccessPermission, type BatchOperationResult, CDNProviderError, type CDNType, DEFAULT_CHUNK_SIZE, DEFAULT_MAX_AUDIO_SIZE, DEFAULT_MAX_DOCUMENT_SIZE, DEFAULT_MAX_FILE_SIZE, DEFAULT_MAX_IMAGE_SIZE, DEFAULT_MAX_VIDEO_SIZE, DEFAULT_PAGE_SIZE, DEFAULT_REQUEST_TIMEOUT, DEFAULT_UPLOAD_TIMEOUT, DOCUMENT_EXTENSIONS, DOCUMENT_MIME_TYPES, ERROR_CODES, type FileEvent, type FileEventListener, type FileEventType, type FileMetadata, FileProcessingError, type FileQueryOptions, FileServiceError, FileUploadError, IMAGE_EXTENSIONS, IMAGE_MIME_TYPES, type PaginatedResult, type ProcessingOptions, type ProcessorType, StorageProviderError, type StorageType, UNIVERSAL_FILE_NAME, UNIVERSAL_FILE_VERSION, UniversalFileClient, type UniversalFileClientConfig, type UploadFileInfo, type UploadProgress, type UploadResult, type UploadStatus, VIDEO_EXTENSIONS, VIDEO_MIME_TYPES, buildQueryString, calculateProgress, calculateRemainingTime, calculateSpeed, createFileClient, createFileError, formatErrorMessage, formatFileSize, generateStoragePath, generateUniqueFileName, getFileCategory, getFileExtension, getMimeTypeFromFileName, isAudioFile, isDocumentFile, isImageFile, isMimeTypeSupported, isVideoFile, parseFileSize, parseQueryString, parseStoragePath, readFileAsArrayBuffer, readFileAsBase64, readFileAsText, sanitizeFileName, universalFileClient, validateFile, validateFileName, validateFileSize, validateFileType };