修改下载目录
This commit is contained in:
20
server.js
20
server.js
@@ -2,7 +2,6 @@ const express = require('express');
|
||||
const cors = require('cors');
|
||||
const dotenv = require('dotenv');
|
||||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
const http = require('http');
|
||||
const WebSocket = require('ws');
|
||||
@@ -15,6 +14,7 @@ const app = express();
|
||||
const PORT = process.env.PORT || 3000;
|
||||
const HOST = process.env.HOST || process.env.LISTEN_ADDRESS || '0.0.0.0';
|
||||
const server = http.createServer(app);
|
||||
const DOWNLOAD_CACHE_DIR = path.join(__dirname, 'Downloads');
|
||||
|
||||
app.use(cors());
|
||||
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 ensureDownloadCacheDir = () => {
|
||||
if (!fs.existsSync(DOWNLOAD_CACHE_DIR)) {
|
||||
fs.mkdirSync(DOWNLOAD_CACHE_DIR, { recursive: true });
|
||||
}
|
||||
};
|
||||
|
||||
const addWsClient = (progressKey, ws) => {
|
||||
if (!wsSubscriptions.has(progressKey)) {
|
||||
wsSubscriptions.set(progressKey, new Set());
|
||||
@@ -257,13 +263,12 @@ wss.on('connection', (ws) => {
|
||||
|
||||
|
||||
const clearDownloadCache = () => {
|
||||
const tmpDir = os.tmpdir();
|
||||
try {
|
||||
if (!fs.existsSync(tmpDir)) return;
|
||||
const files = fs.readdirSync(tmpDir);
|
||||
ensureDownloadCacheDir();
|
||||
const files = fs.readdirSync(DOWNLOAD_CACHE_DIR);
|
||||
for (const file of files) {
|
||||
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 });
|
||||
}
|
||||
}
|
||||
@@ -392,7 +397,7 @@ app.get('/api/stream', async (req, res) => {
|
||||
}
|
||||
|
||||
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 = {
|
||||
software: { 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 progressKey = safeKeySegments.join('/');
|
||||
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 auth = extractS3Credentials(req);
|
||||
|
||||
Reference in New Issue
Block a user