diff --git a/src/routes/container/list.ts b/src/routes/container/list.ts index 5ef89a2..5aa7b04 100644 --- a/src/routes/container/list.ts +++ b/src/routes/container/list.ts @@ -2,10 +2,11 @@ import { CustomError } from '@abearxiong/router'; import { app } from '../../app.ts'; import { ContainerModel, ContainerData, Container } from './models/index.ts'; import semver from 'semver'; +import { uploadMinioContainer } from '../page/module/cache-file.ts'; const list = app.route({ path: 'container', key: 'list', - middleware: ['auth'] + middleware: ['auth'], }); list.run = async (ctx) => { @@ -106,12 +107,13 @@ app .route({ path: 'container', key: 'publish', - nextRoute: { path: 'resource', key: 'publishContainer' }, + // nextRoute: { path: 'resource', key: 'publishContainer' }, + middleware: ['auth'], }) .define(async (ctx) => { - const { data } = ctx.query; - const { id, publish, type = 'patch', beta = false } = data; - type PublishType = 'patch' | 'minor' | 'major'; + const tokenUser = ctx.state.tokenUser; + const { data, token } = ctx.query; + const { id, publish } = data; if (!id) { throw new CustomError('id is required'); } @@ -119,30 +121,31 @@ app if (!container) { throw new CustomError('container not found'); } - const { name, description } = publish; - const oldPublish = container.publish; - let _version = ''; - if (!oldPublish.version) { - if (publish.name) { - throw new CustomError('publish name is required'); - } - container.publish = { - name, - description, - version: '0.0.1', - }; - } else { - _version = semver.inc(oldPublish.version, type as PublishType, beta ? 'beta' : undefined); - container.publish.version = _version; + container.publish = publish; + await container.save(); + const { title, description, key, version, fileName } = publish; + ctx.body = container; + if (!key || !version || !fileName) { + return; } - if (ctx.state) { - ctx.state.container = container; - } else { - ctx.state = { - container, - }; - } - // 执行下一步操作了 - ctx.body = 'run ok'; + const uploadResult = await uploadMinioContainer({ + key, + tokenUser: ctx.state.tokenUser, + version: version, + code: container.code, + filePath: fileName, + }); + await ctx.call({ + path: 'app', + key: 'uploadFiles', + payload: { + token, + data: { + appKey: key, + version, + files: [uploadResult], + }, + }, + }); }) .addTo(app); diff --git a/src/routes/container/models/index.ts b/src/routes/container/models/index.ts index 52f7bbc..97251ee 100644 --- a/src/routes/container/models/index.ts +++ b/src/routes/container/models/index.ts @@ -3,9 +3,10 @@ import { DataTypes, Model } from 'sequelize'; export interface ContainerData {} export type ContainerPublish = { - rid?: string; // resource id - name?: string; + key: string; + title?: string; description?: string; + fileName?: string; version?: string; }; export type Container = Partial>; diff --git a/src/routes/page/module/cache-file.ts b/src/routes/page/module/cache-file.ts index 05fc9f5..a31ee89 100644 --- a/src/routes/page/module/cache-file.ts +++ b/src/routes/page/module/cache-file.ts @@ -65,7 +65,25 @@ export const cachePage = async (page: PageModel, opts: { tokenUser: any; key; ve }, ]; }; - +export const uploadMinioContainer = async ({ tokenUser, key, version, code, filePath }) => { + if ((filePath as string).includes('..')) { + throw new CustomError('file path is invalid'); + } + const minioKeyVersion = `${tokenUser.username}/${key}/${version}`; + const minioPath = path.join(minioKeyVersion, filePath); + console.log('minioPath', minioPath); + // const isHTML = filePath.endsWith('.html'); + const name = minioPath.replace(minioKeyVersion + '/', ''); + await minioClient.putObject(bucketName, minioPath, code, code.length, { + 'Content-Type': getContentType(filePath), + 'app-source': 'user-app', + 'Cache-Control': 'no-cache', // 缓存一年 + }); + return { + name, + path: minioPath, + }; +}; export const uploadMinio = async ({ tokenUser, key, version, path, filePath }) => { const minioPath = `${tokenUser.username}/${key}/${version}/${path}`; const isHTML = filePath.endsWith('.html'); diff --git a/src/routes/resource/publish.ts b/src/routes/resource/publish.ts index c553215..69bdbdb 100644 --- a/src/routes/resource/publish.ts +++ b/src/routes/resource/publish.ts @@ -6,64 +6,64 @@ import { Op } from 'sequelize'; import { publishJsCode } from './lib/publish-minio.ts'; import { CustomError } from '@abearxiong/router'; -app - .route({ - path: 'resource', - key: 'publishContainer', - idUsePath: true, - }) - .define(async (ctx) => { - const container = ctx.state.container as ContainerModel; - const publish = container.publish; - const code = container.code; - let { name, rid, description, version = '0.0.1' } = publish; - const where = []; - if (rid) { - where.push({ id: rid }); - } - if (name) { - where.push({ name }); - } - let resource = await ResourceModel.findOne({ where: { [Op.or]: where } }); - let isCreate = false; - if (!resource) { - isCreate = true; - resource = await ResourceModel.create({ - name, - description, - version, - source: 'container', - sourceId: container.id, - data: { - ...defaultData, - updatedAt: new Date().toISOString(), - }, - }); - } - publish.rid = publish.rid || resource.id; - // TODO: check version - const res = await publishJsCode({ name, version, code }); - if (res.code === 200) { - await container.update({ publish }); - const { etag, versionId, path } = res.data; - resource.version = version; - const newData = { - list: [], - ...resource.data, - }; - newData.list.push({ - etag, - versionId, - path, - }); - newData.lastestVersion = version; - newData.updatedAt = new Date().toISOString(); - resource.data = newData; - await resource.save(); - ctx.body = { resource, container, resourceIsNew: isCreate }; - } else { - throw new CustomError(res.message); - } - // await container.update({ publish }); - }) - .addTo(app); +// app +// .route({ +// path: 'resource', +// key: 'publishContainer', +// idUsePath: true, +// }) +// .define(async (ctx) => { +// const container = ctx.state.container as ContainerModel; +// const publish = container.publish; +// const code = container.code; +// let { name, rid, description, version = '0.0.1' } = publish; +// const where = []; +// if (rid) { +// where.push({ id: rid }); +// } +// if (name) { +// where.push({ name }); +// } +// let resource = await ResourceModel.findOne({ where: { [Op.or]: where } }); +// let isCreate = false; +// if (!resource) { +// isCreate = true; +// resource = await ResourceModel.create({ +// name, +// description, +// version, +// source: 'container', +// sourceId: container.id, +// data: { +// ...defaultData, +// updatedAt: new Date().toISOString(), +// }, +// }); +// } +// publish.rid = publish.rid || resource.id; +// // TODO: check version +// const res = await publishJsCode({ name, version, code }); +// if (res.code === 200) { +// await container.update({ publish }); +// const { etag, versionId, path } = res.data; +// resource.version = version; +// const newData = { +// list: [], +// ...resource.data, +// }; +// newData.list.push({ +// etag, +// versionId, +// path, +// }); +// newData.lastestVersion = version; +// newData.updatedAt = new Date().toISOString(); +// resource.data = newData; +// await resource.save(); +// ctx.body = { resource, container, resourceIsNew: isCreate }; +// } else { +// throw new CustomError(res.message); +// } +// // await container.update({ publish }); +// }) +// .addTo(app);