Skip to content

Commit

Permalink
fix: added cache headers to file backend (#623)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajitsinghkaler authored Jan 24, 2025
1 parent f4ae982 commit 6f58fe2
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/storage/backend/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,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)
Expand Down

0 comments on commit 6f58fe2

Please sign in to comment.