feat: 上传文件到minio
This commit is contained in:
@@ -10,10 +10,15 @@ app
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const data = ctx.query.data || {};
|
||||
if (!data.key) {
|
||||
throw new CustomError('key is required');
|
||||
}
|
||||
const list = await AppListModel.findAll({
|
||||
order: [['updatedAt', 'DESC']],
|
||||
where: {
|
||||
uid: tokenUser.id,
|
||||
key: data.key,
|
||||
},
|
||||
});
|
||||
ctx.body = list;
|
||||
@@ -61,6 +66,10 @@ app
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (!rest.key) {
|
||||
throw new CustomError('key is required');
|
||||
}
|
||||
const app = await AppListModel.create({ data, ...rest, uid: tokenUser.id });
|
||||
ctx.body = app;
|
||||
return ctx;
|
||||
@@ -82,7 +91,9 @@ app
|
||||
if (!app) {
|
||||
throw new CustomError('app not found');
|
||||
}
|
||||
await app.destroy();
|
||||
await app.destroy({
|
||||
force: true,
|
||||
});
|
||||
ctx.body = 'success';
|
||||
return ctx;
|
||||
})
|
||||
|
||||
@@ -11,8 +11,7 @@ export class AppListModel extends Model {
|
||||
declare id: string;
|
||||
declare data: AppData;
|
||||
declare version: string;
|
||||
declare appType: AppType;
|
||||
declare type: string;
|
||||
declare key: string;
|
||||
declare uid: string;
|
||||
}
|
||||
|
||||
@@ -32,13 +31,8 @@ AppListModel.init(
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: '',
|
||||
},
|
||||
appType: {
|
||||
key: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: '',
|
||||
},
|
||||
type: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: '',
|
||||
},
|
||||
uid: {
|
||||
type: DataTypes.UUID,
|
||||
|
||||
@@ -14,6 +14,8 @@ export type App = Partial<InstanceType<typeof AppModel>>;
|
||||
export class AppModel extends Model {
|
||||
declare id: string;
|
||||
declare data: AppData;
|
||||
declare title: string;
|
||||
declare description: string;
|
||||
declare version: string;
|
||||
declare domain: string;
|
||||
declare appType: string;
|
||||
@@ -21,6 +23,7 @@ export class AppModel extends Model {
|
||||
declare type: string;
|
||||
declare uid: string;
|
||||
declare user: string;
|
||||
declare status: string;
|
||||
}
|
||||
AppModel.init(
|
||||
{
|
||||
@@ -30,6 +33,14 @@ AppModel.init(
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
comment: 'id',
|
||||
},
|
||||
title: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: '',
|
||||
},
|
||||
description: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: '',
|
||||
},
|
||||
data: {
|
||||
type: DataTypes.JSON,
|
||||
defaultValue: {},
|
||||
@@ -48,7 +59,7 @@ AppModel.init(
|
||||
},
|
||||
key: {
|
||||
type: DataTypes.STRING,
|
||||
unique: true,
|
||||
// 和 uid 组合唯一
|
||||
},
|
||||
type: {
|
||||
type: DataTypes.STRING,
|
||||
@@ -58,11 +69,25 @@ AppModel.init(
|
||||
type: DataTypes.UUID,
|
||||
allowNull: true,
|
||||
},
|
||||
user: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: true,
|
||||
},
|
||||
status: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: 'running', // stop, running
|
||||
},
|
||||
},
|
||||
{
|
||||
sequelize,
|
||||
tableName: 'kv_app',
|
||||
paranoid: true,
|
||||
indexes: [
|
||||
{
|
||||
unique: true,
|
||||
fields: ['key', 'uid'],
|
||||
},
|
||||
],
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
@@ -48,6 +48,8 @@ app
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
|
||||
const { data, id, ...rest } = ctx.query.data;
|
||||
if (id) {
|
||||
const app = await AppModel.findByPk(id);
|
||||
@@ -60,8 +62,19 @@ app
|
||||
}
|
||||
return;
|
||||
}
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const app = await AppModel.create({ data, ...rest, uid: tokenUser.id });
|
||||
if (!rest.key) {
|
||||
throw new CustomError('key is required');
|
||||
}
|
||||
const findApp = await AppModel.findOne({ where: { key: rest.key, uid: tokenUser.id } });
|
||||
if (findApp) {
|
||||
throw new CustomError('key already exists');
|
||||
}
|
||||
const app = await AppModel.create({
|
||||
data: { files: [] },
|
||||
...rest,
|
||||
uid: tokenUser.id,
|
||||
user: tokenUser.username,
|
||||
});
|
||||
ctx.body = app;
|
||||
return ctx;
|
||||
})
|
||||
|
||||
@@ -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