|
1 | | -import { NextRequest, NextResponse } from "next/server" |
2 | | -import { session, userGitHubClient } from "@/composition" |
3 | | -import { makeUnauthenticatedAPIErrorResponse } from "@/common" |
4 | | -import { revalidatePath } from "next/cache" |
5 | | - |
| 1 | +import { NextRequest, NextResponse } from "next/server"; |
| 2 | +import { session, userGitHubClient } from "@/composition"; |
| 3 | +import { makeUnauthenticatedAPIErrorResponse } from "@/common"; |
6 | 4 |
|
7 | 5 | interface GetBlobParams { |
8 | | - owner: string |
9 | | - repository: string |
10 | | - path: [string] |
| 6 | + owner: string; |
| 7 | + repository: string; |
| 8 | + path: [string]; |
11 | 9 | } |
12 | 10 |
|
13 | | -export async function GET(req: NextRequest, { params }: { params: Promise<GetBlobParams> } ) { |
14 | | - const isAuthenticated = await session.getIsAuthenticated() |
| 11 | +export async function GET( |
| 12 | + req: NextRequest, |
| 13 | + { params }: { params: Promise<GetBlobParams> } |
| 14 | +) { |
| 15 | + const isAuthenticated = await session.getIsAuthenticated(); |
15 | 16 | if (!isAuthenticated) { |
16 | | - return makeUnauthenticatedAPIErrorResponse() |
| 17 | + return makeUnauthenticatedAPIErrorResponse(); |
17 | 18 | } |
18 | | - const { path: paramsPath, owner, repository } = await params |
19 | | - const path = paramsPath.join("/") |
| 19 | + const { path: paramsPath, owner, repository } = await params; |
| 20 | + const path = paramsPath.join("/"); |
20 | 21 | const item = await userGitHubClient.getRepositoryContent({ |
21 | 22 | repositoryOwner: owner, |
22 | 23 | repositoryName: repository, |
23 | 24 | path: path, |
24 | | - ref: req.nextUrl.searchParams.get("ref") ?? undefined |
25 | | - }) |
26 | | - const url = new URL(item.downloadURL) |
| 25 | + ref: req.nextUrl.searchParams.get("ref") ?? undefined, |
| 26 | + }); |
| 27 | + const url = new URL(item.downloadURL); |
27 | 28 | const imageRegex = /\.(jpg|jpeg|png|webp|avif|gif)$/; |
28 | | - const res = await fetch(url, { next: { revalidate: 6000 } }) |
29 | | - const file = await res.blob() |
30 | | - revalidatePath('/(authed)/projects') |
31 | | - const headers = new Headers() |
32 | | - if (res.status !== 200) { |
33 | | - headers.set("Content-Type", "text/plain"); |
34 | | - headers.set("Cache-Control", `max-age=3000`) |
35 | | - } |
| 29 | + const res = await fetch(url); |
| 30 | + const file = await res.blob(); |
| 31 | + |
| 32 | + const headers = new Headers(); |
36 | 33 | if (new RegExp(imageRegex).exec(path)) { |
37 | | - const cacheExpirationInSeconds = 60 * 60 * 24 * 30 // 30 days |
| 34 | + const cacheExpirationInSeconds = 60 * 60 * 24 * 30; // 30 days |
38 | 35 | headers.set("Content-Type", "image/*"); |
39 | | - headers.set("Cache-Control", `max-age=${cacheExpirationInSeconds}`) |
40 | | - } |
41 | | - return new NextResponse(file, { status: 200, headers }) |
| 36 | + headers.set("Cache-Control", `max-age=${cacheExpirationInSeconds}`); |
| 37 | + } else { |
| 38 | + headers.set("Content-Type", "text/plain"); |
| 39 | + } |
| 40 | + return new NextResponse(file, { status: 200, headers }); |
42 | 41 | } |
0 commit comments