From b8a57b1c7a653ad376a301fe6004af04f8e5692b Mon Sep 17 00:00:00 2001 From: Tyler Hargraves Date: Thu, 7 Apr 2022 11:16:10 -0400 Subject: [PATCH] adding is public config that defaults to false --- src/requests/adapter.ts | 5 ++++- src/requests/handler.ts | 6 +++--- src/requests/request.ts | 5 ++++- src/requests/storage-request.ts | 4 +++- src/s3/requests/adapter.ts | 4 +++- 5 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/requests/adapter.ts b/src/requests/adapter.ts index 6fc1850..4d56f36 100644 --- a/src/requests/adapter.ts +++ b/src/requests/adapter.ts @@ -14,11 +14,14 @@ export default class PdfGenerationRequestAdapter { } toPdfGenerationRequest() { + let isPublic = "false"; + if (this.requestBody.hasOwnProperty("isPublic")) isPublic = this.requestBody.isPublic; return new PdfGenerationRequest( this.requestBody.url, this.fileName, this.requestBody.cookies, - this.requestBody.options + this.requestBody.options, + isPublic ); } } diff --git a/src/requests/handler.ts b/src/requests/handler.ts index 5eb89b1..f704fe0 100644 --- a/src/requests/handler.ts +++ b/src/requests/handler.ts @@ -18,13 +18,13 @@ export default class PdfGenerationRequestHandler { async handleRequest() { const generatedPdfFilePath = await new PdfGenerationService().generate(this.request); console.log('PDF generated'); - const pdfUrl = await new S3PdfStorageService().store(this.getPdfStorageRequest(generatedPdfFilePath)); + const pdfUrl = await new S3PdfStorageService().store(this.getPdfStorageRequest(generatedPdfFilePath, this.request.isPublic)); console.log('Pdf stored on S3'); return PdfGenerationResponseAdapter.toCreated(pdfUrl); } - getPdfStorageRequest(pdfFilePath: string) { - return new PdfStorageRequest(this.request?.fileName, pdfFilePath, { + getPdfStorageRequest(pdfFilePath: string, isPublic: boolean) { + return new PdfStorageRequest(this.request?.fileName, pdfFilePath, isPublic, { url: this.request?.url }); } diff --git a/src/requests/request.ts b/src/requests/request.ts index 2a6a083..fe5cbc6 100644 --- a/src/requests/request.ts +++ b/src/requests/request.ts @@ -23,16 +23,19 @@ export default class PdfGenerationRequest { cookies?: Protocol.Network.CookieParam[]; pdfOptions?: Partial; browserOptions?: Partial; + isPublic: boolean; constructor( url: string, fileName: string, cookies: Protocol.Network.CookieParam[], - options: PDFRequestOptions = { pdfOptions: {}, browserOptions: {} } + options: PDFRequestOptions = { pdfOptions: {}, browserOptions: {} }, + isPublic: string ) { this.url = url; this.fileName = fileName; this.cookies = cookies; this.pdfOptions = options?.pdfOptions ?? {}; this.browserOptions = options?.browserOptions ?? {}; + this.isPublic = isPublic === "true"; } } diff --git a/src/requests/storage-request.ts b/src/requests/storage-request.ts index 3a70d74..d38d1d3 100644 --- a/src/requests/storage-request.ts +++ b/src/requests/storage-request.ts @@ -3,10 +3,12 @@ import { PutObjectRequest } from '@aws-sdk/client-s3'; export default class PdfStorageRequest { fileName: string; filePath: string; + isPublic: boolean; metadata: PutObjectRequest['Metadata']; - constructor(fileName: string, filePath: string, metadata: PutObjectRequest['Metadata']) { + constructor(fileName: string, filePath: string, isPublic: boolean, metadata: PutObjectRequest['Metadata']) { this.fileName = fileName; this.filePath = filePath; + this.isPublic = isPublic; this.metadata = metadata; } } diff --git a/src/s3/requests/adapter.ts b/src/s3/requests/adapter.ts index f04c59b..366ac77 100644 --- a/src/s3/requests/adapter.ts +++ b/src/s3/requests/adapter.ts @@ -6,6 +6,8 @@ import FileService from '../../services/file-service'; export default class S3PdfStorageRequestAdapter { toPutObjectCommand(pdfStorageRequest: PdfStorageRequest) { + let isPublicString = 'no'; + if (pdfStorageRequest.isPublic) isPublicString = 'yes'; return new PutObjectCommand({ Bucket: PDF_STORAGE_BUCKET_NAME, Key: `tmp/${pdfStorageRequest.fileName}`, @@ -14,7 +16,7 @@ export default class S3PdfStorageRequestAdapter { ContentDisposition: 'attachment', CacheControl: 'no-cache', Metadata: pdfStorageRequest.metadata, - Tagging: 'public=yes' + Tagging: `public=${isPublicString}` }); } }