refactor(media): use asset-centric storage key layout

This commit is contained in:
2026-02-12 18:41:01 +01:00
parent 86a8af25d8
commit 3e4f0b6c75
12 changed files with 218 additions and 27 deletions

View File

@@ -1,7 +1,15 @@
import { randomUUID } from "node:crypto"
import path from "node:path"
const FALLBACK_EXTENSION = "bin"
const DEFAULT_VARIANT = "original"
type BuildMediaStorageKeyParams = {
tenantId: string
assetId: string
fileRole: string
variant?: string
fileName: string
}
function normalizeSegment(value: string): string {
return value
@@ -22,12 +30,20 @@ function extensionFromFilename(fileName: string): string {
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)
export function buildMediaStorageKey(params: BuildMediaStorageKeyParams): string {
const normalizedTenantId = normalizeSegment(params.tenantId) || "default"
const normalizedAssetId = normalizeSegment(params.assetId)
const normalizedFileRole = normalizeSegment(params.fileRole) || "original"
const normalizedVariant = normalizeSegment(params.variant ?? DEFAULT_VARIANT) || DEFAULT_VARIANT
const extension = extensionFromFilename(params.fileName)
const fileName = `${normalizedAssetId}__${normalizedVariant}.${extension}`
return [normalizedType, year, month, `${randomUUID()}.${extension}`].join("/")
return [
"tenant",
normalizedTenantId,
"asset",
normalizedAssetId,
normalizedFileRole,
fileName,
].join("/")
}