From 37aa2a158575895c519356f3a7cc40293546aa14 Mon Sep 17 00:00:00 2001 From: Ajit Singh Date: Wed, 22 Jan 2025 12:41:17 +0530 Subject: [PATCH] fix: added cache headers to file backend --- src/storage/backend/file.ts | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/storage/backend/file.ts b/src/storage/backend/file.ts index b44402c3..48cf2959 100644 --- a/src/storage/backend/file.ts +++ b/src/storage/backend/file.ts @@ -88,6 +88,41 @@ export class FileBackend implements StorageBackendAdapter { const lastModified = new Date(0) lastModified.setUTCMilliseconds(data.mtimeMs) + if (headers?.ifNoneMatch && headers.ifNoneMatch === eTag) { + return { + metadata: { + cacheControl: cacheControl || 'no-cache', + mimetype: contentType || 'application/octet-stream', + lastModified: lastModified, + httpStatusCode: 304, + size: data.size, + eTag, + contentLength: 0, + }, + body: undefined, + httpStatusCode: 304, + } + } + + if (headers?.ifModifiedSince) { + const ifModifiedSince = new Date(headers.ifModifiedSince) + if (lastModified <= ifModifiedSince) { + return { + metadata: { + cacheControl: cacheControl || 'no-cache', + mimetype: contentType || 'application/octet-stream', + lastModified: lastModified, + httpStatusCode: 304, + size: data.size, + eTag, + contentLength: 0, + }, + body: undefined, + httpStatusCode: 304, + } + } + } + if (headers?.range) { const parts = headers.range.replace(/bytes=/, '').split('-') const startRange = parseInt(parts[0], 10)