diff --git a/back/methods/dms/downloadFile.js b/back/methods/dms/downloadFile.js index 47db5c5a2..3174a19fd 100644 --- a/back/methods/dms/downloadFile.js +++ b/back/methods/dms/downloadFile.js @@ -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}"`]; }; }; diff --git a/back/methods/dms/updateFile.js b/back/methods/dms/updateFile.js index eaafd7f7d..ff3c0806f 100644 --- a/back/methods/dms/updateFile.js +++ b/back/methods/dms/updateFile.js @@ -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 diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 256b51205..94c425279 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -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" } \ No newline at end of file diff --git a/modules/client/front/dms/create/index.html b/modules/client/front/dms/create/index.html index 879210907..6010fb4a9 100644 --- a/modules/client/front/dms/create/index.html +++ b/modules/client/front/dms/create/index.html @@ -51,7 +51,7 @@ diff --git a/modules/client/front/dms/create/index.js b/modules/client/front/dms/create/index.js index a3b32228f..f3738fc2b 100644 --- a/modules/client/front/dms/create/index.js +++ b/modules/client/front/dms/create/index.js @@ -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; + }); } } diff --git a/modules/client/front/dms/create/locale/es.yml b/modules/client/front/dms/create/locale/es.yml index 2de9e68d3..2ea3b31d8 100644 --- a/modules/client/front/dms/create/locale/es.yml +++ b/modules/client/front/dms/create/locale/es.yml @@ -2,4 +2,4 @@ Upload file: Subir fichero Upload: Subir File: Fichero ClientFileDescription: "{{dmsTypeName}} del cliente {{clientName}} id {{clientId}}" -Attached file: Fichero adjunto \ No newline at end of file +Generate identifier for original file: Generar identificador para archivo original \ No newline at end of file diff --git a/modules/client/front/dms/edit/index.html b/modules/client/front/dms/edit/index.html index 6f6392d66..e3dccf1c0 100644 --- a/modules/client/front/dms/edit/index.html +++ b/modules/client/front/dms/edit/index.html @@ -50,7 +50,7 @@ diff --git a/modules/client/front/dms/edit/index.js b/modules/client/front/dms/edit/index.js index ba0108e6b..00e3e4d28 100644 --- a/modules/client/front/dms/edit/index.js +++ b/modules/client/front/dms/edit/index.js @@ -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; + }); } } diff --git a/modules/client/front/dms/edit/locale/es.yml b/modules/client/front/dms/edit/locale/es.yml index 7625d5195..9d97564ba 100644 --- a/modules/client/front/dms/edit/locale/es.yml +++ b/modules/client/front/dms/edit/locale/es.yml @@ -1,3 +1,3 @@ Edit file: Editar fichero File: Fichero -Attached file: Fichero adjunto \ No newline at end of file +Generate identifier for original file: Generar identificador para archivo original \ No newline at end of file diff --git a/modules/client/front/dms/index/index.html b/modules/client/front/dms/index/index.html index cc237c157..26a36d086 100644 --- a/modules/client/front/dms/index/index.html +++ b/modules/client/front/dms/index/index.html @@ -17,7 +17,7 @@ Reference Description Original - File + File Employee Created @@ -46,7 +46,12 @@ field="document.dms.hasFile"> - {{::document.dms.file}} + + {{::document.dms.file}} + + diff --git a/modules/ticket/front/dms/create/index.html b/modules/ticket/front/dms/create/index.html index d6ebb0130..7c8ae6ad2 100644 --- a/modules/ticket/front/dms/create/index.html +++ b/modules/ticket/front/dms/create/index.html @@ -50,7 +50,7 @@ diff --git a/modules/ticket/front/dms/create/index.js b/modules/ticket/front/dms/create/index.js index 1b28b2417..8f6f7e322 100644 --- a/modules/ticket/front/dms/create/index.js +++ b/modules/ticket/front/dms/create/index.js @@ -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; + }); } } diff --git a/modules/ticket/front/dms/create/locale/es.yml b/modules/ticket/front/dms/create/locale/es.yml index e074da48b..999826352 100644 --- a/modules/ticket/front/dms/create/locale/es.yml +++ b/modules/ticket/front/dms/create/locale/es.yml @@ -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 \ No newline at end of file +Generate identifier for original file: Generar identificador para archivo original \ No newline at end of file diff --git a/modules/ticket/front/dms/edit/index.html b/modules/ticket/front/dms/edit/index.html index c740d5103..ac12e4984 100644 --- a/modules/ticket/front/dms/edit/index.html +++ b/modules/ticket/front/dms/edit/index.html @@ -50,7 +50,7 @@ diff --git a/modules/ticket/front/dms/edit/index.js b/modules/ticket/front/dms/edit/index.js index c35c4140d..df25cea94 100644 --- a/modules/ticket/front/dms/edit/index.js +++ b/modules/ticket/front/dms/edit/index.js @@ -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; + }); } } diff --git a/modules/ticket/front/dms/edit/locale/es.yml b/modules/ticket/front/dms/edit/locale/es.yml index 7625d5195..9d97564ba 100644 --- a/modules/ticket/front/dms/edit/locale/es.yml +++ b/modules/ticket/front/dms/edit/locale/es.yml @@ -1,3 +1,3 @@ Edit file: Editar fichero File: Fichero -Attached file: Fichero adjunto \ No newline at end of file +Generate identifier for original file: Generar identificador para archivo original \ No newline at end of file diff --git a/modules/ticket/front/dms/index/index.html b/modules/ticket/front/dms/index/index.html index 9e4beb58d..222a14b04 100644 --- a/modules/ticket/front/dms/index/index.html +++ b/modules/ticket/front/dms/index/index.html @@ -17,7 +17,7 @@ Reference Description Original - File + File Employee Created @@ -46,7 +46,13 @@ field="document.dms.hasFile"> - {{::document.dms.file}} + + + {{::document.dms.file}} + +