23 lines
650 B
TypeScript
23 lines
650 B
TypeScript
import { FileEntry } from '#base_modules/FileManager/types/entries'
|
|
|
|
const PREVIEWABLE_TEXT_EXTENSIONS = ['txt', 'md', 'log', 'json', 'csv']
|
|
|
|
export default class FilePreviewService {
|
|
public async withPreview(
|
|
entry: FileEntry,
|
|
getContent: () => Promise<string>
|
|
): Promise<FileEntry & { content?: string }> {
|
|
if (!this.isPreviewableTextFile(entry.name)) return entry
|
|
|
|
const content = await getContent()
|
|
|
|
return { ...entry, content }
|
|
}
|
|
|
|
private isPreviewableTextFile(name: string): boolean {
|
|
const fileExt = name.split('.').pop()?.toLowerCase()
|
|
|
|
return PREVIEWABLE_TEXT_EXTENSIONS.includes(fileExt as string)
|
|
}
|
|
}
|