Skip to content

Commit

Permalink
Use unique random indexes for temporary messages
Browse files Browse the repository at this point in the history
Signed-off-by: Marco Ambrosini <marcoambrosini@pm.me>
  • Loading branch information
marcoambrosini authored and danxuliu committed Aug 12, 2020
1 parent 5c390a8 commit 6c88f42
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 31 deletions.
47 changes: 18 additions & 29 deletions src/store/fileUploadStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,25 @@ 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, {
token,
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,
})
},

Expand All @@ -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'
Expand Down Expand Up @@ -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 })
})
},

/**
Expand Down
5 changes: 3 additions & 2 deletions src/utils/temporaryMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({}, {
Expand Down

0 comments on commit 6c88f42

Please sign in to comment.