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 committed Aug 10, 2020
1 parent 4af73ed commit b017977
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,6 @@ export default {
type: Number,
default: null,
},
// In case this component is used to display a file that is being uploaded
// this parameter is used to access the file upload status in the store
index: {
type: String,
default: '',
},

// True if this component is used in the upload editor
isUploadEditor: {
type: Boolean,
Expand All @@ -115,6 +108,11 @@ export default {
type: String,
default: '',
},

index: {
type: String,
default: '',
},
},
data() {
return {
Expand Down Expand Up @@ -211,6 +209,7 @@ export default {
return this.mimetype.startsWith('image/') && this.localUrl
},
},

mounted() {
const img = new Image()
img.onerror = () => {
Expand Down
49 changes: 18 additions & 31 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,25 +190,19 @@ 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 })
})
},

/**
* Uploads the files to the root directory of the user
* @param {object} param0 Commit, state and getters
Expand Down Expand Up @@ -266,7 +254,6 @@ const actions = {
const shareableFiles = getters.getShareableFiles(uploadId)
// Share each of those files to the conversation
for (const index in shareableFiles) {
console.debug('i run')
const path = shareableFiles[index].sharePath
try {
const temporaryMessage = shareableFiles[index].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 b017977

Please sign in to comment.