Picsur/shared/src/util/parse-mime.ts

35 lines
998 B
TypeScript
Raw Normal View History

2022-06-05 10:20:16 +00:00
import {
2022-08-27 12:24:26 +00:00
Ext2FileType,
FileType,
Mime2FileType,
SupportedAnimFileTypes,
SupportedFileTypeCategory,
2022-09-06 14:32:16 +00:00
SupportedImageFileTypes,
2022-06-05 10:20:16 +00:00
} from '../dto/mimes.dto';
2022-08-27 12:24:26 +00:00
import { Fail, Failable, FT, HasFailed } from '../types';
2022-04-16 14:35:28 +00:00
2022-08-27 12:24:26 +00:00
export function ParseFileType(filetype: string): Failable<FileType> {
if (SupportedImageFileTypes.includes(filetype))
return { identifier: filetype, category: SupportedFileTypeCategory.Image };
2022-06-27 13:23:06 +00:00
2022-08-27 12:24:26 +00:00
if (SupportedAnimFileTypes.includes(filetype))
return {
identifier: filetype,
category: SupportedFileTypeCategory.Animation,
};
2022-06-27 13:23:06 +00:00
2022-08-27 12:24:26 +00:00
return Fail(FT.UsrValidation, 'Unsupported file type');
}
export function ParseExt2FileType(ext: string): Failable<FileType> {
const result = Ext2FileType(ext);
if (HasFailed(result)) return result;
return ParseFileType(result);
}
export function ParseMime2FileType(mime: string): Failable<FileType> {
const result = Mime2FileType(mime);
if (HasFailed(result)) return result;
return ParseFileType(result);
2022-04-16 14:35:28 +00:00
}