修改下载目录

This commit is contained in:
CN-JS-HuiBai
2026-04-03 00:49:04 +08:00
parent b6dc21c265
commit 7581d33112
3 changed files with 16 additions and 10 deletions

View File

@@ -67,7 +67,7 @@
<option value="neon">NEON</option> <option value="neon">NEON</option>
<option value="nvidia">NVIDIA</option> <option value="nvidia">NVIDIA</option>
<option value="intel">Intel</option> <option value="intel">Intel</option>
<option value="vaapi">VAAPI</option> <option value="vaapi" selected>VAAPI</option>
</select> </select>
</div> </div>
<div id="loading-spinner" class="spinner-container"> <div id="loading-spinner" class="spinner-container">

View File

@@ -116,7 +116,7 @@ document.addEventListener('DOMContentLoaded', () => {
const buildStreamUrl = (targetSeconds = null) => { const buildStreamUrl = (targetSeconds = null) => {
const codec = codecSelect?.value || 'h264'; const codec = codecSelect?.value || 'h264';
const encoder = encoderSelect?.value || 'software'; const encoder = encoderSelect?.value || 'vaapi';
const streamSessionId = createStreamSessionId(); const streamSessionId = createStreamSessionId();
let streamUrl = `/api/stream?bucket=${encodeURIComponent(selectedBucket)}&key=${encodeURIComponent(selectedKey)}&codec=${encodeURIComponent(codec)}&encoder=${encodeURIComponent(encoder)}&streamSessionId=${encodeURIComponent(streamSessionId)}`; let streamUrl = `/api/stream?bucket=${encodeURIComponent(selectedBucket)}&key=${encodeURIComponent(selectedKey)}&codec=${encodeURIComponent(codec)}&encoder=${encodeURIComponent(encoder)}&streamSessionId=${encodeURIComponent(streamSessionId)}`;
if (typeof targetSeconds === 'number' && Number.isFinite(targetSeconds) && targetSeconds > 0) { if (typeof targetSeconds === 'number' && Number.isFinite(targetSeconds) && targetSeconds > 0) {
@@ -909,7 +909,7 @@ document.addEventListener('DOMContentLoaded', () => {
try { try {
const codec = codecSelect?.value || 'h264'; const codec = codecSelect?.value || 'h264';
const encoder = encoderSelect?.value || 'software'; const encoder = encoderSelect?.value || 'vaapi';
if (!selectedBucket) throw new Error('No bucket selected'); if (!selectedBucket) throw new Error('No bucket selected');
const streamUrl = buildStreamUrl(); const streamUrl = buildStreamUrl();
videoPlayer.src = streamUrl; videoPlayer.src = streamUrl;

View File

@@ -2,7 +2,6 @@ const express = require('express');
const cors = require('cors'); const cors = require('cors');
const dotenv = require('dotenv'); const dotenv = require('dotenv');
const fs = require('fs'); const fs = require('fs');
const os = require('os');
const path = require('path'); const path = require('path');
const http = require('http'); const http = require('http');
const WebSocket = require('ws'); const WebSocket = require('ws');
@@ -15,6 +14,7 @@ const app = express();
const PORT = process.env.PORT || 3000; const PORT = process.env.PORT || 3000;
const HOST = process.env.HOST || process.env.LISTEN_ADDRESS || '0.0.0.0'; const HOST = process.env.HOST || process.env.LISTEN_ADDRESS || '0.0.0.0';
const server = http.createServer(app); const server = http.createServer(app);
const DOWNLOAD_CACHE_DIR = path.join(__dirname, 'Downloads');
app.use(cors()); app.use(cors());
app.use(express.json()); app.use(express.json());
@@ -66,6 +66,12 @@ const getProgressKey = (key) => key.split('/').map(segment => segment.replace(/[
const createStreamSessionId = () => `${Date.now()}-${Math.random().toString(36).slice(2, 10)}`; const createStreamSessionId = () => `${Date.now()}-${Math.random().toString(36).slice(2, 10)}`;
const ensureDownloadCacheDir = () => {
if (!fs.existsSync(DOWNLOAD_CACHE_DIR)) {
fs.mkdirSync(DOWNLOAD_CACHE_DIR, { recursive: true });
}
};
const addWsClient = (progressKey, ws) => { const addWsClient = (progressKey, ws) => {
if (!wsSubscriptions.has(progressKey)) { if (!wsSubscriptions.has(progressKey)) {
wsSubscriptions.set(progressKey, new Set()); wsSubscriptions.set(progressKey, new Set());
@@ -257,13 +263,12 @@ wss.on('connection', (ws) => {
const clearDownloadCache = () => { const clearDownloadCache = () => {
const tmpDir = os.tmpdir();
try { try {
if (!fs.existsSync(tmpDir)) return; ensureDownloadCacheDir();
const files = fs.readdirSync(tmpDir); const files = fs.readdirSync(DOWNLOAD_CACHE_DIR);
for (const file of files) { for (const file of files) {
if (file.startsWith('s3-input-') && file.endsWith('.tmp')) { if (file.startsWith('s3-input-') && file.endsWith('.tmp')) {
const filePath = path.join(tmpDir, file); const filePath = path.join(DOWNLOAD_CACHE_DIR, file);
fs.rmSync(filePath, { force: true }); fs.rmSync(filePath, { force: true });
} }
} }
@@ -392,7 +397,7 @@ app.get('/api/stream', async (req, res) => {
} }
const safeCodec = codec === 'h265' ? 'h265' : 'h264'; const safeCodec = codec === 'h265' ? 'h265' : 'h264';
const safeEncoder = ['nvidia', 'intel', 'vaapi', 'neon'].includes(encoder) ? encoder : 'software'; const safeEncoder = ['nvidia', 'intel', 'vaapi', 'neon'].includes(encoder) ? encoder : 'vaapi';
const codecMap = { const codecMap = {
software: { h264: 'libx264', h265: 'libx265' }, software: { h264: 'libx264', h265: 'libx265' },
neon: { h264: 'libx264', h265: 'libx265' }, neon: { h264: 'libx264', h265: 'libx265' },
@@ -405,7 +410,8 @@ app.get('/api/stream', async (req, res) => {
const safeKeySegments = key.split('/').map(segment => segment.replace(/[^a-zA-Z0-9_\-]/g, '_')); const safeKeySegments = key.split('/').map(segment => segment.replace(/[^a-zA-Z0-9_\-]/g, '_'));
const progressKey = safeKeySegments.join('/'); const progressKey = safeKeySegments.join('/');
const safeBucket = bucket.replace(/[^a-zA-Z0-9_\-]/g, '_'); const safeBucket = bucket.replace(/[^a-zA-Z0-9_\-]/g, '_');
const tmpInputPath = path.join(os.tmpdir(), `s3-input-${safeBucket}-${safeKeySegments.join('-')}.tmp`); ensureDownloadCacheDir();
const tmpInputPath = path.join(DOWNLOAD_CACHE_DIR, `s3-input-${safeBucket}-${safeKeySegments.join('-')}.tmp`);
const cacheExists = fs.existsSync(tmpInputPath); const cacheExists = fs.existsSync(tmpInputPath);
const auth = extractS3Credentials(req); const auth = extractS3Credentials(req);