feat: 新增app管理和文件管理

This commit is contained in:
2024-10-06 20:10:20 +08:00
parent 1f81d3400c
commit 477ad00d86
18 changed files with 713 additions and 0 deletions

1
src/routes/file/index.ts Normal file
View File

@@ -0,0 +1 @@
import './list.ts';

30
src/routes/file/list.ts Normal file
View File

@@ -0,0 +1,30 @@
import { app } from '@/app.ts';
import { getMinioList } from './module/get-minio-list.ts';
import path from 'path';
import { CustomError } from '@abearxiong/router';
app
.route({
path: 'file',
key: 'list',
middleware: ['auth'],
})
.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 recursive = data.recursive;
const list = await getMinioList({ prefix: prefix.slice(1), recursive: recursive });
ctx.body = list;
return ctx;
})
.addTo(app);

View File

@@ -0,0 +1,43 @@
import { minioClient } from '@/app.ts';
import { bucketName } from '@/modules/minio.ts';
type MinioListOpt = {
prefix: string;
recursive?: boolean;
};
type MinioFile = {
name: string;
size: number;
lastModified: Date;
etag: string;
};
type MinioDirectory = {
prefix: string;
size: number;
};
type MinioList = (MinioFile | MinioDirectory)[];
export const getMinioList = async (opts: MinioListOpt): Promise<MinioList> => {
const prefix = opts.prefix;
const recursive = opts.recursive ?? false;
return await new Promise((resolve, reject) => {
let res: any[] = [];
let hasError = false;
minioClient
.listObjectsV2(bucketName, prefix, recursive)
.on('data', (data) => {
res.push(data);
})
.on('error', (err) => {
console.error('minio error', opts.prefix, err);
hasError = true;
})
.on('end', () => {
if (hasError) {
reject();
return;
} else {
resolve(res);
}
});
});
};