From 37dc4ff350ff369824f2f8e2353c5ff6e6d9d53f Mon Sep 17 00:00:00 2001 From: Kalil Smith-Nuevelle Date: Tue, 7 Nov 2023 16:27:28 -0600 Subject: [PATCH] Delete temporary files after pdf generation --- src/main.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main.ts b/src/main.ts index 2188568..66e6fb0 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,10 +1,9 @@ import Router from "@koa/router"; import * as Proc from "child_process"; import { createReadStream } from "fs"; -import { writeFile } from "fs/promises"; import Koa, { Context, Next } from "koa"; import koaBody from "koa-body"; -import { temporaryFile } from "tempy"; +import { temporaryFileTask, temporaryWriteTask } from "tempy"; import { promisify } from "util"; import { installSentry } from "./sentry.js"; import { StorageProviderRegistry } from "./storage/index.js"; @@ -54,11 +53,12 @@ function spawnPagedProcess(inputFile: string, outputFile: string) { } async function convertToPDF(html: string) { - const tempInputFilePath = temporaryFile({ extension: ".html" }); - const tempOutputFilePath = temporaryFile({ extension: ".pdf" }); - await writeFile(tempInputFilePath, html); - await spawnPagedProcess(tempInputFilePath, tempOutputFilePath); - return uploadToStorage(tempOutputFilePath, ".pdf"); + return temporaryWriteTask(html, async (tempInputFilePath) => { + return temporaryFileTask(async (tempOutputFilePath) => { + await spawnPagedProcess(tempInputFilePath, tempOutputFilePath); + return uploadToStorage(tempOutputFilePath, ".pdf"); + }, { extension: ".pdf" }); + }, { extension: ".html" }); } const app = new Koa(); const router = new Router();