From fc797b82ed9b33745254a028c4a25d09042a3c48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=B8vring?= Date: Wed, 8 Nov 2023 10:21:35 +0100 Subject: [PATCH 1/2] Proxies all blob requests --- .../blob/[owner]/[repository]/[...path]/route.ts | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/app/api/github/blob/[owner]/[repository]/[...path]/route.ts b/src/app/api/github/blob/[owner]/[repository]/[...path]/route.ts index 9c8b595c..2dd2d532 100644 --- a/src/app/api/github/blob/[owner]/[repository]/[...path]/route.ts +++ b/src/app/api/github/blob/[owner]/[repository]/[...path]/route.ts @@ -17,17 +17,12 @@ export async function GET(req: NextRequest, { params }: { params: GetBlobParams }) const url = new URL(item.downloadURL) const imageRegex = /\.(jpg|jpeg|png|webp|avif|gif)$/; - + const file = await fetch(url).then(r => r.blob()) + const headers = new Headers() if (new RegExp(imageRegex).exec(path)) { - const file = await fetch(url).then(r => r.blob()); - const headers = new Headers(); const cacheExpirationInSeconds = 60 * 60 * 24 * 30 // 30 days - headers.set("Content-Type", "image/*"); - headers.set("Cache-Control", `max-age=${cacheExpirationInSeconds}`); - - return new NextResponse(file, { status: 200, headers }) - } else { - return NextResponse.redirect(url) + headers.set("Cache-Control", `max-age=${cacheExpirationInSeconds}`) } + return new NextResponse(file, { status: 200, headers }) } From 47d8a9d558500eeb52868f44ba7b1a46609ad0e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=B8vring?= Date: Wed, 8 Nov 2023 10:47:15 +0100 Subject: [PATCH 2/2] Moves /api/github/blob to /api/blob --- .../{github => }/blob/[owner]/[repository]/[...path]/route.ts | 0 src/features/projects/data/GitHubProjectDataSource.ts | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename src/app/api/{github => }/blob/[owner]/[repository]/[...path]/route.ts (100%) diff --git a/src/app/api/github/blob/[owner]/[repository]/[...path]/route.ts b/src/app/api/blob/[owner]/[repository]/[...path]/route.ts similarity index 100% rename from src/app/api/github/blob/[owner]/[repository]/[...path]/route.ts rename to src/app/api/blob/[owner]/[repository]/[...path]/route.ts diff --git a/src/features/projects/data/GitHubProjectDataSource.ts b/src/features/projects/data/GitHubProjectDataSource.ts index f001549e..e055b4e3 100644 --- a/src/features/projects/data/GitHubProjectDataSource.ts +++ b/src/features/projects/data/GitHubProjectDataSource.ts @@ -238,6 +238,6 @@ export default class GitHubProjectDataSource implements IProjectDataSource { } private getGitHubBlobURL(owner: string, repository: string, path: string, ref: string): string { - return `/api/github/blob/${owner}/${repository}/${path}?ref=${ref}` + return `/api/blob/${owner}/${repository}/${path}?ref=${ref}` } }