@@ -13,11 +13,14 @@ const { imageMimeTypes, tinifySupportedMimeTypes } = require('../constants/file'
1313tinify . key = process . env . TINIFY_KEY ;
1414
1515const router = new Router ( ) ;
16- const uploadDirectory = path . join ( __dirname , '..' , 'public' , 'files' ) ;
16+ const uploadDirectory = path . join ( __dirname , '..' , 'resource' ) ; // 修改后的上传目录
1717const iconsDirectory = path . join ( __dirname , '..' , 'public' , 'icons' ) ;
1818
19- // 获取缩略图路径
20- const getFileThumbPath = ( fileId ) => path . join ( uploadDirectory , `${ fileId } _thumb` ) ;
19+ // 获取实际文件路径
20+ const getRealFilePath = ( fileId , ext ) => path . join ( uploadDirectory , `${ fileId } ${ ext } ` ) ;
21+
22+ // 获取实际缩略图路径
23+ const getRealThumbPath = ( fileId ) => path . join ( uploadDirectory , `${ fileId } _thumb.png` ) ;
2124
2225// 获取默认缩略图路径
2326const getDefaultThumbPath = ( mime ) => {
@@ -50,52 +53,52 @@ router.post('/upload', async (ctx) => {
5053 for ( const file of fileList ) {
5154 const fileId = uuidv4 ( ) ;
5255 const ext = path . extname ( file . filepath ) ;
53- const outputFilePath = path . join ( uploadDirectory , ` ${ fileId } ${ ext } ` ) ;
56+ const realFilePath = getRealFilePath ( fileId , ext ) ;
5457
5558 const { mime, ext : fileExt } = await detectFileType ( file . filepath , file ) ;
56- let outputFileThumbPath = null ;
59+ let realThumbPath = null ;
5760
5861 if ( shouldGenerateThumb && imageMimeTypes . includes ( mime ) ) {
59- outputFileThumbPath = getFileThumbPath ( fileId ) ;
62+ realThumbPath = getRealThumbPath ( fileId ) ;
6063 await sharp ( file . filepath )
6164 . resize ( 200 , 200 )
62- . toFile ( outputFileThumbPath ) ;
65+ . toFile ( realThumbPath ) ;
6366 } else if ( shouldGenerateThumb ) {
64- outputFileThumbPath = getDefaultThumbPath ( mime ) ;
67+ realThumbPath = getDefaultThumbPath ( mime ) ;
6568 }
6669
6770 if ( shouldCompress && tinifySupportedMimeTypes . includes ( mime ) ) {
68- await tinify . fromFile ( file . filepath ) . toFile ( outputFilePath ) ;
71+ await tinify . fromFile ( file . filepath ) . toFile ( realFilePath ) ;
6972 } else {
7073 if ( shouldKeepTemp ) {
71- await fsp . copyFile ( file . filepath , outputFilePath ) ;
74+ await fsp . copyFile ( file . filepath , realFilePath ) ;
7275 } else {
73- await fsp . rename ( file . filepath , outputFilePath ) ;
76+ await fsp . rename ( file . filepath , realFilePath ) ;
7477 }
7578 }
7679
77- const fileUrl = `${ process . env . PUBLIC_NETWORK_DOMAIN } /files/${ fileId } ${ ext } ` ;
78- const thumbUrl = shouldGenerateThumb ? `${ process . env . PUBLIC_NETWORK_DOMAIN } /files/ ${ fileId } ?type=thumb` : null ;
80+ const fileUrl = `${ process . env . PUBLIC_NETWORK_DOMAIN } /files/${ fileId } ` ;
81+ const thumbUrl = shouldGenerateThumb ? `${ fileUrl } ?type=thumb` : null ;
7982
8083 await File . create ( {
8184 id : fileId ,
82- filename : path . basename ( outputFilePath ) ,
83- filesize : ( await fsp . stat ( outputFilePath ) ) . size ,
85+ filename : path . basename ( realFilePath ) ,
86+ filesize : ( await fsp . stat ( realFilePath ) ) . size ,
8487 filelocation : fileUrl ,
85- real_file_location : outputFilePath ,
88+ real_file_location : realFilePath ,
8689 created_by : ctx . query . createdBy || 'anonymous' ,
8790 is_public : isFilePublic ,
8891 thumb_location : thumbUrl ,
8992 is_thumb : shouldGenerateThumb ,
9093 is_delete : false ,
91- real_file_thumb_location : outputFileThumbPath ,
94+ real_file_thumb_location : realThumbPath ,
9295 mime,
9396 ext : fileExt
9497 } ) ;
9598
9699 const response = { filepath : fileUrl } ;
97100 if ( responseType === 'md' && imageMimeTypes . includes ( mime ) ) {
98- response . filepath = `` ;
101+ response . filepath = `` ;
99102 }
100103 responses . push ( response ) ;
101104
@@ -177,7 +180,7 @@ router.get('/files', async (ctx) => {
177180 }
178181} ) ;
179182
180- // 获取单个文件
183+ // 获取单个文件信息
181184router . get ( '/files/:id' , async ( ctx ) => {
182185 const { id } = ctx . params ;
183186 const { type } = ctx . query ; // 获取查询参数 'type',可以是 'thumb' 或 'original'
0 commit comments