Files
MC_Report/backend/routes/uploads.js
canglan 8c5ce78fd0 chore: add AGPLv3 copyright header to all source files
- 52 JS files (backend + public/js): header with
  Copyright (C) 2026 Sea Network Technology Studio
  Author: CangLan <admin@sea-studio.top>
  + AGPLv3 notice
- idempotent (skips if header present), all syntax-checked
2026-08-19 20:27:05 +08:00

43 lines
1.9 KiB
JavaScript

/*
* MC Report System
* Copyright (C) 2026 Sea Network Technology Studio
* Author: CangLan <admin@sea-studio.top>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const express = require('express');
const path = require('path');
const fs = require('fs');
const { getRow } = require('../db');
const { authenticate } = require('../middleware/auth');
const { UPLOAD_DIR } = require('../middleware/upload');
const router = express.Router();
router.get('/:storedName', authenticate, async (req, res) => {
const att = await getRow('SELECT * FROM attachments WHERE stored_name = ?', [req.params.storedName]);
if (!att) return res.status(404).json({ error: '文件不存在' });
const ticket = await getRow('SELECT * FROM tickets WHERE id = ?', [att.ticket_id]);
if (req.user.role === 'player' && ticket?.user_id !== req.user.id) return res.status(403).json({ error: '无权限' });
const filePath = path.join(UPLOAD_DIR, att.stored_name);
if (!fs.existsSync(filePath)) return res.status(404).json({ error: '文件不存在' });
res.setHeader('Content-Type', att.mime_type);
res.setHeader('Content-Disposition', `inline; filename="${encodeURIComponent(att.original_name)}"`);
res.setHeader('Cache-Control', 'private, max-age=86400');
fs.createReadStream(filePath).pipe(res);
});
module.exports = router;