| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- const fs = require('fs');
- const path = require('path');
- function registerUploadRoutes(app, deps) {
- app.post('/api/upload/video', deps.upload.single('video'), (req, res) => {
- try {
- if (!req.file) {
- return res.status(400).json({ error: '未收到视频文件' });
- }
- const file = req.file;
- const filename = file.filename;
- const ext = path.extname(filename).replace('.', '').toLowerCase();
- const videoId = `VID-${Date.now()}`;
- const fileStat = fs.statSync(file.path);
- const videoEntry = {
- id: videoId,
- title: req.body.title || path.basename(filename, path.extname(filename)),
- filename,
- size: fileStat.size,
- duration: 0,
- category: 'uploaded',
- tags: req.body.tags ? JSON.parse(req.body.tags) : [],
- description: req.body.description || '用户上传的视频',
- source: 'uploaded',
- metadata: {
- resolution: '未知',
- format: ext || 'mp4',
- },
- };
- const manifest = deps.readManifest();
- manifest.push(videoEntry);
- deps.writeManifest(manifest);
- console.log(`📤 视频上传成功: ${filename} (${(fileStat.size / 1024 / 1024).toFixed(1)}MB) → ${videoId}`);
- res.json({
- success: true,
- video: videoEntry,
- filepath: `/backend/video/${filename}`,
- });
- } catch (err) {
- console.error('❌ 视频上传失败:', err.message);
- res.status(500).json({ error: `上传失败: ${err.message}` });
- }
- });
- }
- module.exports = { registerUploadRoutes };
|