Merge pull request #101 from bknd-io/fix/add-cache-directives-for-media

added missing cache directives to media adapter serving
This commit is contained in:
dswbx
2025-03-03 07:15:03 +01:00
committed by GitHub
4 changed files with 32 additions and 6 deletions

View File

@@ -18,6 +18,16 @@ export function pickHeaders(headers: Headers, keys: string[]): Record<string, st
return res;
}
export function pickHeaders2(headers: Headers, keys: string[]): Headers {
const newHeaders = new Headers();
for (const key of keys) {
if (headers.has(key)) {
newHeaders.set(key, headers.get(key)!);
}
}
return newHeaders;
}
export const replaceUrlParam = (urlString: string, params: Record<string, string>) => {
let newString = urlString;
for (const [k, v] of Object.entries(params)) {

View File

@@ -1,5 +1,5 @@
import { isDebug, tbValidator as tb } from "core";
import { HttpStatus, Type, getFileFromContext } from "core/utils";
import { HttpStatus, Type, getFileFromContext, headersToObject } from "core/utils";
import type { StorageAdapter } from "media";
import { StorageEvents, getRandomizedFilename } from "media";
import { Controller } from "modules/Controller";
@@ -36,6 +36,7 @@ export class MediaController extends Controller {
});
// get file by name
// @todo: implement more aggressive cache? (configurable)
hono.get("/file/:filename", async (c) => {
const { filename } = c.req.param();
if (!filename) {
@@ -43,7 +44,16 @@ export class MediaController extends Controller {
}
await this.getStorage().emgr.emit(new StorageEvents.FileAccessEvent({ name: filename }));
return await this.getStorageAdapter().getObject(filename, c.req.raw.headers);
const res = await this.getStorageAdapter().getObject(filename, c.req.raw.headers);
const headers = new Headers(res.headers);
headers.set("Cache-Control", "public, max-age=31536000, immutable");
return new Response(res.body, {
status: res.status,
statusText: res.statusText,
headers,
});
});
// delete a file by name

View File

@@ -7,7 +7,7 @@ import type {
PutObjectRequest,
} from "@aws-sdk/client-s3";
import { AwsClient, isDebug } from "core";
import { type Static, Type, isFile, parse, pickHeaders } from "core/utils";
import { type Static, Type, isFile, parse, pickHeaders, pickHeaders2 } from "core/utils";
import { transform } from "lodash-es";
import type { FileBody, FileListObject, StorageAdapter } from "../Storage";
@@ -171,7 +171,13 @@ export class StorageS3Adapter extends AwsClient implements StorageAdapter {
const url = this.getUrl(key);
const res = await this.fetch(url, {
method: "GET",
headers: pickHeaders(headers, ["range"]),
headers: pickHeaders2(headers, [
"range",
"if-none-match",
"accept-encoding",
"accept",
"if-modified-since",
]),
});
// Response has to be copied, because of middlewares that might set headers

View File

@@ -51,9 +51,9 @@ export default {
if (firstStart) {
console.log("[DB]", credentials);
firstStart = false;
console.log("\n[APP ROUTES]");
/*console.log("\n[APP ROUTES]");
showRoutes(app.server);
console.log("-------\n");
console.log("-------\n");*/
}
}