file.service.ts 14 KB

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