merge with dev
gitea/salix/test This commit looks good Details

This commit is contained in:
Joan Sanchez 2019-07-16 13:41:34 +02:00
commit 89a3c399c1
20 changed files with 141 additions and 118 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: 'application/octet-stream',
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,73 +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.name, 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, fileName, 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,
hasFile: hasFile
}, myOptions);
const models = Self.app.models;
const fileOptions = {};
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

@ -70,7 +70,7 @@ module.exports = Self => {
const addedDms = [];
for (const file of files) {
const newDms = await createDms(ctx, file.name, myOptions);
const newDms = await createDms(ctx, file, myOptions);
const pathHash = storageConnector.getPathHash(newDms.id);
const container = await getContainer(pathHash);
@ -90,7 +90,7 @@ module.exports = Self => {
}
};
async function createDms(ctx, fileName, myOptions) {
async function createDms(ctx, file, myOptions) {
const models = Self.app.models;
const storageConnector = Self.app.dataSources.storage.connector;
const myUserId = ctx.req.accessToken.userId;
@ -104,9 +104,11 @@ module.exports = Self => {
warehouseFk: args.warehouseId,
reference: args.reference,
description: args.description,
contentType: file.type,
hasFile: args.hasFile
}, myOptions);
let fileName = file.name;
const extension = storageConnector.getFileExtension(fileName);
fileName = `${newDms.id}.${extension}`;

View File

@ -17,6 +17,9 @@
"file": {
"type": "string"
},
"contentType": {
"type": "string"
},
"reference": {
"type": "string"
},

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

@ -42,7 +42,7 @@
<vn-horizontal>
<vn-autocomplete vn-one
field="$ctrl.address.postalCode"
on-change="$ctrl.setLocation()"
selection="$ctrl.postcodeSelection"
search-function="{code: $search}"
url="/api/Postcodes/location"
fields="['code', 'townFk']"

View File

@ -46,12 +46,12 @@
label="File"
model="$ctrl.dms.files"
on-change="$ctrl.onFileChange(files)"
accept=".pdf, .png, .jpg, .jpeg, application/zip, application/rar">
accept=".pdf, .png, .jpg, .jpeg, application/zip, application/rar, application/x-7z-compressed">
</vn-input-file>
</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

@ -45,12 +45,12 @@
label="File"
model="$ctrl.dms.files"
on-change="$ctrl.onFileChange(files)"
accept=".pdf, .png, .jpg, .jpeg, application/zip, application/rar">
accept=".pdf, .png, .jpg, .jpeg, application/zip, application/rar, application/x-7z-compressed">
</vn-input-file>
</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

@ -16,8 +16,8 @@
<vn-th field="dmsTypeFk" shrink>Type</vn-th>
<vn-th field="reference">Reference</vn-th>
<vn-th>Description</vn-th>
<vn-th field="hasFile" center>Original</vn-th>
<vn-th shrink>File</vn-th>
<vn-th field="hasFile" shrink>Original</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>
@ -41,12 +41,17 @@
{{::document.dms.description}}
</span>
</vn-td>
<vn-td shrink center>
<vn-td shrink>
<vn-check disabled="true"
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

@ -45,12 +45,12 @@
label="File"
model="$ctrl.dms.files"
on-change="$ctrl.onFileChange(files)"
accept=".pdf, .png, .jpg, .jpeg, application/zip, application/rar">
accept=".pdf, .png, .jpg, .jpeg, application/zip, application/rar, application/x-7z-compressed">
</vn-input-file>
</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

@ -45,12 +45,12 @@
label="File"
model="$ctrl.dms.files"
on-change="$ctrl.onFileChange(files)"
accept=".pdf, .png, .jpg, .jpeg, application/zip, application/rar">
accept=".pdf, .png, .jpg, .jpeg, application/zip, application/rar, application/x-7z-compressed">
</vn-input-file>
</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

@ -16,11 +16,11 @@
<vn-th field="dmsTypeFk" shrink>Type</vn-th>
<vn-th field="reference">Reference</vn-th>
<vn-th>Description</vn-th>
<vn-th field="hasFile" center>Attached file</vn-th>
<vn-th shrink>File</vn-th>
<vn-th>Employee</vn-th>
<vn-th field="hasFile" shrink>Original</vn-th>
<vn-th>File</vn-th>
<vn-th shrink>Employee</vn-th>
<vn-th field="created">Created</vn-th>
<vn-th></vn-th>
<vn-th number></vn-th>
</vn-tr>
</vn-thead>
<vn-tbody>
@ -41,13 +41,19 @@
{{::document.dms.description}}
</span>
</vn-td>
<vn-td center>
<vn-td shrink>
<vn-check disabled="true"
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)">
{{::document.dms.worker.user.nickname | dashIfEmpty}}
@ -55,7 +61,7 @@
<vn-td>
{{::document.dms.created | dateTime:'dd/MM/yyyy HH:mm'}}
</vn-td>
<vn-td center shrink>
<vn-td number>
<a target="_blank"
vn-tooltip="Download file"
href="api/dms/{{::document.dmsFk}}/downloadFile?access_token={{::$ctrl.accessToken}}">