feat: 上传文件到minio
This commit is contained in:
		| @@ -1,8 +1,25 @@ | ||||
| import { app } from '@/app.ts'; | ||||
| import { getMinioList } from './module/get-minio-list.ts'; | ||||
| import { getFileStat, getMinioList } from './module/get-minio-list.ts'; | ||||
| import path from 'path'; | ||||
| import { CustomError } from '@abearxiong/router'; | ||||
| import { get } from 'http'; | ||||
|  | ||||
| const handlePrefix = (prefix: string) => { | ||||
|   // 清理所有的 '..' | ||||
|   if (!prefix) return ''; | ||||
|   if (prefix.includes('..')) { | ||||
|     throw new CustomError('invalid prefix'); | ||||
|   } | ||||
|   return prefix; | ||||
| }; | ||||
| const getPrefixByUser = (data: { prefix: string }, tokenUser: { username: string }) => { | ||||
|   const prefixBase = '/' + tokenUser.username; | ||||
|   const _prefix = handlePrefix(data.prefix); | ||||
|   return { | ||||
|     len: prefixBase.length, | ||||
|     prefix: path.join(prefixBase, './', _prefix), | ||||
|   }; | ||||
| }; | ||||
| app | ||||
|   .route({ | ||||
|     path: 'file', | ||||
| @@ -12,19 +29,37 @@ app | ||||
|   .define(async (ctx) => { | ||||
|     const tokenUser = ctx.state.tokenUser; | ||||
|     const data = ctx.query.data || {}; | ||||
|     const prefixBase = '/' + tokenUser.username; | ||||
|     const handlePrefix = (prefix: string) => { | ||||
|       // 清理所有的 '..' | ||||
|       if (prefix.includes('..')) { | ||||
|         throw new CustomError('invalid prefix'); | ||||
|       } | ||||
|       return prefix; | ||||
|     }; | ||||
|     const _prefix = handlePrefix(data.prefix); | ||||
|     const prefix = path.join(prefixBase, './', _prefix); | ||||
|     const { len, prefix } = getPrefixByUser(data, tokenUser); | ||||
|     const recursive = data.recursive; | ||||
|     const list = await getMinioList({ prefix: prefix.slice(1), recursive: recursive }); | ||||
|     ctx.body = list; | ||||
|  | ||||
|     ctx.body = list.map((item) => { | ||||
|       if ('prefix' in item) { | ||||
|         return { | ||||
|           ...item, | ||||
|           prefix: item.prefix.slice(len), | ||||
|         }; | ||||
|       } else { | ||||
|         return { ...item, name: item.name.slice(len) }; | ||||
|       } | ||||
|     }); | ||||
|     return ctx; | ||||
|   }) | ||||
|   .addTo(app); | ||||
|  | ||||
| app | ||||
|   .route({ | ||||
|     path: 'file', | ||||
|     key: 'stat', | ||||
|     middleware: ['auth'], | ||||
|   }) | ||||
|   .define(async (ctx) => { | ||||
|     const tokenUser = ctx.state.tokenUser; | ||||
|     const data = ctx.query.data || {}; | ||||
|     const { prefix } = getPrefixByUser(data, tokenUser); | ||||
|     console.log('prefix', prefix); | ||||
|     const stat = await getFileStat(prefix.slice(1)); | ||||
|     ctx.body = stat; | ||||
|     return ctx; | ||||
|   }) | ||||
|   .addTo(app); | ||||
|   | ||||
| @@ -5,17 +5,17 @@ type MinioListOpt = { | ||||
|   prefix: string; | ||||
|   recursive?: boolean; | ||||
| }; | ||||
| type MinioFile = { | ||||
| export type MinioFile = { | ||||
|   name: string; | ||||
|   size: number; | ||||
|   lastModified: Date; | ||||
|   etag: string; | ||||
| }; | ||||
| type MinioDirectory = { | ||||
| export type MinioDirectory = { | ||||
|   prefix: string; | ||||
|   size: number; | ||||
| }; | ||||
| type MinioList = (MinioFile | MinioDirectory)[]; | ||||
| export type MinioList = (MinioFile | MinioDirectory)[]; | ||||
| export const getMinioList = async (opts: MinioListOpt): Promise<MinioList> => { | ||||
|   const prefix = opts.prefix; | ||||
|   const recursive = opts.recursive ?? false; | ||||
| @@ -41,3 +41,15 @@ export const getMinioList = async (opts: MinioListOpt): Promise<MinioList> => { | ||||
|       }); | ||||
|   }); | ||||
| }; | ||||
| export const getFileStat = async (prefix: string): Promise<any> => { | ||||
|   try { | ||||
|     const obj = await minioClient.statObject(bucketName, prefix); | ||||
|     return obj; | ||||
|   } catch (e) { | ||||
|     if (e.code === 'NotFound') { | ||||
|       return null; | ||||
|     } | ||||
|     console.error('get File Stat Error not handle', e); | ||||
|     return null; | ||||
|   } | ||||
| }; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user