summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/ide/components/new_dropdown/upload.vue
blob: ec759043efc5dab30bba95ea9e0750b3effc0155 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<script>
import Icon from '~/vue_shared/components/icon.vue';
import ItemButton from './button.vue';

export default {
  components: {
    Icon,
    ItemButton,
  },
  props: {
    path: {
      type: String,
      required: false,
      default: '',
    },
    showLabel: {
      type: Boolean,
      required: false,
      default: true,
    },
    buttonCssClasses: {
      type: String,
      required: false,
      default: null,
    },
  },
  methods: {
    isText(content, fileType) {
      const knownBinaryFileTypes = ['image/'];
      const knownTextFileTypes = ['text/'];
      const isKnownBinaryFileType = knownBinaryFileTypes.find(type => fileType.includes(type));
      const isKnownTextFileType = knownTextFileTypes.find(type => fileType.includes(type));
      const asciiRegex = /^[ -~\t\n\r]+$/; // tests whether a string contains ascii characters only (ranges from space to tilde, tabs and new lines)

      if (isKnownBinaryFileType) {
        return false;
      }

      if (isKnownTextFileType) {
        return true;
      }

      // if it's not a known file type, determine the type by evaluating the file contents
      return asciiRegex.test(content);
    },
    createFile(target, file) {
      const { name } = file;
      let { result } = target;
      const encodedContent = result.split('base64,')[1];
      const rawContent = encodedContent ? atob(encodedContent) : '';
      const isText = this.isText(rawContent, file.type);

      result = isText ? rawContent : encodedContent;

      this.$emit('create', {
        name: `${this.path ? `${this.path}/` : ''}${name}`,
        type: 'blob',
        content: result,
        base64: !isText,
      });
    },
    readFile(file) {
      const reader = new FileReader();

      reader.addEventListener('load', e => this.createFile(e.target, file), { once: true });
      reader.readAsDataURL(file);
    },
    openFile() {
      Array.from(this.$refs.fileUpload.files).forEach(file => this.readFile(file));
    },
    startFileUpload() {
      this.$refs.fileUpload.click();
    },
  },
};
</script>

<template>
  <div>
    <item-button
      :class="buttonCssClasses"
      :show-label="showLabel"
      :icon-classes="showLabel ? 'mr-2' : ''"
      :label="__('Upload file')"
      class="d-flex"
      icon="upload"
      @click="startFileUpload"
    />
    <input
      id="file-upload"
      ref="fileUpload"
      type="file"
      class="hidden"
      multiple
      @change="openFile"
    />
  </div>
</template>