feat: 新增app管理和文件管理
This commit is contained in:
1
src/routes/file/index.ts
Normal file
1
src/routes/file/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
import './list.ts';
|
||||
30
src/routes/file/list.ts
Normal file
30
src/routes/file/list.ts
Normal 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);
|
||||
43
src/routes/file/module/get-minio-list.ts
Normal file
43
src/routes/file/module/get-minio-list.ts
Normal 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);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user