yandere_fe/src/components/media_upload/media_upload.js

71 lines
1.7 KiB
JavaScript
Raw Normal View History

2016-11-06 10:28:37 -08: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 04:05:03 -07:00
for (var i = 0; i < target.files.length; i++) {
let file = target.files[i]
this.uploadFile(file)
}
})
},
data () {
return {
uploading: false
}
},
methods: {
uploadFile (file) {
const self = this
const store = this.$store
2018-12-08 07:23:21 -08:00
if (file.size > store.state.instance.uploadlimit) {
self.$emit('upload-failed', 'upload_error_file_too_big')
return
}
2016-11-07 06:04:27 -08:00
const formData = new FormData()
formData.append('media', file)
2016-11-13 09:26:10 -08:00
self.$emit('uploading')
self.uploading = true
2016-11-06 10:28:37 -08:00
statusPosterService.uploadMedia({ store, formData })
.then((fileData) => {
self.$emit('uploaded', fileData)
2016-11-13 09:26:10 -08:00
self.uploading = false
2017-02-22 13:43:40 -08:00
}, (error) => { // eslint-disable-line handle-callback-err
2016-11-13 09:26:10 -08:00
self.$emit('upload-failed')
self.uploading = false
2016-11-06 10:28:37 -08:00
})
},
fileDrop (e) {
2017-02-22 13:33:28 -08:00
if (e.dataTransfer.files.length > 0) {
e.preventDefault() // allow dropping text like before
this.uploadFile(e.dataTransfer.files[0])
}
},
fileDrag (e) {
let types = e.dataTransfer.types
2017-02-22 13:33:28 -08:00
if (types.contains('Files')) {
e.dataTransfer.dropEffect = 'copy'
} else {
e.dataTransfer.dropEffect = 'none'
}
}
2016-11-13 09:26:10 -08:00
},
props: [
'dropFiles'
],
watch: {
'dropFiles': function (fileInfos) {
if (!this.uploading) {
this.uploadFile(fileInfos[0])
}
2016-11-13 09:26:10 -08:00
}
2016-11-06 10:28:37 -08:00
}
}
export default mediaUpload