diff --git a/frontend/src/repositories/Document.ts b/frontend/src/repositories/Document.ts index b100449..7796bdd 100644 --- a/frontend/src/repositories/Document.ts +++ b/frontend/src/repositories/Document.ts @@ -37,21 +37,24 @@ export class Doc { return this.url.replace(/^\/#/, '') } get img(): boolean { - const ext = this.name.split('.').pop()?.toLowerCase() || '' - return ['jpg', 'jpeg', 'png', 'gif', 'webp', 'avif', 'heic', 'heif', 'svg'].includes(ext) + // Folders cannot be images + if (this.dir) return false + return ['jpg', 'jpeg', 'png', 'gif', 'webp', 'avif', 'heic', 'heif', 'svg'].includes(this.ext) } get previewable(): boolean { + // Folders cannot be previewable + if (this.dir) return false if (this.img) return true - const ext = this.name.split('.').pop()?.toLowerCase() || '' // Not a comprehensive list, but good enough for now - return ['mp4', 'mkv', 'webm', 'ogg', 'mp3', 'flac', 'aac', 'pdf'].includes(ext) + return ['mp4', 'mkv', 'webm', 'ogg', 'mp3', 'flac', 'aac', 'pdf'].includes(this.ext) } get previewurl(): string { return this.url.replace(/^\/files/, '/preview') } get ext(): string { - const ext = this.name.split('.').pop() - return ext ? ext.toLowerCase() : '' + const dotIndex = this.name.lastIndexOf('.') + if (dotIndex === -1 || dotIndex === this.name.length - 1) return '' + return this.name.slice(dotIndex + 1).toLowerCase() } } export type errorEvent = { diff --git a/frontend/src/utils/index.ts b/frontend/src/utils/index.ts index 870ffca..4df8b8f 100644 --- a/frontend/src/utils/index.ts +++ b/frontend/src/utils/index.ts @@ -50,12 +50,11 @@ export function formatUnixDate(t: number) { } export function getFileExtension(filename: string) { - const parts = filename.split('.') - if (parts.length > 1) { - return parts[parts.length - 1] - } else { - return '' // No hay extensión + const dotIndex = filename.lastIndexOf('.') + if (dotIndex === -1 || dotIndex === filename.length - 1) { + return '' // No extension } + return filename.slice(dotIndex + 1) } interface FileTypes { [key: string]: string[] @@ -68,8 +67,9 @@ const filetypes: FileTypes = { } export function getFileType(name: string): string { - const ext = name.split('.').pop()?.toLowerCase() - if (!ext || ext.length === name.length) return 'unknown' + const dotIndex = name.lastIndexOf('.') + if (dotIndex === -1 || dotIndex === name.length - 1) return 'unknown' + const ext = name.slice(dotIndex + 1).toLowerCase() return Object.keys(filetypes).find(type => filetypes[type].includes(ext)) || 'unknown' }