diff --git a/app/builders/messages/message_builder.rb b/app/builders/messages/message_builder.rb
index 2df286f4a..9c26ccca1 100644
--- a/app/builders/messages/message_builder.rb
+++ b/app/builders/messages/message_builder.rb
@@ -29,10 +29,12 @@ class Messages::MessageBuilder
return if @attachments.blank?
@attachments.each do |uploaded_attachment|
- @message.attachments.build(
+ attachment = @message.attachments.build(
account_id: @message.account_id,
file: uploaded_attachment
)
+
+ attachment.file_type = file_type(uploaded_attachment&.content_type) if uploaded_attachment.is_a?(ActionDispatch::Http::UploadedFile)
end
end
diff --git a/app/controllers/api/v1/widget/messages_controller.rb b/app/controllers/api/v1/widget/messages_controller.rb
index a35506d96..c2fa6304b 100644
--- a/app/controllers/api/v1/widget/messages_controller.rb
+++ b/app/controllers/api/v1/widget/messages_controller.rb
@@ -29,10 +29,12 @@ class Api::V1::Widget::MessagesController < Api::V1::Widget::BaseController
return if params[:message][:attachments].blank?
params[:message][:attachments].each do |uploaded_attachment|
- @message.attachments.new(
+ attachment = @message.attachments.new(
account_id: @message.account_id,
file: uploaded_attachment
)
+
+ attachment.file_type = helpers.file_type(uploaded_attachment&.content_type) if uploaded_attachment.is_a?(ActionDispatch::Http::UploadedFile)
end
end
diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb
index 783b138af..52c6c87af 100644
--- a/app/controllers/dashboard_controller.rb
+++ b/app/controllers/dashboard_controller.rb
@@ -25,7 +25,8 @@ class DashboardController < ActionController::Base
'API_CHANNEL_NAME',
'API_CHANNEL_THUMBNAIL',
'ANALYTICS_TOKEN',
- 'ANALYTICS_HOST'
+ 'ANALYTICS_HOST',
+ 'DIRECT_UPLOADS_ENABLED'
).merge(app_config)
end
diff --git a/app/controllers/widgets_controller.rb b/app/controllers/widgets_controller.rb
index 29c1f97f9..ad7d2e3c0 100644
--- a/app/controllers/widgets_controller.rb
+++ b/app/controllers/widgets_controller.rb
@@ -10,7 +10,7 @@ class WidgetsController < ActionController::Base
private
def set_global_config
- @global_config = GlobalConfig.get('LOGO_THUMBNAIL', 'BRAND_NAME', 'WIDGET_BRAND_URL')
+ @global_config = GlobalConfig.get('LOGO_THUMBNAIL', 'BRAND_NAME', 'WIDGET_BRAND_URL', 'DIRECT_UPLOADS_ENABLED')
end
def set_web_widget
diff --git a/app/javascript/dashboard/components/widgets/AttachmentsPreview.vue b/app/javascript/dashboard/components/widgets/AttachmentsPreview.vue
index 57797b5f3..250c5c3c6 100644
--- a/app/javascript/dashboard/components/widgets/AttachmentsPreview.vue
+++ b/app/javascript/dashboard/components/widgets/AttachmentsPreview.vue
@@ -7,7 +7,7 @@
>
![]()
@@ -15,12 +15,12 @@
- {{ attachment.resource.filename }}
+ {{ fileName(attachment.resource) }}
- {{ formatFileSize(attachment.resource.byte_size) }}
+ {{ formatFileSize(attachment.resource) }}
@@ -50,12 +50,17 @@ export default {
onRemoveAttachment(index) {
this.removeAttachment(index);
},
- formatFileSize(size) {
+ formatFileSize(file) {
+ const size = file.byte_size || file.size;
return formatBytes(size, 0);
},
- isTypeImage(type) {
+ isTypeImage(file) {
+ const type = file.content_type || file.type;
return type.includes('image');
},
+ fileName(file) {
+ return file.filename || file.name;
+ },
},
};
diff --git a/app/javascript/dashboard/components/widgets/WootWriter/ReplyBottomPanel.vue b/app/javascript/dashboard/components/widgets/WootWriter/ReplyBottomPanel.vue
index 6481ad57a..5bbd913a9 100644
--- a/app/javascript/dashboard/components/widgets/WootWriter/ReplyBottomPanel.vue
+++ b/app/javascript/dashboard/components/widgets/WootWriter/ReplyBottomPanel.vue
@@ -25,7 +25,7 @@
direct_upload_url: '/rails/active_storage/direct_uploads',
direct_upload: true,
}"
- @input-file="onDirectFileUpload"
+ @input-file="onFileUpload"
>
{},
},
diff --git a/app/javascript/dashboard/components/widgets/conversation/ReplyBox.vue b/app/javascript/dashboard/components/widgets/conversation/ReplyBox.vue
index 2f9681c22..f7be9f917 100644
--- a/app/javascript/dashboard/components/widgets/conversation/ReplyBox.vue
+++ b/app/javascript/dashboard/components/widgets/conversation/ReplyBox.vue
@@ -77,7 +77,7 @@
:mode="replyType"
:inbox="inbox"
:send-button-text="replyButtonLabel"
- :on-direct-file-upload="onDirectFileUpload"
+ :on-file-upload="onFileUpload"
:show-file-upload="showFileUpload"
:toggle-emoji-picker="toggleEmojiPicker"
:show-emoji-picker="showEmojiPicker"
@@ -179,6 +179,7 @@ export default {
currentChat: 'getSelectedChat',
messageSignature: 'getMessageSignature',
currentUser: 'getCurrentUser',
+ globalConfig: 'globalConfig/get',
}),
showRichContentEditor() {
@@ -544,6 +545,13 @@ export default {
isPrivate,
});
},
+ onFileUpload(file) {
+ if (this.globalConfig.directUploadsEnabled) {
+ this.onDirectFileUpload(file);
+ } else {
+ this.onIndirectFileUpload(file);
+ }
+ },
onDirectFileUpload(file) {
if (!file) {
return;
@@ -559,13 +567,7 @@ export default {
if (error) {
this.showAlert(error);
} else {
- this.attachedFiles.push({
- currentChatId: this.currentChat.id,
- resource: blob,
- isPrivate: this.isPrivate,
- thumb: null,
- blobSignedId: blob.signed_id,
- });
+ this.attachFile({ file, blob });
}
});
} else {
@@ -576,22 +578,12 @@ export default {
);
}
},
- onFileUpload(file) {
+ onIndirectFileUpload(file) {
if (!file) {
return;
}
if (checkFileSizeLimit(file, MAXIMUM_FILE_UPLOAD_SIZE)) {
- this.attachedFiles = [];
- const reader = new FileReader();
- reader.readAsDataURL(file.file);
- reader.onloadend = () => {
- this.attachedFiles.push({
- currentChatId: this.currentChat.id,
- resource: file,
- isPrivate: this.isPrivate,
- thumb: reader.result,
- });
- };
+ this.attachFile({ file });
} else {
this.showAlert(
this.$t('CONVERSATION.FILE_SIZE_LIMIT', {
@@ -600,6 +592,19 @@ export default {
);
}
},
+ attachFile({ blob, file }) {
+ const reader = new FileReader();
+ reader.readAsDataURL(file.file);
+ reader.onloadend = () => {
+ this.attachedFiles.push({
+ currentChatId: this.currentChat.id,
+ resource: blob || file,
+ isPrivate: this.isPrivate,
+ thumb: reader.result,
+ blobSignedId: blob ? blob.signed_id : undefined,
+ });
+ };
+ },
removeAttachment(itemIndex) {
this.attachedFiles = this.attachedFiles.filter(
(item, index) => itemIndex !== index
@@ -619,7 +624,11 @@ export default {
if (this.attachedFiles && this.attachedFiles.length) {
messagePayload.files = [];
this.attachedFiles.forEach(attachment => {
- messagePayload.files.push(attachment.blobSignedId);
+ if (this.globalConfig.directUploadsEnabled) {
+ messagePayload.files.push(attachment.blobSignedId);
+ } else {
+ messagePayload.files.push(attachment.resource.file);
+ }
});
}
diff --git a/app/javascript/shared/store/globalConfig.js b/app/javascript/shared/store/globalConfig.js
index 69117179a..9082db4ac 100644
--- a/app/javascript/shared/store/globalConfig.js
+++ b/app/javascript/shared/store/globalConfig.js
@@ -5,6 +5,7 @@ const {
BRAND_NAME: brandName,
CHATWOOT_INBOX_TOKEN: chatwootInboxToken,
CREATE_NEW_ACCOUNT_FROM_DASHBOARD: createNewAccountFromDashboard,
+ DIRECT_UPLOADS_ENABLED: directUploadsEnabled,
DISPLAY_MANIFEST: displayManifest,
INSTALLATION_NAME: installationName,
LOGO_THUMBNAIL: logoThumbnail,
@@ -21,6 +22,7 @@ const state = {
brandName,
chatwootInboxToken,
createNewAccountFromDashboard,
+ directUploadsEnabled: directUploadsEnabled === 'true',
displayManifest,
installationName,
logo,
diff --git a/app/javascript/widget/api/endPoints.js b/app/javascript/widget/api/endPoints.js
index 12073c872..df37f2363 100755
--- a/app/javascript/widget/api/endPoints.js
+++ b/app/javascript/widget/api/endPoints.js
@@ -41,7 +41,12 @@ const sendAttachment = ({ attachment }) => {
const { file } = attachment;
const formData = new FormData();
- formData.append('message[attachments][]', file);
+ if (typeof file === 'string') {
+ formData.append('message[attachments][]', file);
+ } else {
+ formData.append('message[attachments][]', file, file.name);
+ }
+
formData.append('message[referer_url]', referrerURL);
formData.append('message[timestamp]', timestamp);
return {
diff --git a/app/javascript/widget/components/ChatAttachment.vue b/app/javascript/widget/components/ChatAttachment.vue
index 1625a41a2..ff37abefd 100755
--- a/app/javascript/widget/components/ChatAttachment.vue
+++ b/app/javascript/widget/components/ChatAttachment.vue
@@ -2,8 +2,11 @@