Fixes the issue with attachments replacing each other locally so files with same filename appears as the same file
44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
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';
|
|
}
|