fix(upload): replace static S3_BUCKET with getS3Bucket() for dynamic config

This commit is contained in:
Zac Gaetano 2026-05-20 15:48:48 -04:00
parent ab504841c3
commit 737e69d72f

View file

@ -3,7 +3,7 @@ import multer from 'multer';
import { Queue } from 'bullmq'; import { Queue } from 'bullmq';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
import pool from '../db/pool.js'; import pool from '../db/pool.js';
import { s3Client, uploadStream, deleteObject, S3_BUCKET } from '../s3/client.js'; import { s3Client, uploadStream, deleteObject, getS3Bucket } from '../s3/client.js';
import { import {
CreateMultipartUploadCommand, CreateMultipartUploadCommand,
UploadPartCommand, UploadPartCommand,
@ -103,7 +103,6 @@ router.post('/init', async (req, res, next) => {
} }
const assetId = uuidv4(); const assetId = uuidv4();
// Include assetId in the key so same-named files in a project never collide
const s3Key = `originals/${assetId}/${filename}`; const s3Key = `originals/${assetId}/${filename}`;
const tagsArray = tags ? (Array.isArray(tags) ? tags : [tags]) : []; const tagsArray = tags ? (Array.isArray(tags) ? tags : [tags]) : [];
@ -123,7 +122,7 @@ router.post('/init', async (req, res, next) => {
const multipartUpload = await s3Client.send( const multipartUpload = await s3Client.send(
new CreateMultipartUploadCommand({ new CreateMultipartUploadCommand({
Bucket: S3_BUCKET, Bucket: getS3Bucket(),
Key: s3Key, Key: s3Key,
ContentType: contentType, ContentType: contentType,
}) })
@ -152,7 +151,7 @@ router.post('/part', upload.single('file'), async (req, res, next) => {
const partUpload = await s3Client.send( const partUpload = await s3Client.send(
new UploadPartCommand({ new UploadPartCommand({
Bucket: S3_BUCKET, Bucket: getS3Bucket(),
Key: key, Key: key,
PartNumber: parseInt(partNumber, 10), PartNumber: parseInt(partNumber, 10),
UploadId: uploadId, UploadId: uploadId,
@ -182,11 +181,10 @@ router.post('/complete', async (req, res, next) => {
await s3Client.send( await s3Client.send(
new CompleteMultipartUploadCommand({ new CompleteMultipartUploadCommand({
Bucket: S3_BUCKET, Bucket: getS3Bucket(),
Key: key, Key: key,
UploadId: uploadId, UploadId: uploadId,
MultipartUpload: { MultipartUpload: {
// Accept both casings: api.js sends ETag (uppercase), defend against both
Parts: parts.map(p => ({ Parts: parts.map(p => ({
ETag: p.ETag || p.etag, ETag: p.ETag || p.etag,
PartNumber: p.partNumber || p.PartNumber, PartNumber: p.partNumber || p.PartNumber,
@ -195,8 +193,6 @@ router.post('/complete', async (req, res, next) => {
}) })
); );
// Original file in S3 — queue proxy generation
// proxy worker will dispatch thumbnail once proxy is ready
const result = await pool.query( const result = await pool.query(
`UPDATE assets `UPDATE assets
SET status = 'processing', updated_at = NOW() SET status = 'processing', updated_at = NOW()
@ -216,7 +212,6 @@ router.post('/complete', async (req, res, next) => {
outputKey: `proxies/${assetId}.mp4`, outputKey: `proxies/${assetId}.mp4`,
}); });
// Sync AMPP folder — non-blocking
syncToAmpp(asset.id, asset.project_id, asset.bin_id); syncToAmpp(asset.id, asset.project_id, asset.bin_id);
res.json(asset); res.json(asset);
@ -237,7 +232,7 @@ router.post('/abort', async (req, res, next) => {
} }
await s3Client.send( await s3Client.send(
new AbortMultipartUploadCommand({ Bucket: S3_BUCKET, Key: key, UploadId: uploadId }) new AbortMultipartUploadCommand({ Bucket: getS3Bucket(), Key: key, UploadId: uploadId })
); );
await pool.query('DELETE FROM assets WHERE id = $1', [assetId]); await pool.query('DELETE FROM assets WHERE id = $1', [assetId]);
@ -260,7 +255,6 @@ router.post('/simple', upload.single('file'), async (req, res, next) => {
} }
const assetId = uuidv4(); const assetId = uuidv4();
// Include assetId in the key so same-named files in a project never collide
const s3Key = `originals/${assetId}/${filename}`; const s3Key = `originals/${assetId}/${filename}`;
const mimeType = contentType || req.file.mimetype; const mimeType = contentType || req.file.mimetype;
const tagsArray = tags ? (Array.isArray(tags) ? tags : [tags]) : []; const tagsArray = tags ? (Array.isArray(tags) ? tags : [tags]) : [];
@ -289,14 +283,12 @@ router.post('/simple', upload.single('file'), async (req, res, next) => {
const asset = result.rows[0]; const asset = result.rows[0];
// Queue proxy — proxy worker dispatches thumbnail on completion
await proxyQueue.add('generate', { await proxyQueue.add('generate', {
assetId, assetId,
inputKey: s3Key, inputKey: s3Key,
outputKey: `proxies/${assetId}.mp4`, outputKey: `proxies/${assetId}.mp4`,
}); });
// Sync AMPP folder — non-blocking
syncToAmpp(assetId, projectId, binId || null); syncToAmpp(assetId, projectId, binId || null);
res.json(asset); res.json(asset);