This commit is contained in:
2025-10-17 09:44:58 -05:00
parent 45a61a032c
commit 655050bf46
6 changed files with 5182 additions and 714 deletions

View File

@@ -17,7 +17,7 @@ export function getS3Client() {
socketTimeout: 60000,
httpsAgent: new https.Agent({
keepAlive: true,
maxSockets: 50 // Erhöhe parallele Verbindungen
maxSockets: 50
})
})
});
@@ -25,22 +25,66 @@ export function getS3Client() {
export function authenticate(req: NextRequest) {
const auth = req.headers.get('Authorization');
console.log('Received Auth Header:', auth); // Logge den Header
console.log('Expected Password:', process.env.APP_PASSWORD); // Logge das Env-Passwort (Achtung: Sensibel, nur für Debug!)
console.log('Received Auth Header:', auth);
console.log('Expected Password:', process.env.APP_PASSWORD);
if (!auth || !auth.startsWith('Basic ')) return false;
const [user, pass] = Buffer.from(auth.slice(6), 'base64').toString().split(':');
return user === 'admin' && pass === process.env.APP_PASSWORD;
}
export async function getBody(stream: Readable): Promise<Buffer> {
export async function getBody(stream: Readable, timeoutMs = 30000): Promise<Buffer> {
console.log('Getting body from stream...');
return new Promise((resolve, reject) => {
const chunks: Buffer[] = [];
stream.on('data', chunk => chunks.push(chunk));
stream.on('error', reject);
let totalSize = 0;
let timeoutHandle: NodeJS.Timeout;
// Timeout-Handler
const cleanup = () => {
if (timeoutHandle) clearTimeout(timeoutHandle);
stream.removeAllListeners();
};
timeoutHandle = setTimeout(() => {
cleanup();
reject(new Error(`Stream timeout after ${timeoutMs}ms`));
}, timeoutMs);
stream.on('data', (chunk: Buffer) => {
chunks.push(chunk);
totalSize += chunk.length;
// Optional: Limit für maximale Größe (z.B. 50MB)
if (totalSize > 50 * 1024 * 1024) {
cleanup();
reject(new Error('Stream size exceeded maximum limit of 50MB'));
}
});
stream.on('error', (err) => {
cleanup();
console.error('Stream error:', err);
reject(err);
});
stream.on('end', () => {
console.log('Body fetched, size:', Buffer.concat(chunks).length);
resolve(Buffer.concat(chunks));
cleanup();
const buffer = Buffer.concat(chunks);
console.log('Body fetched, size:', buffer.length);
resolve(buffer);
});
// Handle stream destroy/close
stream.on('close', () => {
cleanup();
if (chunks.length > 0) {
const buffer = Buffer.concat(chunks);
console.log('Stream closed, partial data fetched, size:', buffer.length);
resolve(buffer);
} else {
reject(new Error('Stream closed without data'));
}
});
});
}