media_upload.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. const mediaUpload = {
  5. data () {
  6. return {
  7. uploading: false,
  8. uploadReady: true
  9. }
  10. },
  11. methods: {
  12. uploadFile (file) {
  13. const self = this
  14. const store = this.$store
  15. if (file.size > store.state.instance.uploadlimit) {
  16. const filesize = fileSizeFormatService.fileSizeFormat(file.size)
  17. const allowedsize = fileSizeFormatService.fileSizeFormat(store.state.instance.uploadlimit)
  18. self.$emit('upload-failed', 'file_too_big', {filesize: filesize.num, filesizeunit: filesize.unit, allowedsize: allowedsize.num, allowedsizeunit: allowedsize.unit})
  19. return
  20. }
  21. const formData = new FormData()
  22. formData.append('media', file)
  23. self.$emit('uploading')
  24. self.uploading = true
  25. statusPosterService.uploadMedia({ store, formData })
  26. .then((fileData) => {
  27. self.$emit('uploaded', fileData)
  28. self.uploading = false
  29. }, (error) => { // eslint-disable-line handle-callback-err
  30. self.$emit('upload-failed', 'default')
  31. self.uploading = false
  32. })
  33. },
  34. fileDrop (e) {
  35. if (e.dataTransfer.files.length > 0) {
  36. e.preventDefault() // allow dropping text like before
  37. this.uploadFile(e.dataTransfer.files[0])
  38. }
  39. },
  40. fileDrag (e) {
  41. let types = e.dataTransfer.types
  42. if (types.contains('Files')) {
  43. e.dataTransfer.dropEffect = 'copy'
  44. } else {
  45. e.dataTransfer.dropEffect = 'none'
  46. }
  47. },
  48. clearFile () {
  49. this.uploadReady = false
  50. this.$nextTick(() => {
  51. this.uploadReady = true
  52. })
  53. },
  54. change ({target}) {
  55. for (var i = 0; i < target.files.length; i++) {
  56. let file = target.files[i]
  57. this.uploadFile(file)
  58. }
  59. }
  60. },
  61. props: [
  62. 'dropFiles'
  63. ],
  64. watch: {
  65. 'dropFiles': function (fileInfos) {
  66. if (!this.uploading) {
  67. this.uploadFile(fileInfos[0])
  68. }
  69. }
  70. }
  71. }
  72. export default mediaUpload