pleroma-fe/src/components/media_upload/media_upload.js

86 lines
2.2 KiB
JavaScript
Raw Normal View History

2016-11-06 19:28:37 +01:00
/* eslint-env browser */
import statusPosterService from '../../services/status_poster/status_poster.service.js'
2018-12-10 07:50:04 +01:00
import fileSizeFormatService from '../../services/file_size_format/file_size_format.js'
import { library } from '@fortawesome/fontawesome-svg-core'
import { faUpload, faCircleNotch } from '@fortawesome/free-solid-svg-icons'
library.add(
faUpload,
faCircleNotch
)
2016-11-06 19:28:37 +01:00
const mediaUpload = {
data () {
return {
uploadCount: 0,
uploadReady: true
}
},
computed: {
uploading () {
return this.uploadCount > 0
}
},
methods: {
uploadFile (file) {
const self = this
const store = this.$store
2018-12-08 16:23:21 +01:00
if (file.size > store.state.instance.uploadlimit) {
const filesize = fileSizeFormatService.fileSizeFormat(file.size)
const allowedsize = fileSizeFormatService.fileSizeFormat(store.state.instance.uploadlimit)
2019-07-05 09:02:14 +02:00
self.$emit('upload-failed', 'file_too_big', { filesize: filesize.num, filesizeunit: filesize.unit, allowedsize: allowedsize.num, allowedsizeunit: allowedsize.unit })
2018-12-08 16:23:21 +01:00
return
}
2016-11-07 15:04:27 +01:00
const formData = new FormData()
formData.append('file', file)
2016-11-13 18:26:10 +01:00
self.$emit('uploading')
self.uploadCount++
2016-11-13 18:26:10 +01:00
2016-11-06 19:28:37 +01:00
statusPosterService.uploadMedia({ store, formData })
.then((fileData) => {
self.$emit('uploaded', fileData)
self.decreaseUploadCount()
2022-07-31 11:35:48 +02:00
}, (error) => {
console.error('Error uploading file', error)
self.$emit('upload-failed', 'default')
self.decreaseUploadCount()
2016-11-06 19:28:37 +01:00
})
},
2020-06-08 18:26:16 +02:00
decreaseUploadCount () {
this.uploadCount--
if (this.uploadCount === 0) {
this.$emit('all-uploaded')
}
},
clearFile () {
this.uploadReady = false
this.$nextTick(() => {
this.uploadReady = true
})
},
multiUpload (files) {
for (const file of files) {
this.uploadFile(file)
}
},
change ({ target }) {
this.multiUpload(target.files)
}
2016-11-13 18:26:10 +01:00
},
props: [
2020-05-07 15:10:53 +02:00
'dropFiles',
'disabled'
],
watch: {
2022-07-31 11:35:48 +02:00
dropFiles: function (fileInfos) {
if (!this.uploading) {
this.multiUpload(fileInfos)
}
2016-11-13 18:26:10 +01:00
}
2016-11-06 19:28:37 +01:00
}
}
export default mediaUpload