feat(document-service): add type-safe file extension handling with FileFormat union

- Introduced `FileFormat` union type to restrict valid document extensions.
- Added `validExtensions` array for strict extension checking in `Doc.extension` getter.
- Ensured `Doc.iconSrc` reflects only recognized file types or defaults to 'unknown'.
- Improved type safety and consistency in extension handling.
This commit is contained in:
tekh 2025-07-14 10:28:11 +02:00
parent 49667d7fe9
commit 3e7a22f9e2

View File

@ -1,5 +1,27 @@
import { _documents } from "src/_mock" import { _documents } from "src/_mock"
export type FileFormat =
| 'pdf'
| 'docx'
| 'xlsx'
| 'csv'
| 'pptx'
| 'txt'
| 'json'
| 'xml'
| 'html'
| 'jpg'
| 'png'
| 'svg'
| 'zip'
| 'md';
const validExtensions: FileFormat[] = [
'pdf', 'docx', 'xlsx', 'csv', 'pptx',
'txt', 'json', 'xml', 'html', 'jpg',
'png', 'svg', 'zip', 'md'
];
export class Doc { export class Doc {
static map(source?: Partial<Doc>): Doc { static map(source?: Partial<Doc>): Doc {
@ -27,10 +49,13 @@ export class Doc {
return [who, when].filter(Boolean).join(separator); return [who, when].filter(Boolean).join(separator);
} }
get extension(): string | undefined { get extension(): FileFormat | undefined {
const parts = this.name.split('.'); const parts = this.name.split('.');
if (parts.length > 1 && parts[parts.length - 1].trim() !== '') { if (parts.length > 1 && parts[parts.length - 1].trim() !== '') {
return parts[parts.length - 1].toLowerCase(); const ext = parts[parts.length - 1].toLowerCase();
if (validExtensions.includes(ext as FileFormat))
return ext as FileFormat;
} }
return undefined; return undefined;
} }