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 { PdfService } from "./pdf.service";
|
||||||
import { Response } from 'express';
|
import { Response } from 'express';
|
||||||
import { KeyHandoverDto } from "src/model/key-handover.dto";
|
import { KeyHandoverDto } from "src/model/key-handover.dto";
|
||||||
@@ -25,4 +25,13 @@ export class PdfController {
|
|||||||
res.send(pdfBuffer);
|
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) {}
|
constructor(private minioService: MinioService) {}
|
||||||
|
|
||||||
|
async getPDF(key: string, bucket: string) {
|
||||||
|
const pdf = await this.minioService.getPDF(bucket, key)
|
||||||
|
return pdf;
|
||||||
|
}
|
||||||
|
|
||||||
async generateTestPdf(): Promise<Buffer> {
|
async generateTestPdf(): Promise<Buffer> {
|
||||||
const browser = await puppeteer.launch({
|
const browser = await puppeteer.launch({
|
||||||
executablePath: process.env.PUPPETEER_EXECUTABLE_PATH || '/usr/bin/chromium',
|
executablePath: process.env.PUPPETEER_EXECUTABLE_PATH || '/usr/bin/chromium',
|
||||||
|
|||||||
@@ -1,19 +1,14 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
import { PutObjectCommand, S3Client } from '@aws-sdk/client-s3';
|
import { GetObjectCommand, PutObjectCommand, S3Client } from '@aws-sdk/client-s3';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class MinioService {
|
export class MinioService {
|
||||||
private client: S3Client;
|
private client: S3Client;
|
||||||
|
|
||||||
constructor(private configService: ConfigService) {
|
constructor(private configService: ConfigService) {
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
this.client = new S3Client({
|
||||||
async uploadPdf(bucket: string, key: string, pdfBuffer: Buffer): Promise<void> {
|
|
||||||
if (!this.client) {
|
|
||||||
this.client = new S3Client({
|
|
||||||
region: 'us-east-1',
|
region: 'us-east-1',
|
||||||
endpoint: this.configService.get('MINIOHOST') || '',
|
endpoint: this.configService.get('MINIOHOST') || '',
|
||||||
credentials: {
|
credentials: {
|
||||||
@@ -22,8 +17,32 @@ export class MinioService {
|
|||||||
},
|
},
|
||||||
forcePathStyle: true,
|
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(
|
await this.client.send(
|
||||||
new PutObjectCommand({
|
new PutObjectCommand({
|
||||||
Bucket: bucket,
|
Bucket: bucket,
|
||||||
|
|||||||
Reference in New Issue
Block a user