From 6c88f42ba157a357d0e446ab299136f538c3d490 Mon Sep 17 00:00:00 2001 From: Marco Ambrosini Date: Mon, 10 Aug 2020 17:16:46 +0200 Subject: [PATCH] Use unique random indexes for temporary messages Signed-off-by: Marco Ambrosini --- src/store/fileUploadStore.js | 47 ++++++++++++++--------------------- src/utils/temporaryMessage.js | 5 ++-- 2 files changed, 21 insertions(+), 31 deletions(-) diff --git a/src/store/fileUploadStore.js b/src/store/fileUploadStore.js index 01d0cdab4fa..ed6773d2352 100644 --- a/src/store/fileUploadStore.js +++ b/src/store/fileUploadStore.js @@ -94,14 +94,12 @@ const getters = { } const mutations = { - /** - * Adds a "file to be shared to the store" - * @param {object} state the state object - * @param {object} file the file to be added to the store - * @param {number} uploadId The unique identifier of the upload operation - * @param {string} token the conversation's token - */ - addFileToBeUploaded(state, { uploadId, token, file }) { + + // Adds a "file to be shared to the store" + addFileToBeUploaded(state, { file, temporaryMessage }) { + const uploadId = temporaryMessage.messageParameters.file.uploadId + const token = temporaryMessage.messageParameters.file.token + const index = temporaryMessage.messageParameters.file.index // Create upload id if not present if (!state.uploads[uploadId]) { Vue.set(state.uploads, uploadId, { @@ -109,11 +107,12 @@ const mutations = { files: {}, }) } - Vue.set(state.uploads[uploadId].files, Object.keys(state.uploads[uploadId].files).length, { + Vue.set(state.uploads[uploadId].files, index, { file, - status: 'toBeUploaded', + status: 'initialised', totalSize: file.size, uploadedSize: 0, + temporaryMessage, }) }, @@ -128,11 +127,6 @@ const mutations = { Vue.set(state.uploads[uploadId].files[index], 'sharePath', sharePath) }, - // Marks a given file as initialised - markFileAsInitialised(state, { uploadId, index }) { - state.uploads[uploadId].files[index].status = 'initialised' - }, - // Marks a given file as uploading markFileAsUploading(state, { uploadId, index }) { state.uploads[uploadId].files[index].status = 'uploading' @@ -196,23 +190,18 @@ const actions = { commit('setCurrentUploadId', uploadId) // Show upload editor commit('showUploadEditor', true) - files.forEach(file => { - commit('addFileToBeUploaded', { uploadId, token, file }) - }) - // Add temporary messages - for (const index in state.uploads[uploadId].files) { - // Mark file as initialised - commit('markFileAsInitialised', { uploadId, index }) - // currentFile to be uploaded - const currentFile = state.uploads[uploadId].files[index].file + files.forEach(file => { // Get localurl for previews - const localUrl = URL.createObjectURL(currentFile) + const localUrl = URL.createObjectURL(file) + // Create a unique index for each file + const date = new Date() + const index = 'temp_' + date.getTime() + Math.random() // Create temporary message for the file and add it to the message list - const temporaryMessage = createTemporaryMessage('{file}', token, uploadId, index, currentFile, localUrl) - // Add the temporary messages to the store - commit('setTemporaryMessageForFile', { uploadId, index, temporaryMessage }) - } + const temporaryMessage = createTemporaryMessage('{file}', token, uploadId, index, file, localUrl) + console.debug('temporarymessage: ', temporaryMessage, 'uploadId', uploadId) + commit('addFileToBeUploaded', { file, temporaryMessage }) + }) }, /** diff --git a/src/utils/temporaryMessage.js b/src/utils/temporaryMessage.js index 86e219d3309..39a54624de0 100644 --- a/src/utils/temporaryMessage.js +++ b/src/utils/temporaryMessage.js @@ -30,16 +30,17 @@ const createTemporaryMessage = (text, token, uploadId, index, file, localUrl) => let tempId = 'temp-' + date.getTime() const messageParameters = {} if (file) { - tempId += '-' + uploadId + '-' + index + tempId += '-' + uploadId + '-' + Math.random() messageParameters.file = { 'type': 'file', 'file': file, 'mimetype': file.type, 'id': tempId, 'name': file.name, - index, + // index, will be the id from now on uploadId, localUrl, + index, } } const message = Object.assign({}, {