From bb62e55ef23b73ca86d6b8b5bd1a6a3c67a9b0eb Mon Sep 17 00:00:00 2001 From: urielfcampos Date: Mon, 3 Jun 2019 16:01:58 -0300 Subject: [PATCH] =?UTF-8?q?Adicionado=20valida=C3=A7ao=20de=20tamanho=20de?= =?UTF-8?q?=20arquivo=20ao=20upload?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/importStudents.vue | 8 ++++++++ components/mixins/errors.js | 2 ++ components/studentComboBox.vue | 7 +++++++ nuxt.config.js | 5 +++-- shared/config.js | 2 ++ shared/errors.js | 1 + 6 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 shared/config.js diff --git a/components/importStudents.vue b/components/importStudents.vue index 49f7a098..483decc1 100644 --- a/components/importStudents.vue +++ b/components/importStudents.vue @@ -80,13 +80,21 @@ export default { return this.studentsCsv.name !== 'Nenhum arquivo selecionado' }, validateUpload() { + const { config } = process.env if (!this.studentsCsv) { return } + if (this.studentsCsv.size >= config.MAX_FILE_SIZE) { + this.openErrorNotification(process.env.errors.MAX_FILE_SIZE_EXCEEDED) + this.studentsCsv = new File([''], 'Nenhum arquivo selecionado') + this.hasErrors = true + return + } if (this.studentsCsv.name.split('.').pop() === 'csv') { const reader = new FileReader() reader.readAsText(this.studentsCsv) reader.onload = e => { + debugger const validation = this.validateCsv(reader.result) if (typeof validation === 'string') { this.openErrorNotification(validation) diff --git a/components/mixins/errors.js b/components/mixins/errors.js index 21b44ecd..87aee0ac 100644 --- a/components/mixins/errors.js +++ b/components/mixins/errors.js @@ -13,6 +13,8 @@ export const errorsHandler = { return 'Por favor selecione um arquivo do tipo csv' case errors.IMPORT_CSV_REGISTRATION_NUMBER_REPEATED: return 'Arquivo csv com nĂºmeros de matrĂ­cula repetidos' + case errors.MAX_FILE_SIZE_EXCEEDED: + return 'Arquivo com tamanho superior a 6MB' default: return 'Ocorreu um erro' } diff --git a/components/studentComboBox.vue b/components/studentComboBox.vue index 674c0a09..d75462d3 100644 --- a/components/studentComboBox.vue +++ b/components/studentComboBox.vue @@ -413,9 +413,16 @@ export default { }) }, validateUpload(type) { + const { config } = process.env if (!this.uploadFile) { return } + if (this.uploadFile.size >= config.MAX_FILE_SIZE) { + this.openErrorNotification(process.env.errors.MAX_FILE_SIZE_EXCEEDED) + this.uploadFile = new File([''], 'Nenhum arquivo selecionado') + this.hasErrors = true + return + } const isPDF = this.uploadFile.name.split('.').pop() === 'pdf' if (isPDF) { return this.documentUpload(type) diff --git a/nuxt.config.js b/nuxt.config.js index e7be15c3..606c7d6a 100644 --- a/nuxt.config.js +++ b/nuxt.config.js @@ -1,7 +1,7 @@ const pkg = require('./package') const errors = require('./shared/errors') const enums = require('./shared/enums') - +const config = require('./shared/config') module.exports = { mode: 'spa', @@ -65,7 +65,8 @@ module.exports = { }, env: { errors, - enums + enums, + config }, /* diff --git a/shared/config.js b/shared/config.js new file mode 100644 index 00000000..e176238a --- /dev/null +++ b/shared/config.js @@ -0,0 +1,2 @@ +// Size in Bytes +exports.MAX_FILE_SIZE = 6291456 diff --git a/shared/errors.js b/shared/errors.js index de0ab5d4..6010c3b1 100644 --- a/shared/errors.js +++ b/shared/errors.js @@ -13,6 +13,7 @@ exports.INVALID_PARAMS = 'INVALID_PARAMS' exports.NOT_FOUND = 'NOT_FOUND' exports.UPLOAD_FILE_FIELD_MISSING = 'UPLOAD_FILE_FIELD_MISSING' exports.UPLOAD_FILE_TYPE_INVALID = 'UPLOAD_FILE_TYPE_INVALID' +exports.MAX_FILE_SIZE_EXCEEDED = 'MAX_FILE_SIZE_EXCEEDED' exports.NOT_FOUND_ROUTE = 'NOT_FOUND_ROUTE' exports.INVALID_SORT_ARGUMENT = 'INVALID SORT ARGUMENT' exports.INVALID_ORDER_ARGUMENT = 'INVALID ORDER ARGUMENT'