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