Skip to content

Commit

Permalink
fix uploading magazines size limit by using streams
Browse files Browse the repository at this point in the history
  • Loading branch information
tlai18 committed Aug 30, 2024
1 parent e2ed8b8 commit b5ec33e
Showing 1 changed file with 38 additions and 8 deletions.
46 changes: 38 additions & 8 deletions lantern-club/src/pages/api/content/magazine/upload.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { IncomingForm, File as FormidableFile } from 'formidable'; // Correct import for FormidableFile
import { uploadFileToS3 } from '../../../../utils/uploadFileToS3';
import { IncomingForm, File as FormidableFile } from 'formidable';
import AWS from 'aws-sdk';
import fs from 'fs';
import { v4 as uuidv4 } from 'uuid';

AWS.config.update({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: process.env.AWS_REGION,
});

const s3 = new AWS.S3();

export const config = {
api: {
bodyParser: false,
bodyParser: false, // Disable body parsing to allow Formidable to handle the request
},
};

Expand All @@ -14,28 +24,48 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) {
return;
}

const form = new IncomingForm();
const form = new IncomingForm(); // Correctly initializing IncomingForm

form.parse(req, async (err, fields, files) => {
if (err) {
res.status(500).json({ error: err.message });
return;
}

// Assuming the file input field is named 'file'
const file = files['file'] && files['file'][0];
const file = Array.isArray(files['file']) ? files['file'][0] : files['file'];
if (!file) {
res.status(400).json({ error: 'No file uploaded' });
return;
}

try {
const fileUrl = await uploadFileToS3(file as FormidableFile, "uploads");
// Upload the file stream to S3
const fileUrl = await uploadStreamToS3(file as FormidableFile, "uploads");
res.status(200).json({ url: fileUrl });
return
} catch (error) {
console.error('Failed to upload file to S3', error);
res.status(500).json({ error: 'Failed to upload file to S3' });
return
}
});
}

async function uploadStreamToS3(file: FormidableFile, folder: string): Promise<string> {
const key = `${folder}/${uuidv4()}_${file.originalFilename}`;

const uploadParams = {
Bucket: process.env.AWS_BUCKET_NAME as string,
Key: key,
Body: fs.createReadStream(file.filepath), // Stream the file content
ContentType: file.mimetype || 'application/octet-stream',
};

return new Promise((resolve, reject) => {
s3.upload(uploadParams, (err: any, data: { Location: string | PromiseLike<string>; }) => {
if (err) {
return reject(err);
}
resolve(data.Location);
});
});
}

0 comments on commit b5ec33e

Please sign in to comment.