2020-03-30 06:45:06 +00:00
|
|
|
<template>
|
2020-12-07 08:18:41 +00:00
|
|
|
<file-upload
|
|
|
|
:size="4096 * 2048"
|
2021-12-20 18:20:37 +00:00
|
|
|
:accept="allowedFileTypes"
|
2022-02-15 10:46:54 +00:00
|
|
|
:data="{
|
|
|
|
direct_upload_url: '/rails/active_storage/direct_uploads',
|
|
|
|
direct_upload: true,
|
|
|
|
}"
|
|
|
|
@input-file="onFileUpload"
|
2020-12-07 08:18:41 +00:00
|
|
|
>
|
2021-11-18 09:48:51 +00:00
|
|
|
<button class="icon-button flex items-center justify-center">
|
|
|
|
<fluent-icon v-if="!isUploading.image" icon="attach" />
|
2020-03-30 06:45:06 +00:00
|
|
|
<spinner v-if="isUploading" size="small" />
|
2021-11-18 09:48:51 +00:00
|
|
|
</button>
|
2020-03-30 06:45:06 +00:00
|
|
|
</file-upload>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
import FileUpload from 'vue-upload-component';
|
|
|
|
import Spinner from 'shared/components/Spinner.vue';
|
2021-04-15 16:58:19 +00:00
|
|
|
import { checkFileSizeLimit } from 'shared/helpers/FileHelper';
|
2021-12-20 18:20:37 +00:00
|
|
|
import {
|
|
|
|
MAXIMUM_FILE_UPLOAD_SIZE,
|
|
|
|
ALLOWED_FILE_TYPES,
|
|
|
|
} from 'shared/constants/messages';
|
2021-04-15 16:58:19 +00:00
|
|
|
import { BUS_EVENTS } from 'shared/constants/busEvents';
|
2021-11-18 09:48:51 +00:00
|
|
|
import FluentIcon from 'shared/components/FluentIcon/Index.vue';
|
2022-01-27 10:27:22 +00:00
|
|
|
import { DirectUpload } from 'activestorage';
|
2022-02-15 10:46:54 +00:00
|
|
|
import { mapGetters } from 'vuex';
|
2022-01-27 10:27:22 +00:00
|
|
|
|
2020-03-30 06:45:06 +00:00
|
|
|
export default {
|
2021-11-18 09:48:51 +00:00
|
|
|
components: { FluentIcon, FileUpload, Spinner },
|
2020-03-30 06:45:06 +00:00
|
|
|
props: {
|
|
|
|
onAttach: {
|
|
|
|
type: Function,
|
|
|
|
default: () => {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return { isUploading: false };
|
|
|
|
},
|
2021-07-13 06:01:21 +00:00
|
|
|
computed: {
|
2022-02-15 10:46:54 +00:00
|
|
|
...mapGetters({ globalConfig: 'globalConfig/get' }),
|
2021-07-13 06:01:21 +00:00
|
|
|
fileUploadSizeLimit() {
|
|
|
|
return MAXIMUM_FILE_UPLOAD_SIZE;
|
|
|
|
},
|
2021-12-20 18:20:37 +00:00
|
|
|
allowedFileTypes() {
|
|
|
|
return ALLOWED_FILE_TYPES;
|
|
|
|
},
|
2021-07-13 06:01:21 +00:00
|
|
|
},
|
2020-03-30 06:45:06 +00:00
|
|
|
methods: {
|
2020-04-02 06:58:38 +00:00
|
|
|
getFileType(fileType) {
|
|
|
|
return fileType.includes('image') ? 'image' : 'file';
|
|
|
|
},
|
2022-02-15 10:46:54 +00:00
|
|
|
async onFileUpload(file) {
|
|
|
|
if (this.globalConfig.directUploadsEnabled) {
|
|
|
|
this.onDirectFileUpload(file);
|
|
|
|
} else {
|
|
|
|
this.onIndirectFileUpload(file);
|
|
|
|
}
|
|
|
|
},
|
2022-01-27 10:27:22 +00:00
|
|
|
async onDirectFileUpload(file) {
|
|
|
|
if (!file) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.isUploading = true;
|
|
|
|
try {
|
|
|
|
if (checkFileSizeLimit(file, MAXIMUM_FILE_UPLOAD_SIZE)) {
|
2022-02-01 06:55:51 +00:00
|
|
|
const upload = new DirectUpload(
|
|
|
|
file.file,
|
|
|
|
'/rails/active_storage/direct_uploads',
|
|
|
|
null,
|
|
|
|
file.file.name
|
|
|
|
);
|
2022-01-27 10:27:22 +00:00
|
|
|
|
|
|
|
upload.create((error, blob) => {
|
|
|
|
if (error) {
|
|
|
|
window.bus.$emit(BUS_EVENTS.SHOW_ALERT, {
|
|
|
|
message: error,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.onAttach({
|
2022-02-01 06:55:51 +00:00
|
|
|
file: blob.signed_id,
|
2022-02-15 10:46:54 +00:00
|
|
|
...this.getLocalFileAttributes(file),
|
2022-01-27 10:27:22 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
window.bus.$emit(BUS_EVENTS.SHOW_ALERT, {
|
|
|
|
message: this.$t('FILE_SIZE_LIMIT', {
|
|
|
|
MAXIMUM_FILE_UPLOAD_SIZE: this.fileUploadSizeLimit,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
// Error
|
|
|
|
}
|
|
|
|
this.isUploading = false;
|
|
|
|
},
|
2022-02-15 10:46:54 +00:00
|
|
|
async onIndirectFileUpload(file) {
|
2021-04-15 16:58:19 +00:00
|
|
|
if (!file) {
|
|
|
|
return;
|
|
|
|
}
|
2020-03-30 06:45:06 +00:00
|
|
|
this.isUploading = true;
|
|
|
|
try {
|
2021-04-15 16:58:19 +00:00
|
|
|
if (checkFileSizeLimit(file, MAXIMUM_FILE_UPLOAD_SIZE)) {
|
|
|
|
await this.onAttach({
|
|
|
|
file: file.file,
|
2022-02-15 10:46:54 +00:00
|
|
|
...this.getLocalFileAttributes(file),
|
2021-04-15 16:58:19 +00:00
|
|
|
});
|
|
|
|
} else {
|
2021-07-13 06:01:21 +00:00
|
|
|
window.bus.$emit(BUS_EVENTS.SHOW_ALERT, {
|
|
|
|
message: this.$t('FILE_SIZE_LIMIT', {
|
|
|
|
MAXIMUM_FILE_UPLOAD_SIZE: this.fileUploadSizeLimit,
|
|
|
|
}),
|
|
|
|
});
|
2021-04-15 16:58:19 +00:00
|
|
|
}
|
2020-03-30 06:45:06 +00:00
|
|
|
} catch (error) {
|
|
|
|
// Error
|
|
|
|
}
|
|
|
|
this.isUploading = false;
|
|
|
|
},
|
2022-02-15 10:46:54 +00:00
|
|
|
getLocalFileAttributes(file) {
|
|
|
|
return {
|
|
|
|
thumbUrl: window.URL.createObjectURL(file.file),
|
|
|
|
fileType: this.getFileType(file.type),
|
|
|
|
};
|
|
|
|
},
|
2020-03-30 06:45:06 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
</script>
|