debian-mirror-gitlab/app/assets/javascripts/ide/components/new_dropdown/upload.vue

106 lines
2.7 KiB
Vue
Raw Normal View History

2018-05-09 12:01:36 +05:30
<script>
2018-11-18 11:00:15 +05:30
import ItemButton from './button.vue';
export default {
components: {
ItemButton,
},
props: {
path: {
type: String,
required: false,
default: '',
2018-05-09 12:01:36 +05:30
},
2018-11-18 11:00:15 +05:30
showLabel: {
type: Boolean,
required: false,
default: true,
2018-05-09 12:01:36 +05:30
},
2018-11-18 11:00:15 +05:30
buttonCssClasses: {
type: String,
required: false,
default: null,
2018-05-09 12:01:36 +05:30
},
2018-11-18 11:00:15 +05:30
},
methods: {
2018-12-13 13:39:08 +05:30
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) {
2018-11-18 11:00:15 +05:30
const { name } = file;
2020-03-13 15:44:24 +05:30
const encodedContent = target.result.split('base64,')[1];
2018-12-13 13:39:08 +05:30
const rawContent = encodedContent ? atob(encodedContent) : '';
const isText = this.isText(rawContent, file.type);
2018-05-09 12:01:36 +05:30
2020-03-13 15:44:24 +05:30
const emitCreateEvent = content =>
this.$emit('create', {
name: `${this.path ? `${this.path}/` : ''}${name}`,
type: 'blob',
content,
base64: !isText,
binary: !isText,
rawPath: !isText ? target.result : '',
});
2018-05-09 12:01:36 +05:30
2020-03-13 15:44:24 +05:30
if (isText) {
const reader = new FileReader();
reader.addEventListener('load', e => emitCreateEvent(e.target.result), { once: true });
reader.readAsText(file);
} else {
emitCreateEvent(encodedContent);
}
2018-11-18 11:00:15 +05:30
},
readFile(file) {
const reader = new FileReader();
2018-05-09 12:01:36 +05:30
2018-12-13 13:39:08 +05:30
reader.addEventListener('load', e => this.createFile(e.target, file), { once: true });
reader.readAsDataURL(file);
2018-11-18 11:00:15 +05:30
},
openFile() {
Array.from(this.$refs.fileUpload.files).forEach(file => this.readFile(file));
2018-05-09 12:01:36 +05:30
},
2018-11-18 11:00:15 +05:30
startFileUpload() {
this.$refs.fileUpload.click();
},
},
};
2018-05-09 12:01:36 +05:30
</script>
<template>
<div>
2018-11-18 11:00:15 +05:30
<item-button
:class="buttonCssClasses"
:show-label="showLabel"
:icon-classes="showLabel ? 'mr-2' : ''"
:label="__('Upload file')"
class="d-flex"
icon="upload"
@click="startFileUpload"
/>
2018-05-09 12:01:36 +05:30
<input
id="file-upload"
2018-11-08 19:23:39 +05:30
ref="fileUpload"
2018-05-09 12:01:36 +05:30
type="file"
class="hidden"
2018-11-20 20:47:30 +05:30
multiple
@change="openFile"
2018-05-09 12:01:36 +05:30
/>
</div>
</template>