Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(media): base id gen on project ID + content hash (#5222)

Hassieb Pakzad25a559293b86
diff --git a/web/src/pages/api/public/media/index.ts b/web/src/pages/api/public/media/index.ts
index 752a4a9144..2e12a7ccdb 100644
--- a/web/src/pages/api/public/media/index.ts
+++ b/web/src/pages/api/public/media/index.ts
@@ -1,4 +1,5 @@
import { randomUUID } from "crypto";
+import crypto from "node:crypto";
import { env } from "@/src/env.mjs";
import { getFileExtensionFromContentType } from "@/src/features/media/server/getFileExtensionFromContentType";
@@ -80,7 +81,7 @@ export default withMiddlewares({
};
}
- const mediaId = existingMedia?.id ?? randomUUID();
+ const mediaId = getMediaId({ projectId, sha256Hash });
if (!env.LANGFUSE_S3_MEDIA_UPLOAD_BUCKET)
throw new InternalServerError(
@@ -175,3 +176,13 @@ function getBucketPath(params: {
return `${prefix}${projectId}/${mediaId}.${fileExtension}`;
}
+
+function getMediaId(params: { projectId: string; sha256Hash: string }) {
+ const { projectId, sha256Hash } = params;
+
+ return crypto
+ .createHash("sha256")
+ .update(projectId + sha256Hash, "utf8")
+ .digest("base64url")
+ .slice(0, 22);
+}