Browse Source

Decuple fileUpload service from the global store

Moved the logic of "processFiles" into the existing "initialiseUpload"
action of the fileUploadStore

Signed-off-by: Vincent Petry <vincent@nextcloud.com>
pull/5531/head
Vincent Petry 5 years ago
parent
commit
b9632cf7a3
No known key found for this signature in database GPG Key ID: E055D6A4D513575C
  1. 3
      src/components/ChatView.vue
  2. 3
      src/components/NewMessageForm/NewMessageForm.vue
  3. 3
      src/components/UploadEditor.vue
  4. 21
      src/store/fileUploadStore.js
  5. 25
      src/utils/fileUpload.js

3
src/components/ChatView.vue

@ -59,7 +59,6 @@
<script>
import MessagesList from './MessagesList/MessagesList'
import NewMessageForm from './NewMessageForm/NewMessageForm'
import { processFiles } from '../utils/fileUpload'
import { CONVERSATION } from '../constants'
export default {
@ -140,7 +139,7 @@ export default {
// Create a unique id for the upload operation
const uploadId = new Date().getTime()
// Uploads and shares the files
processFiles(files, this.token, uploadId)
this.$store.dispatch('initialiseUpload', { files, token: this.token, uploadId })
},
setScrollStatus(payload) {

3
src/components/NewMessageForm/NewMessageForm.vue

@ -125,7 +125,6 @@ import ActionButton from '@nextcloud/vue/dist/Components/ActionButton'
import EmojiPicker from '@nextcloud/vue/dist/Components/EmojiPicker'
import { EventBus } from '../../services/EventBus'
import { shareFile } from '../../services/filesSharingServices'
import { processFiles } from '../../utils/fileUpload'
import { CONVERSATION } from '../../constants'
import createTemporaryMessage from '../../utils/temporaryMessage'
import EmoticonOutline from 'vue-material-design-icons/EmoticonOutline'
@ -457,7 +456,7 @@ export default {
// Create a unique id for the upload operation
const uploadId = new Date().getTime()
// Uploads and shares the files
await processFiles(files, this.token, uploadId, rename)
this.$store.dispatch('initialiseUpload', { files, token: this.token, uploadId, rename })
},
/**

3
src/components/UploadEditor.vue

@ -70,7 +70,6 @@
import Modal from '@nextcloud/vue/dist/Components/Modal'
import FilePreview from './MessagesList/MessagesGroup/Message/MessagePart/FilePreview.vue'
import Plus from 'vue-material-design-icons/Plus'
import { processFiles } from '../utils/fileUpload'
export default {
name: 'UploadEditor',
@ -138,7 +137,7 @@ export default {
handleFileInput(event) {
const files = Object.values(event.target.files)
processFiles(files, this.token, this.currentUploadId)
this.$store.dispatch('initialiseUpload', { files, token: this.token, uploadId: this.currentUploadId })
},
handleRemoveFileFromSelection(id) {

21
src/store/fileUploadStore.js

@ -24,7 +24,8 @@ import Vue from 'vue'
import client from '../services/DavClient'
import { showError } from '@nextcloud/dialogs'
import fromStateOr from './helper'
import { findUniquePath } from '../utils/fileUpload'
import { findUniquePath, getFileExtension } from '../utils/fileUpload'
import moment from '@nextcloud/moment'
import createTemporaryMessage from '../utils/temporaryMessage'
import { EventBus } from '../services/EventBus'
import { shareFile } from '../services/filesSharingServices'
@ -186,7 +187,23 @@ const mutations = {
const actions = {
initialiseUpload({ commit, dispatch }, { uploadId, token, files }) {
/**
* Initialises uploads and shares files to a conversation
*
* @param {object} files the files to be processed
* @param {string} token the conversation's token where to share the files
* @param {number} uploadId a unique id for the upload operation indexing
* @param {bool} rename whether to rename the files (usually after pasting)
*/
initialiseUpload({ commit, dispatch }, { uploadId, token, files, rename = false }) {
if (rename) {
files.forEach(file => {
// note: can't overwrite the original read-only name attribute
file.newName = moment(file.lastModified || file.lastModifiedDate).format('YYYYMMDD_HHmmss')
+ getFileExtension(file.name)
})
}
// Set last upload id
commit('setCurrentUploadId', uploadId)

25
src/utils/fileUpload.js

@ -20,9 +20,6 @@
*
*/
import store from '../store/index'
import moment from '@nextcloud/moment'
/**
* Returns the file extension for the given path
*
@ -71,27 +68,7 @@ const findUniquePath = async function(client, userRoot, path) {
}
}
/**
* Uploads and shares files to a conversation
* @param {object} files the files to be processed
* @param {string} token the conversation's token where to share the files
* @param {number} uploadId a unique id for the upload operation indexing
* @param {bool} rename whether to rename the files (usually after pasting)
*/
const processFiles = async function(files, token, uploadId, rename) {
if (rename) {
files.forEach(file => {
// note: can't overwrite the original read-only name attribute
file.newName = moment(file.lastModified || file.lastModifiedDate).format('YYYYMMDD_HHmmss')
+ getFileExtension(file.name)
})
}
// Process these files in the store
await store.dispatch('initialiseUpload', { uploadId, token, files })
}
export {
findUniquePath,
processFiles,
getFileExtension,
}
Loading…
Cancel
Save