2016-11-06 18:28:37 +00:00
|
|
|
/* eslint-env browser */
|
|
|
|
import statusPosterService from '../../services/status_poster/status_poster.service.js'
|
2018-12-10 06:50:04 +00:00
|
|
|
import fileSizeFormatService from '../../services/file_size_format/file_size_format.js'
|
|
|
|
|
2016-11-06 18:28:37 +00:00
|
|
|
const mediaUpload = {
|
2017-02-21 13:13:19 +00:00
|
|
|
data () {
|
|
|
|
return {
|
2019-02-04 15:45:26 +00:00
|
|
|
uploading: false,
|
|
|
|
uploadReady: true
|
2017-02-21 13:13:19 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2017-02-21 20:48:48 +00:00
|
|
|
uploadFile (file) {
|
2017-02-21 13:13:19 +00:00
|
|
|
const self = this
|
|
|
|
const store = this.$store
|
2018-12-08 15:23:21 +00:00
|
|
|
if (file.size > store.state.instance.uploadlimit) {
|
2018-12-10 14:06:32 +00:00
|
|
|
const filesize = fileSizeFormatService.fileSizeFormat(file.size)
|
|
|
|
const allowedsize = fileSizeFormatService.fileSizeFormat(store.state.instance.uploadlimit)
|
2018-12-12 13:38:01 +00:00
|
|
|
self.$emit('upload-failed', 'file_too_big', {filesize: filesize.num, filesizeunit: filesize.unit, allowedsize: allowedsize.num, allowedsizeunit: allowedsize.unit})
|
2018-12-08 15:23:21 +00:00
|
|
|
return
|
|
|
|
}
|
2016-11-07 14:04:27 +00:00
|
|
|
const formData = new FormData()
|
|
|
|
formData.append('media', file)
|
2016-11-13 17:26:10 +00:00
|
|
|
|
|
|
|
self.$emit('uploading')
|
|
|
|
self.uploading = true
|
|
|
|
|
2016-11-06 18:28:37 +00:00
|
|
|
statusPosterService.uploadMedia({ store, formData })
|
|
|
|
.then((fileData) => {
|
|
|
|
self.$emit('uploaded', fileData)
|
2016-11-13 17:26:10 +00:00
|
|
|
self.uploading = false
|
2017-02-22 21:43:40 +00:00
|
|
|
}, (error) => { // eslint-disable-line handle-callback-err
|
2018-12-12 13:38:01 +00:00
|
|
|
self.$emit('upload-failed', 'default')
|
2016-11-13 17:26:10 +00:00
|
|
|
self.uploading = false
|
2016-11-06 18:28:37 +00:00
|
|
|
})
|
2017-02-22 12:53:05 +00:00
|
|
|
},
|
|
|
|
fileDrop (e) {
|
2017-02-22 21:33:28 +00:00
|
|
|
if (e.dataTransfer.files.length > 0) {
|
2017-02-22 12:53:05 +00:00
|
|
|
e.preventDefault() // allow dropping text like before
|
|
|
|
this.uploadFile(e.dataTransfer.files[0])
|
|
|
|
}
|
|
|
|
},
|
|
|
|
fileDrag (e) {
|
|
|
|
let types = e.dataTransfer.types
|
2017-02-22 21:33:28 +00:00
|
|
|
if (types.contains('Files')) {
|
2017-02-22 12:53:05 +00:00
|
|
|
e.dataTransfer.dropEffect = 'copy'
|
|
|
|
} else {
|
|
|
|
e.dataTransfer.dropEffect = 'none'
|
|
|
|
}
|
2019-02-04 15:45:26 +00:00
|
|
|
},
|
|
|
|
clearFile () {
|
|
|
|
this.uploadReady = false
|
|
|
|
this.$nextTick(() => {
|
|
|
|
this.uploadReady = true
|
|
|
|
})
|
|
|
|
},
|
|
|
|
change ({target}) {
|
|
|
|
for (var i = 0; i < target.files.length; i++) {
|
|
|
|
let file = target.files[i]
|
|
|
|
this.uploadFile(file)
|
|
|
|
}
|
2017-02-21 13:13:19 +00:00
|
|
|
}
|
2016-11-13 17:26:10 +00:00
|
|
|
},
|
2017-02-21 13:13:19 +00:00
|
|
|
props: [
|
|
|
|
'dropFiles'
|
|
|
|
],
|
|
|
|
watch: {
|
|
|
|
'dropFiles': function (fileInfos) {
|
2017-02-21 20:48:48 +00:00
|
|
|
if (!this.uploading) {
|
2017-02-21 13:13:19 +00:00
|
|
|
this.uploadFile(fileInfos[0])
|
2017-02-21 20:48:48 +00:00
|
|
|
}
|
2016-11-13 17:26:10 +00:00
|
|
|
}
|
2016-11-06 18:28:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default mediaUpload
|