2020-03-30 06:45:06 +00:00
|
|
|
<template>
|
2020-12-07 08:18:41 +00:00
|
|
|
<file-upload
|
2022-10-25 08:05:11 +00:00
|
|
|
ref="upload"
|
2020-12-07 08:18:41 +00:00
|
|
|
:size="4096 * 2048"
|
2021-12-20 18:20:37 +00:00
|
|
|
:accept="allowedFileTypes"
|
2022-02-15 10:46:54 +00:00
|
|
|
:data="{
|
2022-03-16 08:24:18 +00:00
|
|
|
direct_upload_url: '/api/v1/widget/direct_uploads',
|
2022-02-15 10:46:54 +00:00
|
|
|
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
|
|
|
},
|
2022-10-25 08:05:11 +00:00
|
|
|
mounted() {
|
|
|
|
document.addEventListener('paste', this.handleClipboardPaste);
|
|
|
|
},
|
|
|
|
destroyed() {
|
|
|
|
document.removeEventListener('paste', this.handleClipboardPaste);
|
|
|
|
},
|
2020-03-30 06:45:06 +00:00
|
|
|
methods: {
|
2022-10-25 08:05:11 +00:00
|
|
|
handleClipboardPaste(e) {
|
|
|
|
const items = (e.clipboardData || e.originalEvent.clipboardData).items;
|
|
|
|
items.forEach(item => {
|
|
|
|
if (item.kind === 'file') {
|
|
|
|
e.preventDefault();
|
|
|
|
const file = item.getAsFile();
|
|
|
|
this.$refs.upload.add(file);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
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) {
|
2022-10-14 03:43:11 +00:00
|
|
|
await this.onDirectFileUpload(file);
|
2022-02-15 10:46:54 +00:00
|
|
|
} else {
|
2022-10-14 03:43:11 +00:00
|
|
|
await this.onIndirectFileUpload(file);
|
2022-02-15 10:46:54 +00:00
|
|
|
}
|
|
|
|
},
|
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-03-16 08:24:18 +00:00
|
|
|
const { websiteToken } = window.chatwootWebChannel;
|
2022-02-01 06:55:51 +00:00
|
|
|
const upload = new DirectUpload(
|
|
|
|
file.file,
|
2022-03-16 08:24:18 +00:00
|
|
|
`/api/v1/widget/direct_uploads?website_token=${websiteToken}`,
|
|
|
|
{
|
|
|
|
directUploadWillCreateBlobWithXHR: xhr => {
|
|
|
|
xhr.setRequestHeader('X-Auth-Token', window.authToken);
|
|
|
|
},
|
|
|
|
}
|
2022-02-01 06:55:51 +00:00
|
|
|
);
|
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>
|