aboutsummaryrefslogtreecommitdiff
path: root/src/components/media_upload/media_upload.js
blob: e4b3d460e98da9f9a890906ce192469fa3c57e60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* eslint-env browser */
import statusPosterService from '../../services/status_poster/status_poster.service.js'
import fileSizeFormatService from '../../services/file_size_format/file_size_format.js'

const mediaUpload = {
  data () {
    return {
      uploading: false,
      uploadReady: true
    }
  },
  methods: {
    uploadFile (file) {
      const self = this
      const store = this.$store
      if (file.size > store.state.instance.uploadlimit) {
        const filesize = fileSizeFormatService.fileSizeFormat(file.size)
        const allowedsize = fileSizeFormatService.fileSizeFormat(store.state.instance.uploadlimit)
        self.$emit('upload-failed', 'file_too_big', {filesize: filesize.num, filesizeunit: filesize.unit, allowedsize: allowedsize.num, allowedsizeunit: allowedsize.unit})
        return
      }
      const formData = new FormData()
      formData.append('file', file)

      self.$emit('uploading')
      self.uploading = true

      statusPosterService.uploadMedia({ store, formData })
        .then((fileData) => {
          self.$emit('uploaded', fileData)
          self.uploading = false
        }, (error) => { // eslint-disable-line handle-callback-err
          self.$emit('upload-failed', 'default')
          self.uploading = false
        })
    },
    fileDrop (e) {
      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
      if (types.contains('Files')) {
        e.dataTransfer.dropEffect = 'copy'
      } else {
        e.dataTransfer.dropEffect = 'none'
      }
    },
    clearFile () {
      this.uploadReady = false
      this.$nextTick(() => {
        this.uploadReady = true
      })
    },
    change ({target}) {
      for (var i = 0; i < target.files.length; i++) {
        let file = target.files[i]
        this.uploadFile(file)
      }
    }
  },
  props: [
    'dropFiles'
  ],
  watch: {
    'dropFiles': function (fileInfos) {
      if (!this.uploading) {
        this.uploadFile(fileInfos[0])
      }
    }
  }
}

export default mediaUpload