Files
ezbookkeeping/src/core/file.ts
T

105 lines
3.3 KiB
TypeScript

export class KnownFileType {
private static readonly allInstancesByExtension: Record<string, KnownFileType> = {};
public static readonly JSON = new KnownFileType('json', 'application/json');
public static readonly CSV = new KnownFileType('csv', 'text/csv');
public static readonly TSV = new KnownFileType('tsv', 'text/tab-separated-values');
public static readonly MARKDOWN = new KnownFileType('md', 'text/markdown');
public static readonly JS = new KnownFileType('js', 'application/javascript');
public readonly extension: string;
public readonly contentType: string;
private constructor(extension: string, contentType: string) {
this.extension = extension;
this.contentType = contentType;
KnownFileType.allInstancesByExtension[extension] = this;
}
public formatFileName(fileName: string): string {
if (fileName.endsWith(`.${this.extension}`)) {
return fileName;
}
return `${fileName}.${this.extension}`;
}
public createBlob(content: string): Blob {
return new Blob([content], {
type: this.contentType,
});
}
public createFile(content: string, fileName: string): File {
return new File([content], this.formatFileName(fileName), {
type: this.contentType,
});
}
public static parse(extension: string): KnownFileType | undefined {
return KnownFileType.allInstancesByExtension[extension];
}
}
export interface ImportFileTypeAndExtensions {
readonly type: string;
readonly extensions?: string;
}
export interface ImportFileCategoryAndTypes {
readonly categoryName: string;
readonly fileTypes: ImportFileType[];
}
export interface ImportFileType extends ImportFileTypeAndExtensions {
readonly type: string;
readonly name: string;
readonly extensions: string;
readonly subTypes?: ImportFileTypeSubType[];
readonly supportedEncodings?: string[];
readonly dataFromTextbox?: boolean;
readonly document?: {
readonly supportMultiLanguages: boolean | string;
readonly anchor: string;
};
}
export interface ImportFileTypeSubType extends ImportFileTypeAndExtensions {
readonly type: string;
readonly name: string;
readonly extensions?: string;
}
export interface LocalizedImportFileCategoryAndTypes {
readonly displayCategoryName: string;
readonly fileTypes: LocalizedImportFileType[];
}
export interface LocalizedImportFileType extends ImportFileTypeAndExtensions {
readonly type: string;
readonly displayName: string;
readonly extensions: string;
readonly subTypes?: LocalizedImportFileTypeSubType[];
readonly supportedEncodings?: LocalizedImportFileTypeSupportedEncodings[];
readonly dataFromTextbox?: boolean;
readonly document?: LocalizedImportFileDocument;
}
export interface LocalizedImportFileTypeSubType extends ImportFileTypeAndExtensions {
readonly type: string;
readonly displayName: string;
readonly extensions?: string;
}
export interface LocalizedImportFileTypeSupportedEncodings {
readonly encoding: string;
readonly displayName: string;
}
export interface LocalizedImportFileDocument {
readonly language: string;
readonly displayLanguageName: string;
readonly anchor: string;
}