feat: Docker部署与CI/CD集成, 搜索栏修复, 上传目录改为data
This commit is contained in:
31
pages/api/v1/files/[name].ts
Normal file
31
pages/api/v1/files/[name].ts
Normal file
@@ -0,0 +1,31 @@
|
||||
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');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user