get file
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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',
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
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 {
|
||||
@@ -8,11 +8,6 @@ export class MinioService {
|
||||
|
||||
constructor(private configService: ConfigService) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
async uploadPdf(bucket: string, key: string, pdfBuffer: Buffer): Promise<void> {
|
||||
if (!this.client) {
|
||||
this.client = new S3Client({
|
||||
region: 'us-east-1',
|
||||
endpoint: this.configService.get('MINIOHOST') || '',
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user