refactor(server): calculate asset type server-side

This commit is contained in:
Jason Rasmussen
2023-07-10 15:12:43 -04:00
parent 6180828ed2
commit 120727889f
13 changed files with 74 additions and 253 deletions
+15 -1
View File
@@ -1,3 +1,4 @@
import { AssetType } from '@app/infra/entities';
import { BadRequestException } from '@nestjs/common';
import { extname } from 'node:path';
import pkg from 'src/../../package.json';
@@ -91,6 +92,8 @@ const sidecar: Record<string, string> = {
const isType = (filename: string, lookup: Record<string, string>) => !!lookup[extname(filename).toLowerCase()];
const getType = (filename: string, lookup: Record<string, string>) => lookup[extname(filename).toLowerCase()];
const lookup = (filename: string) =>
getType(filename, { ...image, ...video, ...sidecar }) || 'application/octet-stream';
export const mimeTypes = {
image,
@@ -102,5 +105,16 @@ export const mimeTypes = {
isProfile: (filename: string) => isType(filename, profile),
isSidecar: (filename: string) => isType(filename, sidecar),
isVideo: (filename: string) => isType(filename, video),
lookup: (filename: string) => getType(filename, { ...image, ...video, ...sidecar }) || 'application/octet-stream',
lookup,
assetType: (filename: string) => {
const contentType = lookup(filename).split('/')[0];
switch (contentType) {
case 'image':
return AssetType.IMAGE;
case 'video':
return AssetType.VIDEO;
default:
return AssetType.OTHER;
}
},
};