2
0

file.service.ts 15 KB

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