4108-mdb_backend #987

Merged
juan merged 9 commits from 4108-mdb_backend into dev 2022-06-02 07:33:24 +00:00
13 changed files with 225 additions and 3 deletions

View File

@ -5,17 +5,17 @@ module.exports = Self => {
accepts: [
{
arg: 'id',
type: 'Number',
type: 'number',
description: 'The user id',
http: {source: 'path'}
}, {
arg: 'oldPassword',
type: 'String',
type: 'string',
description: 'The old password',
required: true
}, {
arg: 'newPassword',
type: 'String',
type: 'string',
description: 'The new password',
required: true
}

View File

@ -0,0 +1,14 @@
CREATE TABLE `vn`.`mdbBranch` (
`name` VARCHAR(255),
PRIMARY KEY(`name`)
);
CREATE TABLE `vn`.`mdbVersion` (
`app` VARCHAR(255) NOT NULL,
`branchFk` VARCHAR(255) NOT NULL,
`version` INT,
CONSTRAINT `mdbVersion_branchFk` FOREIGN KEY (`branchFk`) REFERENCES `vn`.`mdbBranch` (`name`) ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`)
VALUES('MdbVersion', '*', '*', 'ALLOW', 'ROLE', 'developer');

View File

@ -2577,3 +2577,12 @@ INSERT INTO `vn`.`machineWorker` (`workerFk`, `machineFk`, `inTimed`, `outTimed`
(1106, 2, CURDATE(), NULL),
(1106, 2, DATE_ADD(CURDATE(), INTERVAL + 1 DAY), DATE_ADD(CURDATE(), INTERVAL +1 DAY));
INSERT INTO `vn`.`mdbBranch` (`name`)
VALUES
('test'),
('master');
INSERT INTO `vn`.`mdbVersion` (`app`, `branchFk`, `version`)
VALUES
('tpv', 'test', '1'),
('lab', 'master', '1');

View File

@ -32,6 +32,7 @@ services:
- /mnt/appdata/pdfs:/var/lib/salix/pdfs
- /mnt/appdata/dms:/var/lib/salix/dms
- /mnt/appdata/image:/var/lib/salix/image
- /mnt/appdata/vn-access:/var/lib/salix/vn-access
deploy:
replicas: ${BACK_REPLICAS:?}
placement:

View File

@ -98,5 +98,15 @@
"image/jpg",
"video/mp4"
]
},
"accessStorage": {
"name": "accessStorage",
"connector": "loopback-component-storage",
"provider": "filesystem",
"root": "./storage/access",
"maxFileSize": "524288000",
alexm marked this conversation as resolved Outdated
Outdated
Review

Fica màxim 500MB, crec que el valor està en bytes, comprova-ho en la documentació de looback storage.

Fica màxim 500MB, crec que el valor està en bytes, comprova-ho en la documentació de looback storage.
"allowedContentTypes": [
"application/x-7z-compressed"
]
}
}

View File

@ -0,0 +1,121 @@
const fs = require('fs-extra');
const path = require('path');
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('upload', {
description: 'Upload and attach a access file',
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 = {};
const TempContainer = models.TempContainer;
const AccessContainer = models.AccessContainer;
const fileOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
let srcFile;
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];
const file = await TempContainer.getFile(tempContainer.name, uploadedFile.name);
srcFile = path.join(file.client.root, file.container, file.name);
const accessContainer = await AccessContainer.container('.archive');
juan marked this conversation as resolved Outdated
Outdated
Review

Cuando NODE_ENV es test no hay que guardar el fichero, si no sobreescribira el de producción. Añadir un if para evitar el guardado en ese caso.

Cuando `NODE_ENV` es `test` no hay que guardar el fichero, si no sobreescribira el de producción. Añadir un if para evitar el guardado en ese caso.
const destinationFile = path.join(
accessContainer.client.root, accessContainer.name, appName, `${newVersion}.7z`);
if (process.env.NODE_ENV == 'test')
alexm marked this conversation as resolved Outdated
Outdated
Review

Lleva el try/catch, si intenta borrar el fitxer i no pot, ha de tornar el error.

Lleva el try/catch, si intenta borrar el fitxer i no pot, ha de tornar el error.
await fs.unlink(srcFile);
else {
await fs.move(srcFile, destinationFile, {
overwrite: true
});
await fs.chmod(destinationFile, 0o644);
const existBranch = await models.MdbBranch.findOne({
where: {name: branch}
alexm marked this conversation as resolved Outdated
Outdated
Review

Abans de crear el directori, comprovar que la rama existeix en la taula mdbBranch, en cas contrari llançar un error notificant que la rama no existeix

Abans de crear el directori, comprovar que la rama existeix en la taula mdbBranch, en cas contrari llançar un error notificant que la rama no existeix
});
if (!existBranch)
throw new UserError('Not exist this branch');
const branchPath = path.join(accessContainer.client.root, 'branches', branch);
alexm marked this conversation as resolved
Review

Es necesari borrarlo primer? fs.symlink no el reescriu si existeix?

Es necesari borrarlo primer? fs.symlink no el reescriu si existeix?
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);
alexm marked this conversation as resolved
Review

Es necesari borrarlo primer? fs.symlink no el reescriu si existeix?

Es necesari borrarlo primer? fs.symlink no el reescriu si existeix?
} catch (e) {}
await fs.symlink(destinationFile, destinationBranch);
if (branch == 'master') {
try {
await fs.unlink(destinationRoot);
} catch (e) {}
await fs.symlink(destinationFile, destinationRoot);
}
}
await models.MdbVersion.upsert({
app: appName,
alexm marked this conversation as resolved Outdated
Outdated
Review

Pots gastar upsert, aixina en menys codi insertes (si no existeix) o actualitzes.

https://loopback.io/doc/en/lb3/Creating-updating-and-deleting-data.html

Pots gastar upsert, aixina en menys codi insertes (si no existeix) o actualitzes. https://loopback.io/doc/en/lb3/Creating-updating-and-deleting-data.html
branchFk: branch,
version: newVersion
});
if (tx) await tx.commit();
} catch (e) {
if (tx) await tx.rollback();
if (fs.existsSync(srcFile))
await fs.unlink(srcFile);
throw e;
}
};
};

View File

@ -0,0 +1,11 @@
{
"MdbBranch": {
"dataSource": "vn"
},
"MdbVersion": {
"dataSource": "vn"
},
"AccessContainer": {
"dataSource": "accessStorage"
}
}

View File

@ -0,0 +1,10 @@
{
"name": "AccessContainer",
"base": "Container",
"acls": [{
"accessType": "*",
"principalType": "ROLE",
"principalId": "developer",
"permission": "ALLOW"
}]
}

View File

@ -0,0 +1,16 @@
{
"name": "MdbBranch",
"base": "VnModel",
"options": {
"mysql": {
"table": "mdbBranch"
}
},
"properties": {
"name": {
"id": true,
"type": "string",
"description": "Identifier"
}
}
}

View File

@ -0,0 +1,3 @@
module.exports = Self => {
require('../methods/mdbVersion/upload')(Self);
};

View File

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

1
storage/access/.keep Normal file
View File

@ -0,0 +1 @@
Forces tmp folder creation!