feat(media): support local and s3 upload providers

This commit is contained in:
2026-02-12 12:02:31 +01:00
parent 5becba602c
commit 19738b77d8
11 changed files with 407 additions and 41 deletions

View File

@@ -0,0 +1,33 @@
import { randomUUID } from "node:crypto"
import path from "node:path"
const FALLBACK_EXTENSION = "bin"
function normalizeSegment(value: string): string {
return value
.toLowerCase()
.replace(/[^a-z0-9._-]+/g, "-")
.replace(/^-+|-+$/g, "")
}
function extensionFromFilename(fileName: string): string {
const extension = path.extname(fileName).slice(1)
if (!extension) {
return FALLBACK_EXTENSION
}
const normalized = normalizeSegment(extension)
return normalized.length > 0 ? normalized : FALLBACK_EXTENSION
}
export function buildMediaStorageKey(mediaType: string, fileName: string): string {
const now = new Date()
const year = String(now.getUTCFullYear())
const month = String(now.getUTCMonth() + 1).padStart(2, "0")
const normalizedType = normalizeSegment(mediaType) || "generic"
const extension = extensionFromFilename(fileName)
return [normalizedType, year, month, `${randomUUID()}.${extension}`].join("/")
}