fix: 修改uploadFiles和container上传
This commit is contained in:
parent
0eae9458c6
commit
d8fec88e54
@ -2,10 +2,11 @@ import { CustomError } from '@abearxiong/router';
|
|||||||
import { app } from '../../app.ts';
|
import { app } from '../../app.ts';
|
||||||
import { ContainerModel, ContainerData, Container } from './models/index.ts';
|
import { ContainerModel, ContainerData, Container } from './models/index.ts';
|
||||||
import semver from 'semver';
|
import semver from 'semver';
|
||||||
|
import { uploadMinioContainer } from '../page/module/cache-file.ts';
|
||||||
const list = app.route({
|
const list = app.route({
|
||||||
path: 'container',
|
path: 'container',
|
||||||
key: 'list',
|
key: 'list',
|
||||||
middleware: ['auth']
|
middleware: ['auth'],
|
||||||
});
|
});
|
||||||
|
|
||||||
list.run = async (ctx) => {
|
list.run = async (ctx) => {
|
||||||
@ -106,12 +107,13 @@ app
|
|||||||
.route({
|
.route({
|
||||||
path: 'container',
|
path: 'container',
|
||||||
key: 'publish',
|
key: 'publish',
|
||||||
nextRoute: { path: 'resource', key: 'publishContainer' },
|
// nextRoute: { path: 'resource', key: 'publishContainer' },
|
||||||
|
middleware: ['auth'],
|
||||||
})
|
})
|
||||||
.define(async (ctx) => {
|
.define(async (ctx) => {
|
||||||
const { data } = ctx.query;
|
const tokenUser = ctx.state.tokenUser;
|
||||||
const { id, publish, type = 'patch', beta = false } = data;
|
const { data, token } = ctx.query;
|
||||||
type PublishType = 'patch' | 'minor' | 'major';
|
const { id, publish } = data;
|
||||||
if (!id) {
|
if (!id) {
|
||||||
throw new CustomError('id is required');
|
throw new CustomError('id is required');
|
||||||
}
|
}
|
||||||
@ -119,30 +121,31 @@ app
|
|||||||
if (!container) {
|
if (!container) {
|
||||||
throw new CustomError('container not found');
|
throw new CustomError('container not found');
|
||||||
}
|
}
|
||||||
const { name, description } = publish;
|
container.publish = publish;
|
||||||
const oldPublish = container.publish;
|
await container.save();
|
||||||
let _version = '';
|
const { title, description, key, version, fileName } = publish;
|
||||||
if (!oldPublish.version) {
|
ctx.body = container;
|
||||||
if (publish.name) {
|
if (!key || !version || !fileName) {
|
||||||
throw new CustomError('publish name is required');
|
return;
|
||||||
}
|
|
||||||
container.publish = {
|
|
||||||
name,
|
|
||||||
description,
|
|
||||||
version: '0.0.1',
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
_version = semver.inc(oldPublish.version, type as PublishType, beta ? 'beta' : undefined);
|
|
||||||
container.publish.version = _version;
|
|
||||||
}
|
}
|
||||||
if (ctx.state) {
|
const uploadResult = await uploadMinioContainer({
|
||||||
ctx.state.container = container;
|
key,
|
||||||
} else {
|
tokenUser: ctx.state.tokenUser,
|
||||||
ctx.state = {
|
version: version,
|
||||||
container,
|
code: container.code,
|
||||||
};
|
filePath: fileName,
|
||||||
}
|
});
|
||||||
// 执行下一步操作了
|
await ctx.call({
|
||||||
ctx.body = 'run ok';
|
path: 'app',
|
||||||
|
key: 'uploadFiles',
|
||||||
|
payload: {
|
||||||
|
token,
|
||||||
|
data: {
|
||||||
|
appKey: key,
|
||||||
|
version,
|
||||||
|
files: [uploadResult],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
})
|
})
|
||||||
.addTo(app);
|
.addTo(app);
|
||||||
|
@ -3,9 +3,10 @@ import { DataTypes, Model } from 'sequelize';
|
|||||||
|
|
||||||
export interface ContainerData {}
|
export interface ContainerData {}
|
||||||
export type ContainerPublish = {
|
export type ContainerPublish = {
|
||||||
rid?: string; // resource id
|
key: string;
|
||||||
name?: string;
|
title?: string;
|
||||||
description?: string;
|
description?: string;
|
||||||
|
fileName?: string;
|
||||||
version?: string;
|
version?: string;
|
||||||
};
|
};
|
||||||
export type Container = Partial<InstanceType<typeof ContainerModel>>;
|
export type Container = Partial<InstanceType<typeof ContainerModel>>;
|
||||||
|
@ -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 }) => {
|
export const uploadMinio = async ({ tokenUser, key, version, path, filePath }) => {
|
||||||
const minioPath = `${tokenUser.username}/${key}/${version}/${path}`;
|
const minioPath = `${tokenUser.username}/${key}/${version}/${path}`;
|
||||||
const isHTML = filePath.endsWith('.html');
|
const isHTML = filePath.endsWith('.html');
|
||||||
|
@ -6,64 +6,64 @@ import { Op } from 'sequelize';
|
|||||||
import { publishJsCode } from './lib/publish-minio.ts';
|
import { publishJsCode } from './lib/publish-minio.ts';
|
||||||
import { CustomError } from '@abearxiong/router';
|
import { CustomError } from '@abearxiong/router';
|
||||||
|
|
||||||
app
|
// app
|
||||||
.route({
|
// .route({
|
||||||
path: 'resource',
|
// path: 'resource',
|
||||||
key: 'publishContainer',
|
// key: 'publishContainer',
|
||||||
idUsePath: true,
|
// idUsePath: true,
|
||||||
})
|
// })
|
||||||
.define(async (ctx) => {
|
// .define(async (ctx) => {
|
||||||
const container = ctx.state.container as ContainerModel;
|
// const container = ctx.state.container as ContainerModel;
|
||||||
const publish = container.publish;
|
// const publish = container.publish;
|
||||||
const code = container.code;
|
// const code = container.code;
|
||||||
let { name, rid, description, version = '0.0.1' } = publish;
|
// let { name, rid, description, version = '0.0.1' } = publish;
|
||||||
const where = [];
|
// const where = [];
|
||||||
if (rid) {
|
// if (rid) {
|
||||||
where.push({ id: rid });
|
// where.push({ id: rid });
|
||||||
}
|
// }
|
||||||
if (name) {
|
// if (name) {
|
||||||
where.push({ name });
|
// where.push({ name });
|
||||||
}
|
// }
|
||||||
let resource = await ResourceModel.findOne({ where: { [Op.or]: where } });
|
// let resource = await ResourceModel.findOne({ where: { [Op.or]: where } });
|
||||||
let isCreate = false;
|
// let isCreate = false;
|
||||||
if (!resource) {
|
// if (!resource) {
|
||||||
isCreate = true;
|
// isCreate = true;
|
||||||
resource = await ResourceModel.create({
|
// resource = await ResourceModel.create({
|
||||||
name,
|
// name,
|
||||||
description,
|
// description,
|
||||||
version,
|
// version,
|
||||||
source: 'container',
|
// source: 'container',
|
||||||
sourceId: container.id,
|
// sourceId: container.id,
|
||||||
data: {
|
// data: {
|
||||||
...defaultData,
|
// ...defaultData,
|
||||||
updatedAt: new Date().toISOString(),
|
// updatedAt: new Date().toISOString(),
|
||||||
},
|
// },
|
||||||
});
|
// });
|
||||||
}
|
// }
|
||||||
publish.rid = publish.rid || resource.id;
|
// publish.rid = publish.rid || resource.id;
|
||||||
// TODO: check version
|
// // TODO: check version
|
||||||
const res = await publishJsCode({ name, version, code });
|
// const res = await publishJsCode({ name, version, code });
|
||||||
if (res.code === 200) {
|
// if (res.code === 200) {
|
||||||
await container.update({ publish });
|
// await container.update({ publish });
|
||||||
const { etag, versionId, path } = res.data;
|
// const { etag, versionId, path } = res.data;
|
||||||
resource.version = version;
|
// resource.version = version;
|
||||||
const newData = {
|
// const newData = {
|
||||||
list: [],
|
// list: [],
|
||||||
...resource.data,
|
// ...resource.data,
|
||||||
};
|
// };
|
||||||
newData.list.push({
|
// newData.list.push({
|
||||||
etag,
|
// etag,
|
||||||
versionId,
|
// versionId,
|
||||||
path,
|
// path,
|
||||||
});
|
// });
|
||||||
newData.lastestVersion = version;
|
// newData.lastestVersion = version;
|
||||||
newData.updatedAt = new Date().toISOString();
|
// newData.updatedAt = new Date().toISOString();
|
||||||
resource.data = newData;
|
// resource.data = newData;
|
||||||
await resource.save();
|
// await resource.save();
|
||||||
ctx.body = { resource, container, resourceIsNew: isCreate };
|
// ctx.body = { resource, container, resourceIsNew: isCreate };
|
||||||
} else {
|
// } else {
|
||||||
throw new CustomError(res.message);
|
// throw new CustomError(res.message);
|
||||||
}
|
// }
|
||||||
// await container.update({ publish });
|
// // await container.update({ publish });
|
||||||
})
|
// })
|
||||||
.addTo(app);
|
// .addTo(app);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user