feat: add etag to object response

This commit is contained in:
Huskydog9988
2025-04-07 19:19:45 -04:00
parent 66d1413eb5
commit 9d943bc5dc
2 changed files with 34 additions and 0 deletions

View File

@ -11,6 +11,16 @@ export default defineEventHandler(async (h3) => {
if (!object)
throw createError({ statusCode: 404, statusMessage: "Object not found" });
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/ETag
const etagValue = h3.headers.get("If-None-Match");
if (etagValue !== null) {
// would compare if etag is valid, but objects should never change
setResponseStatus(h3, 304);
return null;
}
// just return object id has etag since object should never change
setHeader(h3, "ETag", id);
setHeader(h3, "Content-Type", object.mime);
setHeader(
h3,

View File

@ -0,0 +1,24 @@
import aclManager from "~/server/internal/acls";
import objectHandler from "~/server/internal/objects";
// this request method is purely used by the browser to check if etag values are still valid
export default defineEventHandler(async (h3) => {
const id = getRouterParam(h3, "id");
if (!id) throw createError({ statusCode: 400, statusMessage: "Invalid ID" });
const userId = await aclManager.getUserIdACL(h3, ["object:read"]);
const object = await objectHandler.fetchWithPermissions(id, userId);
if (!object)
throw createError({ statusCode: 404, statusMessage: "Object not found" });
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/ETag
const etagValue = h3.headers.get("If-None-Match");
if (etagValue !== null) {
// would compare if etag is valid, but objects should never change
setResponseStatus(h3, 304);
return null;
}
return null;
});