dms fixes

This commit is contained in:
Joan Sanchez 2019-07-16 13:37:25 +02:00
parent 17087c8d14
commit 3d52c83d39
17 changed files with 122 additions and 106 deletions

View File

@ -34,7 +34,6 @@ module.exports = Self => {
});
Self.downloadFile = async function(ctx, id) {
const env = process.env.NODE_ENV;
const storageConnector = Self.app.dataSources.storage.connector;
const models = Self.app.models;
const dms = await Self.findById(id);
@ -43,23 +42,21 @@ module.exports = Self => {
if (!hasReadRole)
throw new UserError(`You don't have enough privileges`);
if (env && env != 'development') {
const pathHash = storageConnector.getPathHash(dms.id);
file = {
contentType: dms.contentType,
container: pathHash,
name: dms.file
};
} else {
file = {
contentType: 'text/plain',
container: 'temp',
name: `file.txt`
};
const pathHash = storageConnector.getPathHash(dms.id);
try {
await models.Container.getFile(pathHash, dms.file);
} catch (e) {
if (e.code != 'ENOENT')
throw e;
const error = new UserError(`File doesn't exists`);
error.statusCode = 404;
throw error;
}
const stream = await models.Container.downloadStream(file.container, file.name);
const stream = models.Container.downloadStream(pathHash, dms.file);
return [stream, file.contentType, `filename="${file.name}"`];
return [stream, dms.contentType, `filename="${dms.file}"`];
};
};

View File

@ -32,6 +32,10 @@ module.exports = Self => {
}, {
arg: 'hasFile',
type: 'Boolean',
description: 'True if has original file'
}, {
arg: 'hasAttachedFile',
type: 'Boolean',
description: 'True if has an attached file'
}],
returns: {
@ -45,10 +49,8 @@ module.exports = Self => {
});
Self.updateFile = async(ctx, id, warehouseId, companyId,
dmsTypeId, reference, description, hasFile, options) => {
const storageConnector = Self.app.dataSources.storage.connector;
dmsTypeId, reference, description, hasFile, hasAttachedFile, options) => {
const models = Self.app.models;
const fileOptions = {};
let tx;
let myOptions = {};
@ -66,75 +68,68 @@ module.exports = Self => {
if (!hasWriteRole)
throw new UserError(`You don't have enough privileges`);
// Upload file to temporary path
const tempContainer = await getContainer('temp');
let files = [];
try {
const uploaded = await models.Container.upload(tempContainer.name, ctx.req, ctx.result, fileOptions);
files = Object.values(uploaded.files).map(file => {
return file[0];
});
} catch (err) {
if (err.message != 'No file content uploaded')
throw e;
}
const dms = await Self.findById(id, null, myOptions);
await dms.updateAttributes({
dmsTypeFk: dmsTypeId,
companyFk: companyId,
warehouseFk: warehouseId,
reference: reference,
description: description,
hasFile: hasFile
}, myOptions);
const updatedDmsList = [];
for (const file of files) {
const updatedDms = await updateDms(id, dmsTypeId, companyId, warehouseId,
reference, description, hasFile, file, myOptions);
const pathHash = storageConnector.getPathHash(updatedDms.id);
const container = await getContainer(pathHash);
const originPath = `${tempContainer.client.root}/${tempContainer.name}/${file.name}`;
const destinationPath = `${container.client.root}/${pathHash}/${updatedDms.file}`;
fs.rename(originPath, destinationPath);
updatedDmsList.push(updatedDms);
}
if (hasAttachedFile)
updatedDms = await uploadNewFile(ctx, dms, myOptions);
if (tx) await tx.commit();
return updatedDmsList;
return updatedDms;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
async function updateDms(id, dmsTypeId, companyId, warehouseId,
reference, description, hasFile, file, myOptions) {
async function uploadNewFile(ctx, dms, myOptions) {
const storageConnector = Self.app.dataSources.storage.connector;
const dms = await Self.findById(id, null, myOptions);
const updatedDms = await dms.updateAttributes({
dmsTypeFk: dmsTypeId,
companyFk: companyId,
warehouseFk: warehouseId,
reference: reference,
description: description,
contentType: file.type,
hasFile: hasFile
}, myOptions);
const models = Self.app.models;
const fileOptions = {};
let fileName = file.name;
const oldExtension = storageConnector.getFileExtension(dms.file);
const newExtension = storageConnector.getFileExtension(fileName);
const tempContainer = await getContainer('temp');
const makeUpload = await models.Container.upload(tempContainer.name, ctx.req, ctx.result, fileOptions);
const keys = Object.values(makeUpload.files);
const files = keys.map(file => file[0]);
const file = files[0];
try {
if (oldExtension != newExtension) {
const pathHash = storageConnector.getPathHash(updatedDms.id);
if (file) {
const oldExtension = storageConnector.getFileExtension(dms.file);
const newExtension = storageConnector.getFileExtension(file.name);
const fileName = `${dms.id}.${newExtension}`;
await Self.app.models.Container.removeFile(pathHash, dms.file);
}
} catch (err) {}
try {
if (oldExtension != newExtension) {
const pathHash = storageConnector.getPathHash(dms.id);
fileName = `${updatedDms.id}.${newExtension}`;
await models.Container.removeFile(pathHash, dms.file);
}
} catch (err) {}
return updatedDms.updateAttribute('file', fileName, myOptions);
const updatedDms = await dms.updateAttributes({
contentType: file.type,
file: fileName
}, myOptions);
const pathHash = storageConnector.getPathHash(updatedDms.id);
const container = await getContainer(pathHash);
const originPath = `${tempContainer.client.root}/${tempContainer.name}/${file.name}`;
const destinationPath = `${container.client.root}/${pathHash}/${updatedDms.file}`;
fs.rename(originPath, destinationPath);
return updatedDms;
}
}
/**
* Returns a container instance
* If doesn't exists creates a new one

View File

@ -94,5 +94,6 @@
"Invalid parameters to create a new ticket": "Parámetros inválidos para crear un nuevo ticket",
"This item is not available": "Este artículo no está disponible",
"This postcode already exists": "Este código postal ya existe",
"Concept cannot be blank": "Concept cannot be blank"
"Concept cannot be blank": "El concepto no puede quedar en blanco",
"File doesn't exists": "El archivo no existe"
}

View File

@ -51,7 +51,7 @@
</vn-horizontal>
<vn-vertical>
<vn-check
label="Attached file"
label="Generate identifier for original file"
field="$ctrl.dms.hasFile">
</vn-check>
</vn-vertical>

View File

@ -10,7 +10,8 @@ class Controller {
this.vnApp = vnApp;
this.dms = {
files: [],
hasFile: false
hasFile: false,
hasAttachedFile: false
};
}
@ -78,11 +79,13 @@ class Controller {
}
onFileChange(files) {
if (files.length > 0) {
this.$.$applyAsync(() => {
this.dms.hasFile = true;
});
}
let hasAttachedFile = false;
if (files.length > 0)
hasAttachedFile = true;
this.$.$applyAsync(() => {
this.dms.hasAttachedFile = hasAttachedFile;
});
}
}

View File

@ -2,4 +2,4 @@ Upload file: Subir fichero
Upload: Subir
File: Fichero
ClientFileDescription: "{{dmsTypeName}} del cliente {{clientName}} id {{clientId}}"
Attached file: Fichero adjunto
Generate identifier for original file: Generar identificador para archivo original

View File

@ -50,7 +50,7 @@
</vn-horizontal>
<vn-vertical>
<vn-check
label="Attached file"
label="Generate identifier for original file"
field="$ctrl.dms.hasFile">
</vn-check>
</vn-vertical>

View File

@ -33,6 +33,7 @@ class Controller {
dmsTypeId: dms.dmsTypeFk,
description: dms.description,
hasFile: dms.hasFile,
hasAttachedFile: false,
files: []
};
});
@ -67,11 +68,13 @@ class Controller {
}
onFileChange(files) {
if (files.length > 0) {
this.$.$applyAsync(() => {
this.dms.hasFile = true;
});
}
let hasAttachedFile = false;
if (files.length > 0)
hasAttachedFile = true;
this.$.$applyAsync(() => {
this.dms.hasAttachedFile = hasAttachedFile;
});
}
}

View File

@ -1,3 +1,3 @@
Edit file: Editar fichero
File: Fichero
Attached file: Fichero adjunto
Generate identifier for original file: Generar identificador para archivo original

View File

@ -17,7 +17,7 @@
<vn-th field="reference">Reference</vn-th>
<vn-th>Description</vn-th>
<vn-th field="hasFile" shrink>Original</vn-th>
<vn-th shrink>File</vn-th>
<vn-th>File</vn-th>
<vn-th shrink>Employee</vn-th>
<vn-th field="created">Created</vn-th>
<vn-th number></vn-th>
@ -46,7 +46,12 @@
field="document.dms.hasFile">
</vn-check>
</vn-td>
<vn-td shrink>{{::document.dms.file}}</vn-td>
<vn-td>
<a target="_blank"
vn-tooltip="Download file"
href="api/dms/{{::document.dmsFk}}/downloadFile?access_token={{::$ctrl.accessToken}}">{{::document.dms.file}}
</a>
</vn-td>
<vn-td shrink>
<span class="link"
ng-click="$ctrl.showWorkerDescriptor($event, document.dms.workerFk)">

View File

@ -50,7 +50,7 @@
</vn-horizontal>
<vn-vertical>
<vn-check
label="Attached file"
label="Generate identifier for original file"
field="$ctrl.dms.hasFile">
</vn-check>
</vn-vertical>

View File

@ -10,7 +10,8 @@ class Controller {
this.vnApp = vnApp;
this.dms = {
files: [],
hasFile: false
hasFile: false,
hasAttachedFile: false
};
}
@ -76,11 +77,13 @@ class Controller {
}
onFileChange(files) {
if (files.length > 0) {
this.$.$applyAsync(() => {
this.dms.hasFile = true;
});
}
let hasAttachedFile = false;
if (files.length > 0)
hasAttachedFile = true;
this.$.$applyAsync(() => {
this.dms.hasAttachedFile = hasAttachedFile;
});
}
}

View File

@ -2,4 +2,4 @@ Upload file: Subir fichero
Upload: Subir
File: Fichero
FileDescription: Ticket id {{ticketId}} del cliente {{clientName}} id {{clientId}}
Attached file: Fichero adjunto
Generate identifier for original file: Generar identificador para archivo original

View File

@ -50,7 +50,7 @@
</vn-horizontal>
<vn-vertical>
<vn-check
label="Attached file"
label="Generate identifier for original file"
field="$ctrl.dms.hasFile">
</vn-check>
</vn-vertical>

View File

@ -33,6 +33,7 @@ class Controller {
dmsTypeId: dms.dmsTypeFk,
description: dms.description,
hasFile: dms.hasFile,
hasAttachedFile: false,
files: []
};
});
@ -67,11 +68,13 @@ class Controller {
}
onFileChange(files) {
if (files.length > 0) {
this.$.$applyAsync(() => {
this.dms.hasFile = true;
});
}
let hasAttachedFile = false;
if (files.length > 0)
hasAttachedFile = true;
this.$.$applyAsync(() => {
this.dms.hasAttachedFile = hasAttachedFile;
});
}
}

View File

@ -1,3 +1,3 @@
Edit file: Editar fichero
File: Fichero
Attached file: Fichero adjunto
Generate identifier for original file: Generar identificador para archivo original

View File

@ -17,7 +17,7 @@
<vn-th field="reference">Reference</vn-th>
<vn-th>Description</vn-th>
<vn-th field="hasFile" shrink>Original</vn-th>
<vn-th shrink>File</vn-th>
<vn-th>File</vn-th>
<vn-th shrink>Employee</vn-th>
<vn-th field="created">Created</vn-th>
<vn-th number></vn-th>
@ -46,7 +46,13 @@
field="document.dms.hasFile">
</vn-check>
</vn-td>
<vn-td shrink>{{::document.dms.file}}</vn-td>
<vn-td>
<a target="_blank"
vn-tooltip="Download file"
href="api/dms/{{::document.dmsFk}}/downloadFile?access_token={{::$ctrl.accessToken}}">
{{::document.dms.file}}
</a>
</vn-td>
<vn-td shrink>
<span class="link"
ng-click="$ctrl.showWorkerDescriptor($event, document.dms.workerFk)">