refs #4975 Upload method modified and addedd model
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Guillermo Bonet 2022-12-22 07:12:09 +01:00
parent dc82e610b7
commit d97a2738a4
2 changed files with 71 additions and 17 deletions

View File

@ -12,7 +12,7 @@ module.exports = Self => {
required: true,
description: 'The app name'
}, {
arg: 'newVersion',
arg: 'toVersion',
type: 'number',
required: true,
description: `The new version number`
@ -21,6 +21,11 @@ module.exports = Self => {
type: 'string',
required: true,
description: `The branch name`
}, {
arg: 'fromVersion',
type: 'string',
required: true,
description: `The old version number`
}
],
returns: {
@ -33,14 +38,12 @@ module.exports = Self => {
}
});
Self.upload = async(ctx, appName, newVersion, branch, options) => {
Self.upload = async(ctx, appName, toVersion, branch, fromVersion, options) => {
const models = Self.app.models;
const myOptions = {};
const TempContainer = models.TempContainer;
const AccessContainer = models.AccessContainer;
const fileOptions = {};
let tx;
if (typeof options == 'object')
@ -53,6 +56,29 @@ module.exports = Self => {
let srcFile;
try {
const existBranch = await models.MdbBranch.findOne({
where: {name: branch}
}, myOptions);
if (!existBranch)
throw new UserError('Not exist this branch');
let lastVersion = await models.MdbBranch.findOne({
where: {appName}
}, myOptions);
if (lastVersion++ != toVersion)
throw new UserError('Try again');
const userId = ctx.req.accessToken.userId;
models.MdbVersionTree.create({
app: appName,
version: toVersion,
branchFk: branch,
fromVersion,
userFk: userId
});
const tempContainer = await TempContainer.container('access');
const uploaded = await TempContainer.upload(tempContainer.name, ctx.req, ctx.result, fileOptions);
const files = Object.values(uploaded.files).map(file => {
@ -65,7 +91,7 @@ module.exports = Self => {
const accessContainer = await AccessContainer.container('.archive');
const destinationFile = path.join(
accessContainer.client.root, accessContainer.name, appName, `${newVersion}.7z`);
accessContainer.client.root, accessContainer.name, appName, `${toVersion}.7z`);
if (process.env.NODE_ENV == 'test')
await fs.unlink(srcFile);
@ -75,18 +101,11 @@ module.exports = Self => {
});
await fs.chmod(destinationFile, 0o644);
const existBranch = await models.MdbBranch.findOne({
where: {name: branch}
});
if (!existBranch)
throw new UserError('Not exist this branch');
const branchPath = path.join(accessContainer.client.root, 'branches', branch);
await fs.mkdir(branchPath, {recursive: true});
const destinationBranch = path.join(branchPath, `${appName}.7z`);
const destinationRelative = `../../.archive/${appName}/${newVersion}.7z`;
const destinationRelative = `../../.archive/${appName}/${toVersion}.7z`;
try {
await fs.unlink(destinationBranch);
} catch (e) {}
@ -94,7 +113,7 @@ module.exports = Self => {
if (branch == 'master') {
const destinationRoot = path.join(accessContainer.client.root, `${appName}.7z`);
const rootRelative = `./.archive/${appName}/${newVersion}.7z`;
const rootRelative = `./.archive/${appName}/${toVersion}.7z`;
try {
await fs.unlink(destinationRoot);
} catch (e) {}
@ -105,15 +124,15 @@ module.exports = Self => {
await models.MdbVersion.upsert({
app: appName,
branchFk: branch,
version: newVersion
});
version: toVersion
}, myOptions);
if (tx) await tx.commit();
} catch (e) {
if (tx) await tx.rollback();
if (fs.existsSync(srcFile))
await fs.unlink(srcFile);
fs.unlink(srcFile);
throw e;
}

View File

@ -0,0 +1,35 @@
{
"name": "MdbVersionTree",
"base": "VnModel",
"options": {
"mysql": {
"table": "mdbVersionTree"
}
},
"properties": {
"app": {
"type": "string",
"description": "The app name",
"id": true
},
"version": {
"type": "number"
},
"branchFk": {
"type": "string"
},
"fromVersion": {
"type": "number"
},
"userFk": {
"type": "number"
}
},
"relations": {
"branch": {
"type": "belongsTo",
"model": "MdbBranch",
"foreignKey": "branchFk"
}
}
}