-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: if a file is greater than 1mb, upload to S3 (#580)
- Loading branch information
1 parent
94fff24
commit f3a844c
Showing
11 changed files
with
204 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
export function isNonNullish<T>(x: T | null | undefined): x is T { | ||
return x != null; | ||
} | ||
|
||
export function assertNonNullish<T>(x: T | null | undefined, message?: string): asserts x is T { | ||
if (x == null) { | ||
throw new Error(message ?? "Value is null or undefined"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { createGetSignedUrl, createPutSignedUrl } from "../../../utils/createSignedUrl"; | ||
|
||
export const runtime = "edge"; | ||
export const maxDuration = 5; | ||
|
||
export default async function GET(req: NextRequest): Promise<NextResponse> { | ||
if (req.method !== "GET") { | ||
return new NextResponse(null, { status: 405 }); | ||
} | ||
|
||
const domain = req.nextUrl.hostname; | ||
const time: string = new Date().toISOString(); | ||
const file = req.nextUrl.searchParams.get("file"); | ||
|
||
if (file == null) { | ||
return new NextResponse(null, { status: 400 }); | ||
} | ||
|
||
try { | ||
const key = constructS3Key(domain, time, file); | ||
const [put, get] = await Promise.all([ | ||
await createPutSignedUrl(key, 60), // 1 minute | ||
await createGetSignedUrl(key, 60 * 5), // 5 minutes | ||
]); | ||
return NextResponse.json({ put, get }, { headers: { "Cache-Control": "public, max-age=60" } }); | ||
} catch (err) { | ||
// eslint-disable-next-line no-console | ||
console.error("Failed to create signed URL", err); | ||
return new NextResponse(null, { status: 500 }); | ||
} | ||
} | ||
|
||
function constructS3Key(domain: string, time: string, file: string): string { | ||
return `${domain}/user-upload/${time}/${file}`; | ||
} |
Oops, something went wrong.