Skip to content

Commit 33a2a8d

Browse files
committed
feat: support size_limit in upload config
1 parent b051698 commit 33a2a8d

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/components/MessageInput/hooks/utils.ts

+24-5
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,13 @@ export const checkUploadPermissions = async <
136136
allowed_mime_types,
137137
blocked_file_extensions,
138138
blocked_mime_types,
139+
size_limit,
139140
} =
140141
((uploadType === 'image'
141142
? appSettings?.app?.image_upload_config
142143
: appSettings?.app?.file_upload_config) as FileUploadConfig) || {};
143144

144-
const sendErrorNotification = () =>
145+
const sendNotAllowedErrorNotification = () =>
145146
addNotification(
146147
t(`Upload type: "{{ type }}" is not allowed`, { type: file.type || 'unknown type' }),
147148
'error',
@@ -153,7 +154,7 @@ export const checkUploadPermissions = async <
153154
);
154155

155156
if (!allowed) {
156-
sendErrorNotification();
157+
sendNotAllowedErrorNotification();
157158
return false;
158159
}
159160
}
@@ -164,7 +165,7 @@ export const checkUploadPermissions = async <
164165
);
165166

166167
if (blocked) {
167-
sendErrorNotification();
168+
sendNotAllowedErrorNotification();
168169
return false;
169170
}
170171
}
@@ -175,7 +176,7 @@ export const checkUploadPermissions = async <
175176
);
176177

177178
if (!allowed) {
178-
sendErrorNotification();
179+
sendNotAllowedErrorNotification();
179180
return false;
180181
}
181182
}
@@ -186,10 +187,28 @@ export const checkUploadPermissions = async <
186187
);
187188

188189
if (blocked) {
189-
sendErrorNotification();
190+
sendNotAllowedErrorNotification();
190191
return false;
191192
}
192193
}
193194

195+
if (file.size && size_limit && file.size > size_limit) {
196+
addNotification(
197+
t('File is too large: {{ size }}, maximum upload size is {{ limit }}', {
198+
limit: prettifyFileSize(size_limit),
199+
size: prettifyFileSize(file.size),
200+
}),
201+
'error',
202+
);
203+
return false;
204+
}
205+
194206
return true;
195207
};
208+
209+
function prettifyFileSize(bytes: number) {
210+
const units = ['B', 'kB', 'MB', 'GB'];
211+
const exponent = Math.min(Math.floor(Math.log(bytes) / Math.log(1024)), units.length - 1);
212+
const mantissa = bytes / 1024 ** exponent;
213+
return `${mantissa.toPrecision(3)} ${units[exponent]}`;
214+
}

src/i18n/en.json

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"Error: {{ errorMessage }}": "Error: {{ errorMessage }}",
3030
"Failed to jump to the first unread message": "Failed to jump to the first unread message",
3131
"Failed to mark channel as read": "Failed to mark channel as read",
32+
"File is too large: {{ size }}, maximum upload size is {{ limit }}": "File is too large: {{ size }}, maximum upload size is {{ limit }}",
3233
"Flag": "Flag",
3334
"Latest Messages": "Latest Messages",
3435
"Load more": "Load more",

0 commit comments

Comments
 (0)