-
Notifications
You must be signed in to change notification settings - Fork 52
Uploads #171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Uploads #171
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9b2dc07
feat(upload): add multer s3 image upload endpoint with 5mb limit
inteee 26fa60d
docs(upload): document s3 environment variables and api contract
inteee 761ca9e
Merge branch 'main' into uploads
inteee ab3c605
fix(upload): enforce image mime validation and document allowed formats
inteee 56d506f
test(upload): add tests for file upload validation and error handling
inteee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,6 +1,8 @@ | ||
| import { Router } from "express" | ||
| import { healthRouter } from "./health.routes.js" | ||
| import { uploadRouter } from "./upload.routes.js" | ||
|
|
||
| export const apiRouter = Router() | ||
|
|
||
| apiRouter.use("/health", healthRouter) | ||
| apiRouter.use("/upload", uploadRouter) |
This file contains hidden or 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,31 @@ | ||
| import { Router } from "express" | ||
| import multer from "multer" | ||
| import { uploadImageToS3 } from "../services/upload.service.js" | ||
|
|
||
| const upload = multer({ | ||
| storage: multer.memoryStorage(), | ||
| limits: { fileSize: 5 * 1024 * 1024 }, | ||
| }) | ||
|
|
||
| export const uploadRouter = Router() | ||
|
|
||
| uploadRouter.post("/", upload.single("file"), async (req, res, next) => { | ||
| try { | ||
| if (!req.file) { | ||
| res.status(400).json({ | ||
| error: "Bad Request", | ||
| message: "Missing image file in form-data field 'file'", | ||
| }) | ||
| return | ||
| } | ||
|
|
||
| const { url } = await uploadImageToS3(req.file) | ||
|
|
||
| res.status(201).json({ | ||
| ok: true, | ||
| url, | ||
| }) | ||
| } catch (error) { | ||
| next(error) | ||
| } | ||
| }) | ||
This file contains hidden or 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,47 @@ | ||
| import { randomUUID } from "node:crypto" | ||
| import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3" | ||
| import { env } from "../config/env.js" | ||
|
|
||
| function ensureS3Config() { | ||
| if (!env.AWS_REGION || !env.AWS_ACCESS_KEY_ID || !env.AWS_SECRET_ACCESS_KEY || !env.AWS_S3_BUCKET) { | ||
| throw new Error("S3 config missing. Set AWS_REGION, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_S3_BUCKET.") | ||
| } | ||
| } | ||
|
|
||
| function getS3Client() { | ||
| ensureS3Config() | ||
| return new S3Client({ | ||
| region: env.AWS_REGION, | ||
| credentials: { | ||
| accessKeyId: env.AWS_ACCESS_KEY_ID!, | ||
| secretAccessKey: env.AWS_SECRET_ACCESS_KEY!, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| function getPublicUrl(key: string): string { | ||
| if (env.AWS_S3_PUBLIC_BASE_URL) { | ||
| return `${env.AWS_S3_PUBLIC_BASE_URL.replace(/\/$/, "")}/${key}` | ||
| } | ||
| return `https://${env.AWS_S3_BUCKET}.s3.${env.AWS_REGION}.amazonaws.com/${key}` | ||
| } | ||
|
|
||
| export async function uploadImageToS3(file: Express.Multer.File): Promise<{ url: string; key: string }> { | ||
| const extension = file.originalname.includes(".") ? file.originalname.split(".").pop() : "bin" | ||
| const key = `uploads/${new Date().toISOString().slice(0, 10)}/${randomUUID()}.${extension}` | ||
|
|
||
| const client = getS3Client() | ||
| const command = new PutObjectCommand({ | ||
| Bucket: env.AWS_S3_BUCKET, | ||
| Key: key, | ||
| Body: file.buffer, | ||
| ContentType: file.mimetype, | ||
| }) | ||
|
|
||
| await client.send(command) | ||
|
|
||
| return { | ||
| url: getPublicUrl(key), | ||
| key, | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add strict MIME allowlist enforcement in the upload route (jpeg/png/webp check), focus on