Files
video_socket-server/src/server.ts

155 lines
4.7 KiB
TypeScript
Raw Normal View History

2026-04-29 15:18:30 +08:00
import * as express from 'express';
import * as path from 'path';
import * as fs from 'fs';
import * as morgan from 'morgan';
2026-05-23 22:47:34 +08:00
import { v4 as uuid } from 'uuid';
2026-04-29 15:18:30 +08:00
import signaling from './signaling';
import { log, LogLevel } from './log';
import Options from './class/options';
2026-05-23 23:49:47 +08:00
import { reset as resetHandler } from './class/httphandler';
2026-04-29 15:18:30 +08:00
import { initSwagger } from './swagger';
const cors = require('cors');
const multer = require('multer');
2026-05-23 22:47:34 +08:00
const AVATAR_UPLOAD_LIMIT_BYTES = 2 * 1024 * 1024;
const ALLOWED_AVATAR_MIME_TYPES = new Set(['image/jpeg', 'image/png', 'image/webp', 'image/gif']);
const ALLOWED_AVATAR_EXTENSIONS = new Set(['.jpg', '.jpeg', '.png', '.webp', '.gif']);
function safeAvatarExtension(file: any): string {
const originalExt = path.extname(file.originalname || '').toLowerCase();
if (ALLOWED_AVATAR_EXTENSIONS.has(originalExt)) {
return originalExt;
}
2026-05-23 23:49:47 +08:00
2026-05-23 22:47:34 +08:00
switch (file.mimetype) {
case 'image/jpeg':
return '.jpg';
case 'image/png':
return '.png';
case 'image/webp':
return '.webp';
case 'image/gif':
return '.gif';
default:
return '';
}
}
function isAllowedAvatar(file: any): boolean {
2026-05-23 23:49:47 +08:00
return ALLOWED_AVATAR_MIME_TYPES.has(file.mimetype) && safeAvatarExtension(file).length > 0;
2026-05-23 22:47:34 +08:00
}
2026-04-29 15:18:30 +08:00
export const createServer = (config: Options): express.Express => {
const app: express.Express = express();
resetHandler(config.mode);
2026-05-23 23:49:47 +08:00
if (config.logging !== 'none') {
2026-04-29 15:18:30 +08:00
app.use(morgan(config.logging));
}
2026-05-23 23:49:47 +08:00
app.use(cors({ origin: '*' }));
2026-04-29 15:18:30 +08:00
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
2026-05-23 23:49:47 +08:00
app.get('/config', (_req, res) => res.json({
useWebSocket: config.type === 'websocket',
2026-05-17 22:07:07 +08:00
startupMode: config.mode,
logging: config.logging,
protocol: config.secure ? 'https' : 'http',
port: config.port
}));
2026-05-23 23:49:47 +08:00
2026-04-29 15:18:30 +08:00
app.use('/signaling', signaling);
app.use(express.static(path.join(__dirname, '../client/public')));
app.use('/module', express.static(path.join(__dirname, '../client/src')));
2026-05-23 23:49:47 +08:00
app.get('/', (_req, res) => {
const indexPagePath = path.join(__dirname, '../client/public/index.html');
2026-04-29 15:18:30 +08:00
fs.access(indexPagePath, (err) => {
if (err) {
2026-05-23 23:49:47 +08:00
log(LogLevel.warn, `Can't find file '${indexPagePath}'`);
2026-04-29 15:18:30 +08:00
res.status(404).send(`Can't find file ${indexPagePath}`);
2026-05-23 23:49:47 +08:00
return;
2026-04-29 15:18:30 +08:00
}
2026-05-23 23:49:47 +08:00
res.sendFile(indexPagePath);
2026-04-29 15:18:30 +08:00
});
});
2026-05-23 23:49:47 +08:00
2026-04-29 15:18:30 +08:00
initSwagger(app, config);
const storage = multer.diskStorage({
2026-05-23 23:49:47 +08:00
destination: (_req: any, _file: any, cb: (error: Error | null, destination: string) => void) => {
2026-04-29 15:18:30 +08:00
const uploadDir = path.join(__dirname, '../client/public/uploads/avatars');
if (!fs.existsSync(uploadDir)) {
fs.mkdirSync(uploadDir, { recursive: true });
}
cb(null, uploadDir);
},
2026-05-23 23:49:47 +08:00
filename: (_req: any, file: any, cb: (error: Error | null, filename: string) => void) => {
2026-04-29 15:18:30 +08:00
cb(null, file.originalname);
}
});
2026-05-23 22:47:34 +08:00
const upload = multer({
2026-05-23 23:49:47 +08:00
storage,
2026-05-23 22:47:34 +08:00
limits: {
fileSize: AVATAR_UPLOAD_LIMIT_BYTES
},
fileFilter: (_req: express.Request, file: any, cb: (error: Error | null, acceptFile?: boolean) => void) => {
if (!isAllowedAvatar(file)) {
cb(new Error('Only jpg, png, webp, or gif avatars are allowed'));
return;
}
2026-05-23 23:49:47 +08:00
2026-05-23 22:47:34 +08:00
cb(null, true);
}
});
2026-04-29 15:18:30 +08:00
2026-05-23 22:47:34 +08:00
app.post('/api/upload/avatar', (req: express.Request, res: express.Response) => {
upload.single('avatar')(req, res, (error: Error) => {
if (error) {
log(LogLevel.warn, 'Avatar upload rejected:', error.message);
const isSizeLimit = error.name === 'MulterError' && (error as any).code === 'LIMIT_FILE_SIZE';
2026-05-23 23:49:47 +08:00
res.status(400).json({
2026-05-23 22:47:34 +08:00
success: false,
message: isSizeLimit ? 'Avatar file is too large' : error.message
});
2026-05-23 23:49:47 +08:00
return;
2026-05-23 22:47:34 +08:00
}
const request = req as any;
if (!request.file) {
2026-05-23 23:49:47 +08:00
res.status(400).json({ success: false, message: 'No file uploaded' });
return;
2026-05-23 22:47:34 +08:00
}
2026-04-29 15:18:30 +08:00
2026-05-23 22:47:34 +08:00
const ext = safeAvatarExtension(request.file);
if (!ext) {
fs.unlink(request.file.path, () => undefined);
2026-05-23 23:49:47 +08:00
res.status(400).json({ success: false, message: 'Unsupported avatar file type' });
return;
2026-05-23 22:47:34 +08:00
}
const oldPath = request.file.path;
const newFilename = `avatar_${uuid()}${ext}`;
const newPath = path.join(path.dirname(oldPath), newFilename);
2026-04-29 15:18:30 +08:00
2026-05-23 22:47:34 +08:00
fs.rename(oldPath, newPath, (err) => {
if (err) {
log(LogLevel.error, 'Error renaming file:', err);
2026-05-23 23:49:47 +08:00
res.status(500).json({ success: false, message: 'Avatar rename failed' });
return;
}
2026-04-29 15:18:30 +08:00
2026-05-23 23:49:47 +08:00
res.json({ success: true, avatarUrl: `/uploads/avatars/${newFilename}` });
});
2026-05-23 22:47:34 +08:00
});
2026-04-29 15:18:30 +08:00
});
app.use('/uploads', express.static(path.join(__dirname, '../client/public/uploads')));
return app;
};