feat: add listener
This commit is contained in:
1
src/routes/file-listener/index.ts
Normal file
1
src/routes/file-listener/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
import './list.ts';
|
||||
107
src/routes/file-listener/list.ts
Normal file
107
src/routes/file-listener/list.ts
Normal file
@@ -0,0 +1,107 @@
|
||||
import { Op } from 'sequelize';
|
||||
import { app } from '@/app.ts';
|
||||
import { FileSyncModel } from './model.ts';
|
||||
app
|
||||
.route({
|
||||
path: 'file-listener',
|
||||
key: 'list',
|
||||
middleware: ['auth'],
|
||||
description: '获取用户的某一个文件夹下的所有的列表的数据',
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const username = tokenUser.username;
|
||||
const { page = 1, pageSize = 20, sort = 'DESC' } = ctx.query;
|
||||
let { prefix } = ctx.query;
|
||||
if (prefix) {
|
||||
if (typeof prefix !== 'string') {
|
||||
ctx.throw(400, 'prefix must be a string');
|
||||
}
|
||||
if (prefix.startsWith('/')) {
|
||||
prefix = prefix.slice(1); // Remove leading slash if present
|
||||
}
|
||||
if (!prefix.startsWith(username + '/')) {
|
||||
ctx.throw(400, 'prefix must start with the your username:', username);
|
||||
}
|
||||
}
|
||||
const searchWhere = prefix
|
||||
? {
|
||||
[Op.or]: [{ name: { [Op.like]: `${prefix}%` } }],
|
||||
}
|
||||
: {};
|
||||
|
||||
const { rows: files, count } = await FileSyncModel.findAndCountAll({
|
||||
where: {
|
||||
...searchWhere,
|
||||
},
|
||||
offset: (page - 1) * pageSize,
|
||||
limit: pageSize,
|
||||
order: [['updatedAt', sort]],
|
||||
});
|
||||
const getPublicFiles = (files: FileSyncModel[]) => {
|
||||
return files.map((file) => {
|
||||
const value = file.toJSON();
|
||||
const stat = value.stat || {};
|
||||
delete stat.password;
|
||||
return {
|
||||
...value,
|
||||
stat: stat,
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
ctx.body = {
|
||||
list: getPublicFiles(files),
|
||||
pagination: {
|
||||
page,
|
||||
current: page,
|
||||
pageSize,
|
||||
total: count,
|
||||
},
|
||||
};
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'file-listener',
|
||||
key: 'get',
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const username = tokenUser.username;
|
||||
const { id, name, hash } = ctx.query.data || {};
|
||||
|
||||
if (!id && !name && !hash) {
|
||||
ctx.throw(400, 'id, name or hash is required');
|
||||
}
|
||||
let fileSync: FileSyncModel | null = null;
|
||||
if (id) {
|
||||
fileSync = await FileSyncModel.findByPk(id);
|
||||
}
|
||||
if (name && !fileSync) {
|
||||
fileSync = await FileSyncModel.findOne({
|
||||
where: {
|
||||
name,
|
||||
hash,
|
||||
},
|
||||
});
|
||||
}
|
||||
if (!fileSync && hash) {
|
||||
fileSync = await FileSyncModel.findOne({
|
||||
where: {
|
||||
name: {
|
||||
[Op.like]: `${username}/%`,
|
||||
},
|
||||
hash,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (!fileSync || !fileSync.name.startsWith(`${username}/`)) {
|
||||
ctx.throw(404, 'NotFoundFile');
|
||||
}
|
||||
ctx.body = fileSync;
|
||||
})
|
||||
.addTo(app);
|
||||
3
src/routes/file-listener/model.ts
Normal file
3
src/routes/file-listener/model.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { FileSyncModel, FileSyncModelType } from '@kevisual/file-listener/src/file-sync/model.ts';
|
||||
|
||||
export { FileSyncModel, FileSyncModelType };
|
||||
@@ -13,3 +13,5 @@ import './micro-app/index.ts';
|
||||
import './config/index.ts';
|
||||
|
||||
import './mark/index.ts';
|
||||
|
||||
import './file-listener/index.ts';
|
||||
|
||||
Reference in New Issue
Block a user