import { app } from '@/app.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', key: 'list', middleware: ['auth'], }) .define(async (ctx) => { const tokenUser = ctx.state.tokenUser; const data = ctx.query.data || {}; const { len, prefix } = getPrefixByUser(data, tokenUser); const recursive = data.recursive; const list = await getMinioList({ prefix: prefix.slice(1), recursive: recursive }); 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);