46 lines
1.6 KiB
TypeScript
46 lines
1.6 KiB
TypeScript
import { S3Client } from '@aws-sdk/client-s3';
|
|
import { NodeHttpHandler } from '@smithy/node-http-handler';
|
|
import { NextRequest } from 'next/server';
|
|
import { Readable } from 'stream';
|
|
import https from 'https';
|
|
|
|
export function getS3Client() {
|
|
return new S3Client({
|
|
region: process.env.AWS_REGION,
|
|
credentials: {
|
|
accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
|
|
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!
|
|
},
|
|
maxAttempts: 3,
|
|
requestHandler: new NodeHttpHandler({
|
|
connectionTimeout: 10000,
|
|
socketTimeout: 60000,
|
|
httpsAgent: new https.Agent({
|
|
keepAlive: true,
|
|
maxSockets: 50 // Erhöhe parallele Verbindungen
|
|
})
|
|
})
|
|
});
|
|
}
|
|
|
|
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!)
|
|
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> {
|
|
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);
|
|
stream.on('end', () => {
|
|
console.log('Body fetched, size:', Buffer.concat(chunks).length);
|
|
resolve(Buffer.concat(chunks));
|
|
});
|
|
});
|
|
} |