Skip to content
This repository has been archived by the owner on Jun 27, 2024. It is now read-only.

Commit

Permalink
feat: add index and 404 page looking for r2 handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ivy committed Aug 23, 2023
1 parent 6fe382a commit 0aa76f3
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions ext/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,35 @@ async function handleR2Files(
? app.request.pathname.substring(prefix.length + 1)
: app.request.pathname,
)

if (!object) return new Response(null, { status: 404 })

return new Response(object.body as ReadableStream, {
headers: {
...(serve.etag !== false && { etag: object.httpEtag }),
'cache-control': serve.cacheControl ?? 's-maxage=300', // 5m
},
})
if (object) {
return new Response(object.body as ReadableStream, {
headers: {
...(serve.etag !== false && { etag: object.httpEtag }),
'cache-control': serve.cacheControl ?? 's-maxage=300', // 5m
},
})
} else {
const indexPath = join(app.request.pathname, 'index.html')
const indexObject = await bucket.get(indexPath)
if (indexObject) {
return new Response(indexObject.body as ReadableStream, {
headers: {
...(serve.etag !== false && { etag: indexObject.httpEtag }),
'cache-control': serve.cacheControl ?? 's-maxage=300', // 5m
},
})
} else {
const errorPath = join(prefix, '404.html')
const errorObject = await bucket.get(errorPath)
if (errorObject) {
return new Response(errorObject.body as ReadableStream, {
headers: {
'cache-control': serve.cacheControl ?? 's-maxage=300', // 5m
},
})
}
}
}
}

async function handleFsFiles(
Expand Down

0 comments on commit 0aa76f3

Please sign in to comment.