From 6d821d6af5274464b724d550029b78b2eba6d54c Mon Sep 17 00:00:00 2001 From: Billy <30775249+InternalLoss@users.noreply.github.com> Date: Sat, 18 Jan 2025 17:52:58 +0000 Subject: [PATCH 1/2] Mii uploads: use config for bucket name --- example.env | 1 + src/config-manager.ts | 6 ++++++ src/models/pnid.ts | 8 ++++---- src/types/common/config.ts | 1 + 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/example.env b/example.env index 626100d..8e07339 100644 --- a/example.env +++ b/example.env @@ -10,6 +10,7 @@ PN_ACT_CONFIG_EMAIL_SES_REGION=region PN_ACT_CONFIG_EMAIL_SES_ACCESS_KEY=ACCESS_KEY PN_ACT_CONFIG_EMAIL_SES_SECRET_KEY=ACCESS_SECRET PN_ACT_CONFIG_EMAIL_FROM=Company Name +PN_ACT_CONFIG_S3_BUCKET=BUCKET_NAME PN_ACT_CONFIG_S3_ENDPOINT=nyc3.digitaloceanspaces.com PN_ACT_CONFIG_S3_ACCESS_KEY=ACCESS_KEY PN_ACT_CONFIG_S3_ACCESS_SECRET=ACCESS_SECRET diff --git a/src/config-manager.ts b/src/config-manager.ts index 7d98100..dc3080d 100644 --- a/src/config-manager.ts +++ b/src/config-manager.ts @@ -54,6 +54,7 @@ export const config: Config = { from: process.env.PN_ACT_CONFIG_EMAIL_FROM || '' }, s3: { + bucket: process.env.PN_ACT_CONFIG_S3_BUCKET || '', endpoint: process.env.PN_ACT_CONFIG_S3_ENDPOINT || '', key: process.env.PN_ACT_CONFIG_S3_ACCESS_KEY || '', secret: process.env.PN_ACT_CONFIG_S3_ACCESS_SECRET || '', @@ -182,6 +183,11 @@ if (!config.hcaptcha.secret) { disabledFeatures.captcha = true; } +if (!config.s3.bucket) { + LOG_WARN('Failed to find S3 bucket config. Disabling feature. To enable feature set the PN_ACT_CONFIG_S3_BUCKET environment variable'); + disabledFeatures.s3 = true; +} + if (!config.s3.endpoint) { LOG_WARN('Failed to find S3 endpoint config. Disabling feature. To enable feature set the PN_ACT_CONFIG_S3_ENDPOINT environment variable'); disabledFeatures.s3 = true; diff --git a/src/models/pnid.ts b/src/models/pnid.ts index 5e2cc34..3fe65cd 100644 --- a/src/models/pnid.ts +++ b/src/models/pnid.ts @@ -198,8 +198,8 @@ PNIDSchema.method('generateMiiImages', async function generateMiiImages(): Promi const userMiiKey = `mii/${this.pid}`; - await uploadCDNAsset('pn-cdn', `${userMiiKey}/standard.tga`, tga, 'public-read'); - await uploadCDNAsset('pn-cdn', `${userMiiKey}/normal_face.png`, miiStudioNormalFaceImageData, 'public-read'); + await uploadCDNAsset(`${config.s3.bucket}`, `${userMiiKey}/standard.tga`, tga, 'public-read'); + await uploadCDNAsset(`${config.s3.bucket}`, `${userMiiKey}/normal_face.png`, miiStudioNormalFaceImageData, 'public-read'); const expressions = ['frustrated', 'smile_open_mouth', 'wink_left', 'sorrow', 'surprise_open_mouth']; for (const expression of expressions) { @@ -210,7 +210,7 @@ PNIDSchema.method('generateMiiImages', async function generateMiiImages(): Promi instanceCount: 1, }); const miiStudioExpressionImageData = await got(miiStudioExpressionUrl).buffer(); - await uploadCDNAsset('pn-cdn', `${userMiiKey}/${expression}.png`, miiStudioExpressionImageData, 'public-read'); + await uploadCDNAsset(`${config.s3.bucket}`, `${userMiiKey}/${expression}.png`, miiStudioExpressionImageData, 'public-read'); } const miiStudioBodyUrl = mii.studioUrl({ @@ -219,7 +219,7 @@ PNIDSchema.method('generateMiiImages', async function generateMiiImages(): Promi instanceCount: 1, }); const miiStudioBodyImageData = await got(miiStudioBodyUrl).buffer(); - await uploadCDNAsset('pn-cdn', `${userMiiKey}/body.png`, miiStudioBodyImageData, 'public-read'); + await uploadCDNAsset(`${config.s3.bucket}`, `${userMiiKey}/body.png`, miiStudioBodyImageData, 'public-read'); }); PNIDSchema.method('scrub', async function scrub() { diff --git a/src/types/common/config.ts b/src/types/common/config.ts index 3554c56..cc6390f 100644 --- a/src/types/common/config.ts +++ b/src/types/common/config.ts @@ -27,6 +27,7 @@ export interface Config { from: string; }; s3: { + bucket: string; endpoint: string; key: string; secret: string; From 6e724c2e60b03b40e181da3920acb04774f64717 Mon Sep 17 00:00:00 2001 From: Billy <30775249+InternalLoss@users.noreply.github.com> Date: Sat, 18 Jan 2025 17:59:50 +0000 Subject: [PATCH 2/2] nit: don't use an escaped string when a variable is fine. --- src/models/pnid.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/models/pnid.ts b/src/models/pnid.ts index 3fe65cd..a5509d0 100644 --- a/src/models/pnid.ts +++ b/src/models/pnid.ts @@ -198,8 +198,8 @@ PNIDSchema.method('generateMiiImages', async function generateMiiImages(): Promi const userMiiKey = `mii/${this.pid}`; - await uploadCDNAsset(`${config.s3.bucket}`, `${userMiiKey}/standard.tga`, tga, 'public-read'); - await uploadCDNAsset(`${config.s3.bucket}`, `${userMiiKey}/normal_face.png`, miiStudioNormalFaceImageData, 'public-read'); + await uploadCDNAsset(config.s3.bucket, `${userMiiKey}/standard.tga`, tga, 'public-read'); + await uploadCDNAsset(config.s3.bucket, `${userMiiKey}/normal_face.png`, miiStudioNormalFaceImageData, 'public-read'); const expressions = ['frustrated', 'smile_open_mouth', 'wink_left', 'sorrow', 'surprise_open_mouth']; for (const expression of expressions) { @@ -210,7 +210,7 @@ PNIDSchema.method('generateMiiImages', async function generateMiiImages(): Promi instanceCount: 1, }); const miiStudioExpressionImageData = await got(miiStudioExpressionUrl).buffer(); - await uploadCDNAsset(`${config.s3.bucket}`, `${userMiiKey}/${expression}.png`, miiStudioExpressionImageData, 'public-read'); + await uploadCDNAsset(config.s3.bucket, `${userMiiKey}/${expression}.png`, miiStudioExpressionImageData, 'public-read'); } const miiStudioBodyUrl = mii.studioUrl({ @@ -219,7 +219,7 @@ PNIDSchema.method('generateMiiImages', async function generateMiiImages(): Promi instanceCount: 1, }); const miiStudioBodyImageData = await got(miiStudioBodyUrl).buffer(); - await uploadCDNAsset(`${config.s3.bucket}`, `${userMiiKey}/body.png`, miiStudioBodyImageData, 'public-read'); + await uploadCDNAsset(config.s3.bucket, `${userMiiKey}/body.png`, miiStudioBodyImageData, 'public-read'); }); PNIDSchema.method('scrub', async function scrub() {