Skip to content

Commit

Permalink
add enum for bucket name
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobc2700 committed Jul 15, 2024
1 parent fc9b4d8 commit 643bf2d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 79 deletions.
69 changes: 0 additions & 69 deletions src/app.ts

This file was deleted.

7 changes: 6 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ export const Environment = z.enum(["PRODUCTION", "DEVELOPMENT", "TESTING"]);

export const MailingListName = z.enum(["rp_interest"]);

// Native enum for bucket names
export enum BucketName {
RP_2024_RESUMES = "rp-2024-resumes",
RP_2024_SPEAKERS = "rp-2024-speakers",
}

export const Config = {
DEFAULT_APP_PORT: 3000,
ALLOWED_CORS_ORIGIN_PATTERNS: [
Expand Down Expand Up @@ -71,7 +77,6 @@ export const Config = {

S3_ACCESS_KEY: getEnv("S3_ACCESS_KEY"),
S3_SECRET_KEY: getEnv("S3_SECRET_KEY"),
S3_BUCKET_NAME: getEnv("S3_BUCKET_NAME"),
S3_REGION: getEnv("S3_REGION"),
MAX_RESUME_SIZE_BYTES: 6 * 1024 * 1024,
RESUME_URL_EXPIRY_SECONDS: 60,
Expand Down
21 changes: 17 additions & 4 deletions src/services/s3/s3-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Role } from "../auth/auth-models";
import { S3 } from "@aws-sdk/client-s3";
import { getResumeUrl, postResumeUrl } from "./s3-utils";
import BatchResumeDownloadValidator from "./s3-schema";
import { BucketName } from "../../config";

const s3Router: Router = Router();

Expand All @@ -21,7 +22,11 @@ s3Router.get(
const userId: string = payload.userId;

try {
const { url, fields } = await postResumeUrl(userId, s3);
const { url, fields } = await postResumeUrl(
userId,
s3,
BucketName.RP_2024_RESUMES
);
return res.status(StatusCodes.OK).send({ url, fields });
} catch (error) {
next(error);
Expand All @@ -40,7 +45,11 @@ s3Router.get(
const s3 = res.locals.s3 as S3;

try {
const downloadUrl = await getResumeUrl(userId, s3);
const downloadUrl = await getResumeUrl(
userId,
s3,
BucketName.RP_2024_RESUMES
);
return res.status(StatusCodes.OK).send({ url: downloadUrl });
} catch (error) {
next(error);
Expand All @@ -57,7 +66,11 @@ s3Router.get(
const s3 = res.locals.s3 as S3;

try {
const downloadUrl = await getResumeUrl(userId, s3);
const downloadUrl = await getResumeUrl(
userId,
s3,
BucketName.RP_2024_RESUMES
);
return res.status(StatusCodes.OK).send({ url: downloadUrl });
} catch (error) {
next(error);
Expand All @@ -76,7 +89,7 @@ s3Router.get(
const { userIds } = BatchResumeDownloadValidator.parse(req.body);

const batchDownloadPromises = userIds.map((userId) =>
getResumeUrl(userId, s3)
getResumeUrl(userId, s3, BucketName.RP_2024_RESUMES)
.then((url) => ({ userId, url: url }))
.catch(() => ({ userId, url: null }))
);
Expand Down
18 changes: 13 additions & 5 deletions src/services/s3/s3-utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { GetObjectCommand, S3 } from "@aws-sdk/client-s3";
import Config from "../../config";
import Config, { BucketName } from "../../config";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import { createPresignedPost } from "@aws-sdk/s3-presigned-post";

export async function postResumeUrl(userId: string, client: S3) {
export async function postResumeUrl(
userId: string,
client: S3,
bucketName: BucketName
) {
const { url, fields } = await createPresignedPost(client, {
Bucket: Config.S3_BUCKET_NAME,
Bucket: bucketName,
Key: `${userId}.pdf`,
Conditions: [
["content-length-range", 0, Config.MAX_RESUME_SIZE_BYTES], // 6 MB max
Expand All @@ -20,9 +24,13 @@ export async function postResumeUrl(userId: string, client: S3) {
return { url, fields };
}

export async function getResumeUrl(userId: string, client: S3) {
export async function getResumeUrl(
userId: string,
client: S3,
bucketName: BucketName
) {
const command = new GetObjectCommand({
Bucket: Config.S3_BUCKET_NAME,
Bucket: bucketName,
Key: `${userId}.pdf`,
});

Expand Down

0 comments on commit 643bf2d

Please sign in to comment.