This commit is contained in:
Bastian Wagner
2026-03-13 16:12:06 +01:00
parent 202f6579b4
commit 7aa91252a7
3 changed files with 42 additions and 9 deletions

View File

@@ -1,4 +1,4 @@
import { Body, Controller, Get, Header, Post, Res } from "@nestjs/common";
import { Body, Controller, Get, Header, Param, Post, Res } from "@nestjs/common";
import { PdfService } from "./pdf.service";
import { Response } from 'express';
import { KeyHandoverDto } from "src/model/key-handover.dto";
@@ -25,4 +25,13 @@ export class PdfController {
res.send(pdfBuffer);
}
@Get('keyhandover/:id')
@Header('Content-Type', 'application/pdf')
async getPDF(@Res() res: Response, @Param('id') id: string,) {
const file = await this.pdfService.getPDF(id, 'keyvault-pro')
res.setHeader('Content-Type', file.contentType ?? 'application/octet-stream');
res.setHeader('Content-Disposition', `inline; filename="${file.fileName}"`);
res.send(file.buffer);
}
}

View File

@@ -8,6 +8,11 @@ export class PdfService {
constructor(private minioService: MinioService) {}
async getPDF(key: string, bucket: string) {
const pdf = await this.minioService.getPDF(bucket, key)
return pdf;
}
async generateTestPdf(): Promise<Buffer> {
const browser = await puppeteer.launch({
executablePath: process.env.PUPPETEER_EXECUTABLE_PATH || '/usr/bin/chromium',

View File

@@ -1,19 +1,14 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { PutObjectCommand, S3Client } from '@aws-sdk/client-s3';
import { GetObjectCommand, PutObjectCommand, S3Client } from '@aws-sdk/client-s3';
@Injectable()
export class MinioService {
private client: S3Client;
constructor(private configService: ConfigService) {
}
async uploadPdf(bucket: string, key: string, pdfBuffer: Buffer): Promise<void> {
if (!this.client) {
this.client = new S3Client({
this.client = new S3Client({
region: 'us-east-1',
endpoint: this.configService.get('MINIOHOST') || '',
credentials: {
@@ -22,8 +17,32 @@ export class MinioService {
},
forcePathStyle: true,
});
console.log(this.configService.get('MINIOHOST'))
}
async getPDF(bucket: string, key: string) {
console.log(bucket, key)
const response = await this.client.send(
new GetObjectCommand({
Bucket: bucket,
Key: key
})
)
console.log("DONE")
const chunks: Uint8Array[] = [];
for await (const chunk of response.Body as any) {
chunks.push(chunk);
}
return {
buffer: Buffer.concat(chunks),
contentType: response.ContentType,
fileName: key.split('/').pop() ?? 'download',
};
}
async uploadPdf(bucket: string, key: string, pdfBuffer: Buffer): Promise<void> {
await this.client.send(
new PutObjectCommand({
Bucket: bucket,