From 7aa91252a7f94e97ed8ce39630205303ecb00681 Mon Sep 17 00:00:00 2001 From: Bastian Wagner Date: Fri, 13 Mar 2026 16:12:06 +0100 Subject: [PATCH] get file --- src/pdf/pdf.controller.ts | 11 +++++++++- src/pdf/pdf.service.ts | 5 +++++ src/storage/minio/minio.service.ts | 35 +++++++++++++++++++++++------- 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/pdf/pdf.controller.ts b/src/pdf/pdf.controller.ts index 3988a02..4fbc224 100644 --- a/src/pdf/pdf.controller.ts +++ b/src/pdf/pdf.controller.ts @@ -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); + } } \ No newline at end of file diff --git a/src/pdf/pdf.service.ts b/src/pdf/pdf.service.ts index 44de175..6d5e6f6 100644 --- a/src/pdf/pdf.service.ts +++ b/src/pdf/pdf.service.ts @@ -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 { const browser = await puppeteer.launch({ executablePath: process.env.PUPPETEER_EXECUTABLE_PATH || '/usr/bin/chromium', diff --git a/src/storage/minio/minio.service.ts b/src/storage/minio/minio.service.ts index 77445aa..2fd5c33 100644 --- a/src/storage/minio/minio.service.ts +++ b/src/storage/minio/minio.service.ts @@ -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 { - 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 { + await this.client.send( new PutObjectCommand({ Bucket: bucket,