2016-11-06 10:28:37 -08:00
|
|
|
/* eslint-env browser */
|
|
|
|
import statusPosterService from '../../services/status_poster/status_poster.service.js'
|
2018-12-09 22:50:04 -08:00
|
|
|
import fileSizeFormatService from '../../services/file_size_format/file_size_format.js'
|
|
|
|
|
2016-11-06 10:28:37 -08:00
|
|
|
const mediaUpload = {
|
2017-02-21 05:13:19 -08:00
|
|
|
data () {
|
|
|
|
return {
|
2020-06-08 09:22:17 -07:00
|
|
|
uploadCount: 0,
|
2019-02-04 07:45:26 -08:00
|
|
|
uploadReady: true
|
2017-02-21 05:13:19 -08:00
|
|
|
}
|
|
|
|
},
|
2020-06-08 09:22:17 -07:00
|
|
|
computed: {
|
|
|
|
uploading () {
|
|
|
|
return this.uploadCount > 0
|
|
|
|
}
|
|
|
|
},
|
2017-02-21 05:13:19 -08:00
|
|
|
methods: {
|
2017-02-21 12:48:48 -08:00
|
|
|
uploadFile (file) {
|
2017-02-21 05:13:19 -08:00
|
|
|
const self = this
|
|
|
|
const store = this.$store
|
2018-12-08 07:23:21 -08:00
|
|
|
if (file.size > store.state.instance.uploadlimit) {
|
2018-12-10 06:06:32 -08:00
|
|
|
const filesize = fileSizeFormatService.fileSizeFormat(file.size)
|
|
|
|
const allowedsize = fileSizeFormatService.fileSizeFormat(store.state.instance.uploadlimit)
|
2019-07-05 00:02:14 -07:00
|
|
|
self.$emit('upload-failed', 'file_too_big', { filesize: filesize.num, filesizeunit: filesize.unit, allowedsize: allowedsize.num, allowedsizeunit: allowedsize.unit })
|
2018-12-08 07:23:21 -08:00
|
|
|
return
|
|
|
|
}
|
2016-11-07 06:04:27 -08:00
|
|
|
const formData = new FormData()
|
2019-03-15 12:02:00 -07:00
|
|
|
formData.append('file', file)
|
2016-11-13 09:26:10 -08:00
|
|
|
|
|
|
|
self.$emit('uploading')
|
2020-06-08 09:22:17 -07:00
|
|
|
self.uploadCount++
|
2016-11-13 09:26:10 -08:00
|
|
|
|
2016-11-06 10:28:37 -08:00
|
|
|
statusPosterService.uploadMedia({ store, formData })
|
|
|
|
.then((fileData) => {
|
|
|
|
self.$emit('uploaded', fileData)
|
2020-06-08 09:22:17 -07:00
|
|
|
self.decreaseUploadCount()
|
2017-02-22 13:43:40 -08:00
|
|
|
}, (error) => { // eslint-disable-line handle-callback-err
|
2018-12-12 05:38:01 -08:00
|
|
|
self.$emit('upload-failed', 'default')
|
2020-06-08 09:22:17 -07:00
|
|
|
self.decreaseUploadCount()
|
2016-11-06 10:28:37 -08:00
|
|
|
})
|
2017-02-22 04:53:05 -08:00
|
|
|
},
|
2020-06-08 09:26:16 -07:00
|
|
|
decreaseUploadCount () {
|
2020-06-08 09:22:17 -07:00
|
|
|
this.uploadCount--
|
|
|
|
if (this.uploadCount === 0) {
|
|
|
|
this.$emit('all-uploaded')
|
|
|
|
}
|
|
|
|
},
|
2019-02-04 07:45:26 -08:00
|
|
|
clearFile () {
|
|
|
|
this.uploadReady = false
|
|
|
|
this.$nextTick(() => {
|
|
|
|
this.uploadReady = true
|
|
|
|
})
|
|
|
|
},
|
2020-06-08 09:22:17 -07:00
|
|
|
multiUpload (files) {
|
|
|
|
for (const file of files) {
|
2019-02-04 07:45:26 -08:00
|
|
|
this.uploadFile(file)
|
|
|
|
}
|
2020-06-08 09:22:17 -07:00
|
|
|
},
|
|
|
|
change ({ target }) {
|
|
|
|
this.multiUpload(target.files)
|
2017-02-21 05:13:19 -08:00
|
|
|
}
|
2016-11-13 09:26:10 -08:00
|
|
|
},
|
2017-02-21 05:13:19 -08:00
|
|
|
props: [
|
2020-05-07 06:10:53 -07:00
|
|
|
'dropFiles',
|
|
|
|
'disabled'
|
2017-02-21 05:13:19 -08:00
|
|
|
],
|
|
|
|
watch: {
|
|
|
|
'dropFiles': function (fileInfos) {
|
2017-02-21 12:48:48 -08:00
|
|
|
if (!this.uploading) {
|
2020-06-08 09:22:17 -07:00
|
|
|
this.multiUpload(fileInfos)
|
2017-02-21 12:48:48 -08:00
|
|
|
}
|
2016-11-13 09:26:10 -08:00
|
|
|
}
|
2016-11-06 10:28:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default mediaUpload
|