refactor: stricter domain: attachments
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
const ROOM_NAME_SANITIZER = /[^\w.-]+/g;
|
||||
const STORED_FILENAME_SANITIZER = /[^\w.-]+/g;
|
||||
|
||||
export function sanitizeAttachmentRoomName(roomName: string): string {
|
||||
const sanitizedRoomName = roomName.trim().replace(ROOM_NAME_SANITIZER, '_');
|
||||
|
||||
return sanitizedRoomName || 'room';
|
||||
}
|
||||
|
||||
export function resolveAttachmentStoredFilename(attachmentId: string, filename: string): string {
|
||||
const sanitizedAttachmentId = attachmentId.trim().replace(STORED_FILENAME_SANITIZER, '_') || 'attachment';
|
||||
const basename = filename.trim().split(/[\\/]/)
|
||||
.pop() ?? '';
|
||||
const extensionIndex = basename.lastIndexOf('.');
|
||||
|
||||
if (extensionIndex <= 0 || extensionIndex === basename.length - 1) {
|
||||
return sanitizedAttachmentId;
|
||||
}
|
||||
|
||||
const sanitizedExtension = basename.slice(extensionIndex)
|
||||
.replace(STORED_FILENAME_SANITIZER, '_')
|
||||
.toLowerCase();
|
||||
|
||||
return sanitizedExtension === '.'
|
||||
? sanitizedAttachmentId
|
||||
: `${sanitizedAttachmentId}${sanitizedExtension}`;
|
||||
}
|
||||
|
||||
export function resolveAttachmentStorageBucket(mime: string): 'video' | 'audio' | 'image' | 'files' {
|
||||
if (mime.startsWith('video/')) {
|
||||
return 'video';
|
||||
}
|
||||
|
||||
if (mime.startsWith('audio/')) {
|
||||
return 'audio';
|
||||
}
|
||||
|
||||
if (mime.startsWith('image/')) {
|
||||
return 'image';
|
||||
}
|
||||
|
||||
return 'files';
|
||||
}
|
||||
Reference in New Issue
Block a user