file.service.mt.ts 15 KB

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