logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://hacktivis.me/git/pleroma-fe.git

media_upload.js (2426B)


  1. /* eslint-env browser */
  2. import statusPosterService from '../../services/status_poster/status_poster.service.js'
  3. import fileSizeFormatService from '../../services/file_size_format/file_size_format.js'
  4. import { library } from '@fortawesome/fontawesome-svg-core'
  5. import { faUpload, faCircleNotch } from '@fortawesome/free-solid-svg-icons'
  6. library.add(
  7. faUpload,
  8. faCircleNotch
  9. )
  10. const mediaUpload = {
  11. data () {
  12. return {
  13. uploadCount: 0,
  14. uploadReady: true
  15. }
  16. },
  17. computed: {
  18. uploading () {
  19. return this.uploadCount > 0
  20. }
  21. },
  22. methods: {
  23. onClick () {
  24. if (this.uploadReady) {
  25. this.$refs.input.click()
  26. }
  27. },
  28. uploadFile (file) {
  29. const self = this
  30. const store = this.$store
  31. if (file.size > store.state.instance.uploadlimit) {
  32. const filesize = fileSizeFormatService.fileSizeFormat(file.size)
  33. const allowedsize = fileSizeFormatService.fileSizeFormat(store.state.instance.uploadlimit)
  34. self.$emit('upload-failed', 'file_too_big', { filesize: filesize.num, filesizeunit: filesize.unit, allowedsize: allowedsize.num, allowedsizeunit: allowedsize.unit })
  35. return
  36. }
  37. const formData = new FormData()
  38. formData.append('file', file)
  39. self.$emit('uploading')
  40. self.uploadCount++
  41. statusPosterService.uploadMedia({ store, formData })
  42. .then((fileData) => {
  43. self.$emit('uploaded', fileData)
  44. self.decreaseUploadCount()
  45. }, (error) => {
  46. console.error('Error uploading file', error)
  47. self.$emit('upload-failed', 'default')
  48. self.decreaseUploadCount()
  49. })
  50. },
  51. decreaseUploadCount () {
  52. this.uploadCount--
  53. if (this.uploadCount === 0) {
  54. this.$emit('all-uploaded')
  55. }
  56. },
  57. clearFile () {
  58. this.uploadReady = false
  59. this.$nextTick(() => {
  60. this.uploadReady = true
  61. })
  62. },
  63. multiUpload (files) {
  64. for (const file of files) {
  65. this.uploadFile(file)
  66. }
  67. },
  68. change ({ target }) {
  69. this.multiUpload(target.files)
  70. }
  71. },
  72. props: {
  73. dropFiles: Object,
  74. disabled: Boolean,
  75. normalButton: Boolean,
  76. acceptTypes: {
  77. type: String,
  78. default: '*/*'
  79. }
  80. },
  81. watch: {
  82. dropFiles: function (fileInfos) {
  83. if (!this.uploading) {
  84. this.multiUpload(fileInfos)
  85. }
  86. }
  87. }
  88. }
  89. export default mediaUpload