file.service.ts 14 KB

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