Changed get files api to send the data as a stream

This commit is contained in:
Lee 2022-11-15 03:33:12 +00:00
parent 126b4e9f9d
commit fdbc772c38
No known key found for this signature in database
GPG Key ID: 6EA25896ECCB3121

@ -1,3 +1,4 @@
import { StatusCodes } from "http-status-codes";
import { getFileRaw } from "src/utils/helpers/fileHelpers"; import { getFileRaw } from "src/utils/helpers/fileHelpers";
/** /**
@ -5,10 +6,15 @@ import { getFileRaw } from "src/utils/helpers/fileHelpers";
* @param {NextResponse} res * @param {NextResponse} res
*/ */
export default async function handler(req, res) { export default async function handler(req, res) {
const fileId = req.query.fileId; const fileId = req.query.fileId.split(".")[0]; // The file id to fetch
const { file, buffer } = await getFileRaw(fileId); const { file, readStream } = await getFileRaw(fileId); // Fetch the info and readStream for the file
if (file == null) {
res.setHeader("Content-Type", file.contentType); return res
res.send(buffer); .status(StatusCodes.NOT_FOUND)
.json({ status: StatusCodes.NOT_FOUND, message: "File not found" });
}
res.setHeader("Content-Type", file.contentType); // Set the content type header
readStream.pipe(res); // Send the file to the client
} }