2022-05-27 06:23:52 +00:00
|
|
|
const fs = require('fs-extra');
|
|
|
|
const path = require('path');
|
|
|
|
|
2022-05-25 12:59:28 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('upload', {
|
2022-05-27 11:39:33 +00:00
|
|
|
description: 'Upload and attach a access file',
|
2022-05-25 12:59:28 +00:00
|
|
|
accepts: [
|
|
|
|
{
|
|
|
|
arg: 'appName',
|
|
|
|
type: 'string',
|
|
|
|
required: true,
|
|
|
|
description: 'The app name'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'newVersion',
|
|
|
|
type: 'number',
|
|
|
|
required: true,
|
|
|
|
description: `The new version number`
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'branch',
|
|
|
|
type: 'string',
|
|
|
|
required: true,
|
|
|
|
description: `The branch name`
|
|
|
|
}
|
|
|
|
],
|
|
|
|
returns: {
|
|
|
|
type: ['object'],
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/upload`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.upload = async(ctx, appName, newVersion, branch, options) => {
|
|
|
|
const models = Self.app.models;
|
|
|
|
const myOptions = {};
|
2022-05-27 06:23:52 +00:00
|
|
|
|
|
|
|
const TempContainer = models.TempContainer;
|
|
|
|
const AccessContainer = models.AccessContainer;
|
|
|
|
const fileOptions = {};
|
2022-05-25 12:59:28 +00:00
|
|
|
|
2022-05-27 06:23:52 +00:00
|
|
|
let tx;
|
2022-05-25 12:59:28 +00:00
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
2022-05-27 06:23:52 +00:00
|
|
|
if (!myOptions.transaction) {
|
|
|
|
tx = await Self.beginTransaction({});
|
|
|
|
myOptions.transaction = tx;
|
|
|
|
}
|
|
|
|
|
2022-05-31 07:06:15 +00:00
|
|
|
let srcFile;
|
2022-05-27 06:23:52 +00:00
|
|
|
try {
|
|
|
|
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 => {
|
|
|
|
return file[0];
|
|
|
|
});
|
|
|
|
const uploadedFile = files[0];
|
2022-05-27 11:27:05 +00:00
|
|
|
|
2022-05-27 06:23:52 +00:00
|
|
|
const file = await TempContainer.getFile(tempContainer.name, uploadedFile.name);
|
2022-05-31 07:06:15 +00:00
|
|
|
srcFile = path.join(file.client.root, file.container, file.name);
|
2022-05-27 06:23:52 +00:00
|
|
|
|
2022-05-31 07:06:15 +00:00
|
|
|
const accessContainer = await AccessContainer.container('.archive');
|
|
|
|
const destinationFile = path.join(
|
|
|
|
accessContainer.client.root, accessContainer.name, appName, `${newVersion}.7z`);
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV == 'test') {
|
|
|
|
try {
|
|
|
|
await fs.unlink(srcFile);
|
|
|
|
} catch (e) {}
|
|
|
|
} else {
|
|
|
|
await fs.move(srcFile, destinationFile, {
|
|
|
|
overwrite: true
|
|
|
|
});
|
|
|
|
await fs.chmod(destinationFile, 0o644);
|
|
|
|
|
|
|
|
const branchPath = path.join(accessContainer.client.root, 'branches', branch);
|
|
|
|
await fs.mkdir(branchPath, {recursive: true});
|
|
|
|
|
|
|
|
const destinationBranch = path.join(branchPath, `${appName}.7z`);
|
|
|
|
const destinationRoot = path.join(accessContainer.client.root, `${appName}.7z`);
|
|
|
|
try {
|
|
|
|
await fs.unlink(destinationBranch);
|
|
|
|
} catch (e) {}
|
|
|
|
await fs.symlink(destinationFile, destinationBranch);
|
|
|
|
|
|
|
|
if (branch == 'master') {
|
|
|
|
try {
|
|
|
|
await fs.unlink(destinationRoot);
|
|
|
|
} catch (e) {}
|
|
|
|
await fs.symlink(destinationFile, destinationRoot);
|
|
|
|
}
|
|
|
|
}
|
2022-05-27 06:23:52 +00:00
|
|
|
|
2022-05-31 07:06:15 +00:00
|
|
|
const version = await models.MdbVersion.findOne({
|
|
|
|
where: {
|
|
|
|
app: appName,
|
|
|
|
branchFk: branch
|
|
|
|
}
|
2022-05-27 06:23:52 +00:00
|
|
|
}, myOptions);
|
|
|
|
|
2022-05-31 07:06:15 +00:00
|
|
|
if (!version) {
|
|
|
|
return await models.MdbVersion.create({
|
|
|
|
app: appName,
|
|
|
|
branchFk: branch,
|
|
|
|
version: newVersion
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
await version.updateAttributes({version: newVersion}, myOptions);
|
|
|
|
|
2022-05-27 06:23:52 +00:00
|
|
|
if (tx) await tx.commit();
|
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
2022-05-31 07:06:15 +00:00
|
|
|
|
|
|
|
if (await fs.stat(srcFile))
|
|
|
|
await fs.unlink(srcFile);
|
|
|
|
|
2022-05-27 06:23:52 +00:00
|
|
|
throw e;
|
|
|
|
}
|
2022-05-25 12:59:28 +00:00
|
|
|
};
|
|
|
|
};
|