fix: 修改uploadFiles和container上传

This commit is contained in:
xion 2024-10-24 09:23:34 +08:00
parent 0eae9458c6
commit d8fec88e54
4 changed files with 115 additions and 93 deletions

View File

@ -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);

View File

@ -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<InstanceType<typeof ContainerModel>>;

View File

@ -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');

View File

@ -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);