import { app } from '@/app.ts'; import { ConfigModel } from './models/model.ts'; import { ShareConfigService } from './services/share.ts'; import { oss } from '@/app.ts'; import { ConfigOssService } from '@kevisual/oss/services'; app .route({ path: 'config', key: 'list', middleware: ['auth'], }) .define(async (ctx) => { const { id } = ctx.state.tokenUser; const config = await ConfigModel.findAll({ where: { uid: id, }, order: [['updatedAt', 'DESC']], }); ctx.body = { list: config, }; }) .addTo(app); app .route({ path: 'config', key: 'update', middleware: ['auth'], }) .define(async (ctx) => { const tokernUser = ctx.state.tokenUser; const tuid = tokernUser.id; const { id, data, ...rest } = ctx.query?.data || {}; let config: ConfigModel; if (id) { config = await ConfigModel.findByPk(id); let keyIsChange = false; if (rest?.key) { keyIsChange = rest.key !== config?.key; } if (!config || config.uid !== tuid) { ctx.throw(403, 'no permission'); } if (keyIsChange) { const key = rest.key; const keyConfig = await ConfigModel.findOne({ where: { key, uid: tuid, }, }); if (keyConfig && keyConfig.id !== id) { ctx.throw(403, 'key is already exists'); } } await config.update({ data: { ...config.data, ...data, }, ...rest, }); if (config.data?.permission?.share === 'public') { await ShareConfigService.expireShareConfig(config.key, tokernUser.username); } ctx.body = config; } else if (rest?.key) { // id 不存在,key存在,则属于更新,key不能重复 const key = rest.key; config = await ConfigModel.findOne({ where: { key, uid: tuid, }, }); if (config) { await config.update({ data: { ...config.data, ...data }, ...rest, }); ctx.body = config; } else { // 根据key创建一个配置 config = await ConfigModel.create({ key, ...rest, data: data, uid: tuid, }); ctx.body = config; } } const key = config?.key; const ossConfig = ConfigOssService.fromBase({ oss, opts: { owner: tokernUser.username, }, }); if (ossConfig.isEndWithJson(key)) { const data = config.data; const hash = ossConfig.hash(data); if (config.hash !== hash) { config.hash = hash; await config.save({ fields: ['hash'], }); await ossConfig.putJsonObject(key, data); } } if (config) return; // id和key不存在。创建一个新的配置, 而且没有id的 const newConfig = await ConfigModel.create({ ...rest, data: data, uid: tuid, }); ctx.body = newConfig; }) .addTo(app); app .route({ path: 'config', key: 'get', middleware: ['auth'], }) .define(async (ctx) => { const tokernUser = ctx.state.tokenUser; const tuid = tokernUser.id; const { id, key } = ctx.query?.data || {}; if (!id && !key) { ctx.throw(400, 'id or key is required'); } let config: ConfigModel; if (id) { config = await ConfigModel.findByPk(id); } if (!config && key) { config = await ConfigModel.findOne({ where: { key, uid: tuid, }, }); } if (!config) { ctx.throw(404, 'config not found'); } if (config && config.uid === tuid) { ctx.body = config; } else { ctx.throw(403, 'no permission'); } }) .addTo(app); app .route({ path: 'config', key: 'delete', middleware: ['auth'], }) .define(async (ctx) => { const tokernUser = ctx.state.tokenUser; const tuid = tokernUser.id; const { id, key } = ctx.query?.data || {}; if (id || key) { const search: any = id ? { id } : { key }; const config = await ConfigModel.findOne({ where: { ...search }, }); if (config && config.uid === tuid) { const key = config.key; const ossConfig = ConfigOssService.fromBase({ oss, opts: { owner: tokernUser.username, }, }); if (ossConfig.isEndWithJson(key)) { try { await ossConfig.deleteObject(key); } catch (e) { } } await config.destroy(); } else { ctx.throw(403, 'no permission'); } } else { ctx.throw(400, 'id is required'); } }) .addTo(app);