-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetadata-builder.ts
More file actions
30 lines (27 loc) · 988 Bytes
/
Copy pathmetadata-builder.ts
File metadata and controls
30 lines (27 loc) · 988 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { extname } from 'node:path'
import { calculateFileHash } from '@backend/services/file-processor/hash-calculator'
import { extractImageMetadata } from '@backend/services/file-processor/image-extractor'
import { detectMimeType } from '@backend/services/file-processor/mime-detector'
import type { FileInput, FileMetadata } from '@backend/services/file-processor/types'
export const buildFileMetadata = async (
buffer: ArrayBuffer,
size: number,
filename: string,
uploadFile: FileInput,
): Promise<FileMetadata> => {
const extension = extname(filename)
const mimeType =
uploadFile.type === 'url' && uploadFile.mimeType
? uploadFile.mimeType
: await detectMimeType(Buffer.from(buffer), extension)
const hash = calculateFileHash(Buffer.from(buffer))
const imageData = await extractImageMetadata(Buffer.from(buffer), mimeType)
return {
filename,
size,
mimeType,
extension,
hash,
imageMetadata: imageData.imageMetadata,
}
}