feat: Add channel-specific file upload rules and size limits (#12237)

This commit is contained in:
Sivin Varghese
2025-08-26 22:23:39 +05:30
committed by GitHub
parent 19faa7fdfa
commit 39dfa35229
7 changed files with 168 additions and 150 deletions

View File

@@ -1,10 +1,7 @@
import { mapGetters } from 'vuex';
import { useAlert } from 'dashboard/composables';
import {
MAXIMUM_FILE_UPLOAD_SIZE,
MAXIMUM_FILE_UPLOAD_SIZE_TWILIO_SMS_CHANNEL,
} from 'shared/constants/messages';
import { checkFileSizeLimit } from 'shared/helpers/FileHelper';
import { getMaxUploadSizeByChannel } from '@chatwoot/utils';
import { DirectUpload } from 'activestorage';
export default {
@@ -13,7 +10,22 @@ export default {
accountId: 'getCurrentAccountId',
}),
},
methods: {
maxSizeFor(mime) {
return getMaxUploadSizeByChannel({
channelType: this.inbox?.channel_type,
medium: this.inbox?.medium, // e.g. 'sms' | 'whatsapp'
mime, // e.g. 'image/png'
});
},
alertOverLimit(maxSizeMB) {
useAlert(
this.$t('CONVERSATION.FILE_SIZE_LIMIT', {
MAXIMUM_SUPPORTED_FILE_UPLOAD_SIZE: maxSizeMB,
})
);
},
onFileUpload(file) {
if (this.globalConfig.directUploadsEnabled) {
this.onDirectFileUpload(file);
@@ -21,59 +33,52 @@ export default {
this.onIndirectFileUpload(file);
}
},
onDirectFileUpload(file) {
const MAXIMUM_SUPPORTED_FILE_UPLOAD_SIZE = this.isATwilioSMSChannel
? MAXIMUM_FILE_UPLOAD_SIZE_TWILIO_SMS_CHANNEL
: MAXIMUM_FILE_UPLOAD_SIZE;
if (!file) return;
if (!file) {
const mime = file.file?.type || file.type;
const maxSizeMB = this.maxSizeFor(mime);
if (!checkFileSizeLimit(file, maxSizeMB)) {
this.alertOverLimit(maxSizeMB);
return;
}
if (checkFileSizeLimit(file, MAXIMUM_SUPPORTED_FILE_UPLOAD_SIZE)) {
const upload = new DirectUpload(
file.file,
`/api/v1/accounts/${this.accountId}/conversations/${this.currentChat.id}/direct_uploads`,
{
directUploadWillCreateBlobWithXHR: xhr => {
xhr.setRequestHeader(
'api_access_token',
this.currentUser.access_token
);
},
}
);
upload.create((error, blob) => {
if (error) {
useAlert(error);
} else {
this.attachFile({ file, blob });
}
});
} else {
useAlert(
this.$t('CONVERSATION.FILE_SIZE_LIMIT', {
MAXIMUM_SUPPORTED_FILE_UPLOAD_SIZE,
})
);
}
const upload = new DirectUpload(
file.file,
`/api/v1/accounts/${this.accountId}/conversations/${this.currentChat.id}/direct_uploads`,
{
directUploadWillCreateBlobWithXHR: xhr => {
xhr.setRequestHeader(
'api_access_token',
this.currentUser.access_token
);
},
}
);
upload.create((error, blob) => {
if (error) {
useAlert(error);
} else {
this.attachFile({ file, blob });
}
});
},
onIndirectFileUpload(file) {
const MAXIMUM_SUPPORTED_FILE_UPLOAD_SIZE = this.isATwilioSMSChannel
? MAXIMUM_FILE_UPLOAD_SIZE_TWILIO_SMS_CHANNEL
: MAXIMUM_FILE_UPLOAD_SIZE;
if (!file) {
if (!file) return;
const mime = file.file?.type || file.type;
const maxSizeMB = this.maxSizeFor(mime);
if (!checkFileSizeLimit(file, maxSizeMB)) {
this.alertOverLimit(maxSizeMB);
return;
}
if (checkFileSizeLimit(file, MAXIMUM_SUPPORTED_FILE_UPLOAD_SIZE)) {
this.attachFile({ file });
} else {
useAlert(
this.$t('CONVERSATION.FILE_SIZE_LIMIT', {
MAXIMUM_SUPPORTED_FILE_UPLOAD_SIZE,
})
);
}
this.attachFile({ file });
},
},
};