1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
/**
* @copyright Copyright (c) 2019 Marco Ambrosini <marcoambrosini@pm.me>
*
* @author Marco Ambrosini <marcoambrosini@pm.me>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import Vue from 'vue'
import client from '../services/DavClient'
import { showError } from '@nextcloud/dialogs'
import { loadState } from '@nextcloud/initial-state'
import { findUniquePath } from '../utils/fileUpload'
import createTemporaryMessage from '../utils/temporaryMessage'
import { EventBus } from '../services/EventBus'
import { shareFile } from '../services/filesSharingServices'
const state = {
attachmentFolder: loadState('spreed', 'attachment_folder'),
attachmentFolderFreeSpace: loadState('spreed', 'attachment_folder_free_space'),
uploads: {
},
currentUploadId: undefined,
showUploadEditor: false,
}
const getters = {
getInitialisedUploads: (state) => (uploadId) => {
if (state.uploads[uploadId]) {
const initialisedUploads = {}
for (const index in state.uploads[uploadId].files) {
const currentFile = state.uploads[uploadId].files[index]
if (currentFile.status === 'initialised') {
initialisedUploads[index] = (currentFile)
}
}
return initialisedUploads
} else {
return {}
}
},
// Returns all the files that have been successfully uploaded provided an
// upload id
getShareableFiles: (state) => (uploadId) => {
if (state.uploads[uploadId]) {
const shareableFiles = {}
for (const index in state.uploads[uploadId].files) {
const currentFile = state.uploads[uploadId].files[index]
if (currentFile.status === 'successUpload') {
shareableFiles[index] = (currentFile)
}
}
return shareableFiles
} else {
return {}
}
},
// gets the current attachment folder
getAttachmentFolder: (state) => () => {
return state.attachmentFolder
},
// gets the current attachment folder
getAttachmentFolderFreeSpace: (state) => () => {
return state.attachmentFolderFreeSpace
},
uploadProgress: (state) => (uploadId, index) => {
if (state.uploads[uploadId].files[index]) {
return state.uploads[uploadId].files[index].uploadedSize / state.uploads[uploadId].files[index].totalSize * 100
} else {
return 0
}
},
currentUploadId: (state) => {
return state.currentUploadId
},
showUploadEditor: (state) => {
return state.showUploadEditor
},
}
const mutations = {
// 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, index, {
file,
status: 'initialised',
totalSize: file.size,
uploadedSize: 0,
temporaryMessage,
})
},
// Marks a given file as failed upload
markFileAsFailedUpload(state, { uploadId, index, status }) {
state.uploads[uploadId].files[index].status = 'failedUpload'
},
// Marks a given file as uploaded
markFileAsSuccessUpload(state, { uploadId, index, sharePath }) {
state.uploads[uploadId].files[index].status = 'successUpload'
Vue.set(state.uploads[uploadId].files[index], 'sharePath', sharePath)
},
// Marks a given file as uploading
markFileAsUploading(state, { uploadId, index }) {
state.uploads[uploadId].files[index].status = 'uploading'
},
// Marks a given file as sharing
markFileAsSharing(state, { uploadId, index }) {
state.uploads[uploadId].files[index].status = 'sharing'
},
// Marks a given file as shared
markFileAsShared(state, { uploadId, index }) {
state.uploads[uploadId].files[index].status = 'shared'
},
/**
* Set the attachmentFolder
*
* @param {object} state current store state;
* @param {string} attachmentFolder The new target location for attachments
*/
setAttachmentFolder(state, attachmentFolder) {
state.attachmentFolder = attachmentFolder
},
// Sets uploaded amount of bytes
setUploadedSize(state, { uploadId, index, uploadedSize }) {
state.uploads[uploadId].files[index].uploadedSize = uploadedSize
},
// Set temporary message for each file
setTemporaryMessageForFile(state, { uploadId, index, temporaryMessage }) {
console.debug('uploadId: ' + uploadId + ' index: ' + index)
Vue.set(state.uploads[uploadId].files[index], 'temporaryMessage', temporaryMessage)
},
// Sets the id of the current upload operation
setCurrentUploadId(state, currentUploadId) {
state.currentUploadId = currentUploadId
},
// Shows hides the upload editor
showUploadEditor(state, show) {
state.showUploadEditor = show
},
removeFileFromSelection(state, fileId) {
const uploadId = state.currentUploadId
for (const key in state.uploads[uploadId].files) {
if (state.uploads[uploadId].files[key].temporaryMessage.id === fileId) {
Vue.delete(state.uploads[uploadId].files, key)
}
}
},
discardUpload(state, uploadId) {
Vue.delete(state.uploads, uploadId)
},
}
const actions = {
initialiseUpload({ commit, dispatch }, { uploadId, token, files }) {
// Set last upload id
commit('setCurrentUploadId', uploadId)
// Show upload editor
commit('showUploadEditor', true)
files.forEach(file => {
// Get localurl for previews
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, file, localUrl)
console.debug('temporarymessage: ', temporaryMessage, 'uploadId', uploadId)
commit('addFileToBeUploaded', { file, temporaryMessage })
})
},
/**
* Discards an upload
* @param {object} param0 Commit and state
* @param {object} uploadId The unique uploadId
*/
discardUpload({ commit, state, getters }, uploadId) {
commit('discardUpload', { uploadId })
},
/**
* Uploads the files to the root directory of the user
* @param {object} param0 Commit, state and getters
* @param {object} uploadId The unique uploadId
*/
async uploadFiles({ commit, dispatch, state, getters }, uploadId) {
EventBus.$emit('uploadStart')
// Tag the previously indexed files and add the temporary messages to the
// messages list
for (const index in state.uploads[uploadId].files) {
// mark all files as uploading
commit('markFileAsUploading', { uploadId, index })
// Store the previously created temporary message
const temporaryMessage = state.uploads[uploadId].files[index].temporaryMessage
// Add temporary messages (files) to the messages list
dispatch('addTemporaryMessage', temporaryMessage)
// Scroll the message list
EventBus.$emit('scrollChatToBottom')
}
// Iterate again and perform the uploads
for (const index in state.uploads[uploadId].files) {
// currentFile to be uploaded
const currentFile = state.uploads[uploadId].files[index].file
// userRoot path
const userRoot = '/files/' + getters.getUserId()
const fileName = (currentFile.newName || currentFile.name)
// Candidate rest of the path
const path = getters.getAttachmentFolder() + '/' + fileName
// Get a unique relative path based on the previous path variable
const uniquePath = await findUniquePath(client, userRoot, path)
try {
// Upload the file
await client.putFileContents(userRoot + uniquePath, currentFile, { onUploadProgress: progress => {
const uploadedSize = progress.loaded
commit('setUploadedSize', { state, uploadId, index, uploadedSize })
} })
// Path for the sharing request
const sharePath = '/' + uniquePath
// Mark the file as uploaded in the store
commit('markFileAsSuccessUpload', { uploadId, index, sharePath })
} catch (exception) {
console.error(`Error while uploading file "${fileName}":` + exception, fileName, exception.response.status)
const temporaryMessage = state.uploads[uploadId].files[index].temporaryMessage
let reason = 'failed-upload'
if (exception.response.status === 507) {
reason = 'quota'
showError(t('spreed', 'Not enough free space to upload file "{fileName}"', { fileName }))
} else {
showError(t('spreed', 'Error while uploading file "{fileName}"', { fileName }))
}
// Mark the upload as failed in the store
commit('markFileAsFailedUpload', { uploadId, index })
dispatch('markTemporaryMessageAsFailed', {
message: temporaryMessage,
reason: reason,
})
}
// Get the files that have successfully been uploaded from the store
const shareableFiles = getters.getShareableFiles(uploadId)
// Share each of those files to the conversation
for (const index in shareableFiles) {
const path = shareableFiles[index].sharePath
try {
const temporaryMessage = shareableFiles[index].temporaryMessage
const token = temporaryMessage.token
dispatch('markFileAsSharing', { uploadId, index })
await shareFile(path, token, temporaryMessage.referenceId)
dispatch('markFileAsShared', { uploadId, index })
} catch (exception) {
console.debug('An error happened when trying to share your file: ', exception)
}
}
}
EventBus.$emit('uploadFinished')
},
/**
* Set the folder to store new attachments in
*
* @param {object} context default store context;
* @param {string} attachmentFolder Folder to store new attachments in
*/
setAttachmentFolder(context, attachmentFolder) {
context.commit('setAttachmentFolder', attachmentFolder)
},
/**
* Mark a file as shared
* @param {object} context default store context;
* @param {object} param1 The unique upload id original file index
* @throws {Error} when the item is already being shared by another async call
*/
markFileAsSharing({ commit, state }, { uploadId, index }) {
if (state.uploads[uploadId].files[index].status !== 'successUpload') {
throw new Error('Item is already being shared')
}
commit('markFileAsSharing', { uploadId, index })
},
/**
* Mark a file as shared
* @param {object} context default store context;
* @param {object} param1 The unique upload id original file index
*/
markFileAsShared(context, { uploadId, index }) {
context.commit('markFileAsShared', { uploadId, index })
},
removeFileFromSelection({ commit }, fileId) {
commit('removeFileFromSelection', fileId)
},
}
export default { state, mutations, getters, actions }
|