Refactor R2 key prefix for deterministic identifiers

This commit is contained in:
thePR0M3TH3AN committed 2026-01-26 12:47:05 -05:00
1 parent 00b0e85438
commit c02db755ac
6 files changed
+42 -12

No files matched your search

+6 -5
View File
@@ -211,13 +211,13 @@ function guessExtension(file) {
}
}
export function buildR2Key(npub, file) {
const now = new Date();
const year = now.getUTCFullYear();
const month = String(now.getUTCMonth() + 1).padStart(2, "0");
export function buildR2Key(npub, file, identifier = "") {
const safeNpub = String(npub || "")
.toLowerCase()
.replace(/[^a-z0-9]/g, "");
const safeIdentifier = String(identifier || "")
.toLowerCase()
.replace(/[^a-z0-9]/g, "");
const baseName = typeof file?.name === "string" ? file.name : "video";
const withoutExt = baseName.replace(/\.[^/.]+$/, "");
const slug = withoutExt
@@ -226,7 +226,8 @@ export function buildR2Key(npub, file) {
.replace(/^-+|-+$/g, "");
const safeSlug = slug || "video";
const ext = guessExtension(file);
return `u/${safeNpub}/${year}/${month}/${safeSlug}.${ext}`;
const namespace = safeIdentifier || "uploads";
return `u/${safeNpub}/${namespace}/${safeSlug}.${ext}`;
}
export function buildPublicUrl(baseUrl, key) {