file.service.mt.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. import { GenericCrudService } from '@d8d/shared-crud';
  2. import { DataSource } from 'typeorm';
  3. import { FileMt } from '../entities/file.entity';
  4. import { MinioService } from './minio.service';
  5. import { v4 as uuidv4 } from 'uuid';
  6. import { logger } from '@d8d/shared-utils';
  7. export class FileServiceMt extends GenericCrudService<FileMt> {
  8. private readonly minioService: MinioService;
  9. constructor(dataSource: DataSource) {
  10. super(dataSource, FileMt);
  11. this.minioService = new MinioService();
  12. }
  13. /**
  14. * 创建文件记录并生成预签名上传URL
  15. */
  16. async createFile(data: Partial<FileMt>, tenantId?: number) {
  17. try {
  18. // 生成唯一文件存储路径(包含租户ID)
  19. const tenantPrefix = tenantId ? `tenants/${tenantId}/` : '';
  20. const fileKey = `${tenantPrefix}${data.uploadUserId}/${uuidv4()}-${data.name}`;
  21. // 生成MinIO上传策略
  22. const uploadPolicy = await this.minioService.generateUploadPolicy(fileKey);
  23. // 准备文件记录数据
  24. const fileData = {
  25. ...data,
  26. path: fileKey,
  27. tenantId,
  28. uploadTime: new Date(),
  29. createdAt: new Date(),
  30. updatedAt: new Date()
  31. };
  32. // 保存文件记录到数据库
  33. const savedFile = await this.create(fileData as FileMt);
  34. // 重新加载文件记录,包含关联的uploadUser数据
  35. const fileWithUser = await this.repository.findOne({
  36. where: { id: savedFile.id },
  37. relations: ['uploadUser']
  38. });
  39. // 返回文件记录和上传策略
  40. return {
  41. file: fileWithUser || savedFile,
  42. uploadPolicy
  43. };
  44. } catch (error) {
  45. logger.error('Failed to create file:', error);
  46. throw new Error('文件创建失败');
  47. }
  48. }
  49. /**
  50. * 删除文件记录及对应的MinIO文件
  51. */
  52. async deleteFile(id: number, options?: { tenantId?: number }) {
  53. // 获取文件记录,考虑租户隔离
  54. const where: any = { id };
  55. if (options?.tenantId !== undefined) {
  56. where.tenantId = options.tenantId;
  57. }
  58. const file = await this.repository.findOne({ where });
  59. if (!file) {
  60. throw new Error('文件不存在');
  61. }
  62. try {
  63. // 验证文件是否存在于MinIO
  64. const fileExists = await this.minioService.objectExists(this.minioService.bucketName, file.path);
  65. if (!fileExists) {
  66. logger.error(`File not found in MinIO: ${this.minioService.bucketName}/${file.path}`);
  67. // 仍然继续删除数据库记录,但记录警告日志
  68. } else {
  69. // 从MinIO删除文件
  70. await this.minioService.deleteObject(this.minioService.bucketName, file.path);
  71. }
  72. // 从数据库删除记录
  73. await this.delete(id);
  74. return true;
  75. } catch (error) {
  76. logger.error('Failed to delete file:', error);
  77. throw new Error('文件删除失败');
  78. }
  79. }
  80. /**
  81. * 获取文件访问URL
  82. */
  83. async getFileUrl(id: number) {
  84. const file = await this.getById(id);
  85. if (!file) {
  86. throw new Error('文件不存在');
  87. }
  88. return this.minioService.getPresignedFileUrl(this.minioService.bucketName, file.path);
  89. }
  90. /**
  91. * 获取文件下载URL(带Content-Disposition头)
  92. */
  93. async getFileDownloadUrl(id: number) {
  94. const file = await this.getById(id);
  95. if (!file) {
  96. throw new Error('文件不存在');
  97. }
  98. const url = await this.minioService.getPresignedFileDownloadUrl(
  99. this.minioService.bucketName,
  100. file.path,
  101. file.name
  102. );
  103. return {
  104. url,
  105. filename: file.name
  106. };
  107. }
  108. /**
  109. * 创建多部分上传策略
  110. */
  111. async createMultipartUploadPolicy(data: Partial<FileMt>, partCount: number, tenantId?: number) {
  112. try {
  113. // 生成唯一文件存储路径(包含租户ID)
  114. const tenantPrefix = tenantId ? `tenants/${tenantId}/` : '';
  115. const fileKey = `${tenantPrefix}${data.uploadUserId}/${uuidv4()}-${data.name}`;
  116. // 初始化多部分上传
  117. const uploadId = await this.minioService.createMultipartUpload(
  118. this.minioService.bucketName,
  119. fileKey
  120. );
  121. // 生成各部分上传URL
  122. const uploadUrls = await this.minioService.generateMultipartUploadUrls(
  123. this.minioService.bucketName,
  124. fileKey,
  125. uploadId,
  126. partCount
  127. );
  128. // 准备文件记录数据
  129. const fileData = {
  130. ...data,
  131. path: fileKey,
  132. tenantId,
  133. uploadTime: new Date(),
  134. createdAt: new Date(),
  135. updatedAt: new Date()
  136. };
  137. // 保存文件记录到数据库
  138. const savedFile = await this.create(fileData as FileMt);
  139. // 返回文件记录和上传策略
  140. return {
  141. file: savedFile,
  142. uploadId,
  143. uploadUrls,
  144. bucket: this.minioService.bucketName,
  145. key: fileKey
  146. };
  147. } catch (error) {
  148. logger.error('Failed to create multipart upload policy:', error);
  149. throw new Error('创建多部分上传策略失败');
  150. }
  151. }
  152. /**
  153. * 完成分片上传
  154. */
  155. async completeMultipartUpload(data: {
  156. uploadId: string;
  157. bucket: string;
  158. key: string;
  159. parts: Array<{ partNumber: number; etag: string }>;
  160. }) {
  161. logger.db('Starting multipart upload completion:', {
  162. uploadId: data.uploadId,
  163. bucket: data.bucket,
  164. key: data.key,
  165. partsCount: data.parts.length
  166. });
  167. // 查找文件记录
  168. const file = await this.repository.findOneBy({ path: data.key });
  169. if (!file) {
  170. throw new Error('文件记录不存在');
  171. }
  172. try {
  173. // 完成MinIO分片上传 - 注意格式转换
  174. const result = await this.minioService.completeMultipartUpload(
  175. data.bucket,
  176. data.key,
  177. data.uploadId,
  178. data.parts.map(part => ({ PartNumber: part.partNumber, ETag: part.etag }))
  179. );
  180. // 更新文件大小等信息
  181. file.size = result.size;
  182. file.updatedAt = new Date();
  183. await this.repository.save(file);
  184. // 生成文件访问URL
  185. const url = this.minioService.getFileUrl(data.bucket, data.key);
  186. logger.db('Multipart upload completed successfully:', {
  187. fileId: file.id,
  188. size: result.size,
  189. key: data.key
  190. });
  191. return {
  192. fileId: file.id,
  193. url,
  194. key: data.key,
  195. size: result.size
  196. };
  197. } catch (error) {
  198. logger.error('Failed to complete multipart upload:', error);
  199. throw new Error('完成分片上传失败');
  200. }
  201. }
  202. /**
  203. * 保存文件记录并将文件内容直接上传到MinIO
  204. * @param fileData - 文件基础信息
  205. * @param fileContent - 文件内容(Buffer)
  206. * @param contentType - 文件MIME类型
  207. * @param tenantId - 租户ID
  208. * @returns 保存的文件记录和文件访问URL
  209. */
  210. async saveFile(
  211. fileData: {
  212. name: string;
  213. size: number;
  214. mimeType: string;
  215. uploadUserId: number;
  216. [key: string]: any;
  217. },
  218. fileContent: Buffer,
  219. contentType?: string,
  220. tenantId?: number
  221. ) {
  222. try {
  223. logger.db('Starting saveFile process:', {
  224. filename: fileData.name,
  225. size: fileData.size,
  226. mimeType: fileData.mimeType,
  227. uploadUserId: fileData.uploadUserId,
  228. tenantId
  229. });
  230. // 生成唯一文件存储路径(包含租户ID)
  231. const tenantPrefix = tenantId ? `tenants/${tenantId}/` : '';
  232. const fileKey = `${tenantPrefix}${fileData.uploadUserId}/${uuidv4()}-${fileData.name}`;
  233. // 确保存储桶存在
  234. await this.minioService.ensureBucketExists();
  235. // 直接上传文件内容到MinIO
  236. const fileUrl = await this.minioService.createObject(
  237. this.minioService.bucketName,
  238. fileKey,
  239. fileContent,
  240. contentType || fileData.mimeType
  241. );
  242. // 准备文件记录数据
  243. const completeFileData = {
  244. ...fileData,
  245. path: fileKey,
  246. tenantId,
  247. uploadTime: new Date(),
  248. };
  249. // 保存文件记录到数据库
  250. const savedFile = await this.create(completeFileData as any);
  251. logger.db('File saved successfully:', {
  252. fileId: savedFile.id,
  253. filename: savedFile.name,
  254. size: savedFile.size,
  255. url: fileUrl,
  256. tenantId
  257. });
  258. return {
  259. file: savedFile,
  260. url: fileUrl
  261. };
  262. } catch (error) {
  263. logger.error('Failed to save file:', error);
  264. throw new Error(`文件保存失败: ${error instanceof Error ? error.message : '未知错误'}`);
  265. }
  266. }
  267. /**
  268. * 保存文件记录并将文件内容直接上传到MinIO(支持自定义存储路径)
  269. * @param fileData - 文件基础信息
  270. * @param fileContent - 文件内容(Buffer)
  271. * @param customPath - 自定义存储路径(可选)
  272. * @param contentType - 文件MIME类型
  273. * @param tenantId - 租户ID
  274. * @returns 保存的文件记录和文件访问URL
  275. */
  276. async saveFileWithCustomPath(
  277. fileData: {
  278. name: string;
  279. size: number;
  280. mimeType: string;
  281. uploadUserId: number;
  282. [key: string]: any;
  283. },
  284. fileContent: Buffer,
  285. customPath?: string,
  286. contentType?: string,
  287. tenantId?: number
  288. ) {
  289. try {
  290. logger.db('Starting saveFileWithCustomPath process:', {
  291. filename: fileData.name,
  292. size: fileData.size,
  293. mimeType: fileData.mimeType,
  294. uploadUserId: fileData.uploadUserId,
  295. customPath: customPath || 'auto-generated',
  296. tenantId
  297. });
  298. // 使用自定义路径或生成唯一文件存储路径(包含租户ID)
  299. const tenantPrefix = tenantId ? `tenants/${tenantId}/` : '';
  300. const fileKey = customPath || `${tenantPrefix}${fileData.uploadUserId}/${uuidv4()}-${fileData.name}`;
  301. // 确保存储桶存在
  302. await this.minioService.ensureBucketExists();
  303. // 直接上传文件内容到MinIO
  304. const fileUrl = await this.minioService.createObject(
  305. this.minioService.bucketName,
  306. fileKey,
  307. fileContent,
  308. contentType || fileData.mimeType
  309. );
  310. // 准备文件记录数据
  311. const completeFileData = {
  312. ...fileData,
  313. path: fileKey,
  314. tenantId,
  315. uploadTime: new Date(),
  316. // createdAt: new Date(),
  317. // updatedAt: new Date()
  318. };
  319. // 保存文件记录到数据库
  320. const savedFile = await this.create(completeFileData as any);
  321. logger.db('File saved with custom path successfully:', {
  322. fileId: savedFile.id,
  323. filename: savedFile.name,
  324. size: savedFile.size,
  325. path: fileKey,
  326. url: fileUrl,
  327. tenantId
  328. });
  329. return {
  330. file: savedFile,
  331. url: fileUrl
  332. };
  333. } catch (error) {
  334. logger.error('Failed to save file with custom path:', error);
  335. throw new Error(`文件保存失败: ${error instanceof Error ? error.message : '未知错误'}`);
  336. }
  337. }
  338. /**
  339. * 从URL下载文件并保存到MinIO
  340. * @param url - 文件URL
  341. * @param fileData - 文件基础信息(不含name和size,将自动获取)
  342. * @param options - 可选配置
  343. * @param tenantId - 租户ID
  344. * @returns 保存的文件记录和文件访问URL
  345. */
  346. async downloadAndSaveFromUrl(
  347. url: string,
  348. fileData: {
  349. uploadUserId: number;
  350. mimeType?: string;
  351. customFileName?: string;
  352. customPath?: string;
  353. [key: string]: any;
  354. },
  355. options?: {
  356. timeout?: number;
  357. retries?: number;
  358. },
  359. tenantId?: number
  360. ) {
  361. try {
  362. const axios = require('axios');
  363. logger.db('Starting downloadAndSaveFromUrl process:', {
  364. url,
  365. uploadUserId: fileData.uploadUserId,
  366. customFileName: fileData.customFileName,
  367. customPath: fileData.customPath,
  368. tenantId
  369. });
  370. // 下载文件
  371. const response = await axios.get(url, {
  372. responseType: 'arraybuffer',
  373. timeout: options?.timeout || 30000,
  374. maxRedirects: 5,
  375. headers: {
  376. 'User-Agent': 'Mozilla/5.0 (compatible; FileDownloader/1.0)'
  377. }
  378. });
  379. const buffer = Buffer.from(response.data);
  380. // 从URL或响应头中获取文件名
  381. let fileName = fileData.customFileName;
  382. if (!fileName) {
  383. // 尝试从Content-Disposition头获取文件名
  384. const contentDisposition = response.headers['content-disposition'];
  385. if (contentDisposition) {
  386. const filenameMatch = contentDisposition.match(/filename[*]?=(?:utf-8'')?(.+)/i);
  387. if (filenameMatch) {
  388. fileName = decodeURIComponent(filenameMatch[1].replace(/['"]/g, ''));
  389. }
  390. }
  391. // 从URL路径获取文件名
  392. if (!fileName) {
  393. const urlPath = new URL(url).pathname;
  394. fileName = urlPath.split('/').pop() || `file_${Date.now()}`;
  395. }
  396. }
  397. // 确保文件有扩展名
  398. if (!fileName.includes('.') && fileData.mimeType) {
  399. const ext = this.getExtensionFromMimeType(fileData.mimeType);
  400. if (ext) {
  401. fileName += `.${ext}`;
  402. }
  403. }
  404. // 确定MIME类型
  405. let mimeType = fileData.mimeType || response.headers['content-type'];
  406. if (!mimeType || mimeType === 'application/octet-stream') {
  407. mimeType = this.inferMimeType(fileName);
  408. }
  409. // 保存文件
  410. const saveResult = await this.saveFileWithCustomPath(
  411. {
  412. ...fileData,
  413. name: fileName,
  414. size: buffer.length,
  415. mimeType,
  416. fileType: this.getFileTypeFromMimeType(mimeType)
  417. },
  418. buffer,
  419. fileData.customPath,
  420. mimeType,
  421. tenantId
  422. );
  423. logger.db('Download and save completed successfully:', {
  424. fileId: saveResult.file.id,
  425. fileName,
  426. size: buffer.length,
  427. url: saveResult.url
  428. });
  429. return saveResult;
  430. } catch (error) {
  431. logger.error('Failed to download and save file from URL:', {
  432. url,
  433. error: error instanceof Error ? error.message : '未知错误',
  434. stack: error instanceof Error ? error.stack : undefined
  435. });
  436. throw new Error(`从URL下载文件失败: ${error instanceof Error ? error.message : '未知错误'}`);
  437. }
  438. }
  439. /**
  440. * 根据MIME类型获取文件扩展名
  441. */
  442. private getExtensionFromMimeType(mimeType: string): string | null {
  443. const mimeMap: Record<string, string> = {
  444. 'image/jpeg': 'jpg',
  445. 'image/png': 'png',
  446. 'image/gif': 'gif',
  447. 'image/webp': 'webp',
  448. 'image/svg+xml': 'svg',
  449. 'application/pdf': 'pdf',
  450. 'text/plain': 'txt',
  451. 'application/json': 'json',
  452. 'application/xml': 'xml',
  453. 'video/mp4': 'mp4',
  454. 'audio/mp3': 'mp3'
  455. };
  456. return mimeMap[mimeType] || null;
  457. }
  458. /**
  459. * 根据文件名推断MIME类型
  460. */
  461. private inferMimeType(fileName: string): string {
  462. const ext = fileName.toLowerCase().split('.').pop();
  463. const extMap: Record<string, string> = {
  464. 'jpg': 'image/jpeg',
  465. 'jpeg': 'image/jpeg',
  466. 'png': 'image/png',
  467. 'gif': 'image/gif',
  468. 'webp': 'image/webp',
  469. 'svg': 'image/svg+xml',
  470. 'pdf': 'application/pdf',
  471. 'txt': 'text/plain',
  472. 'json': 'application/json',
  473. 'xml': 'application/xml',
  474. 'mp4': 'video/mp4',
  475. 'mp3': 'audio/mp3',
  476. 'wav': 'audio/wav'
  477. };
  478. return extMap[ext || ''] || 'application/octet-stream';
  479. }
  480. /**
  481. * 根据MIME类型获取文件类型
  482. */
  483. private getFileTypeFromMimeType(mimeType: string): string {
  484. if (mimeType.startsWith('image/')) return 'image';
  485. if (mimeType.startsWith('video/')) return 'video';
  486. if (mimeType.startsWith('audio/')) return 'audio';
  487. if (mimeType === 'application/pdf') return 'document';
  488. if (mimeType.startsWith('text/')) return 'document';
  489. return 'other';
  490. }
  491. }