32 lines
1.0 KiB
TypeScript
32 lines
1.0 KiB
TypeScript
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
import { getServerConfig } from '../../../../lib/serverConfig';
|
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
const { name } = req.query;
|
|
if (typeof name !== 'string') {
|
|
return res.status(400).send('Invalid file name');
|
|
}
|
|
|
|
try {
|
|
const baseDir = path.join(process.cwd(), getServerConfig().uploadDir);
|
|
const filePath = path.join(baseDir, name);
|
|
if (!fs.existsSync(filePath)) {
|
|
return res.status(404).send('File not found');
|
|
}
|
|
const ext = path.extname(filePath).toLowerCase();
|
|
const type = ext === '.zip' ? 'application/zip'
|
|
: ext === '.mp4' ? 'video/mp4'
|
|
: ext === '.webm' ? 'video/webm'
|
|
: ext === '.mov' ? 'video/quicktime'
|
|
: 'application/octet-stream';
|
|
res.setHeader('Content-Type', type);
|
|
const stream = fs.createReadStream(filePath);
|
|
stream.pipe(res);
|
|
} catch (e) {
|
|
res.status(500).send('Internal error');
|
|
}
|
|
}
|
|
|