Skip to content

Commit

Permalink
Merge pull request #1 from pubpub/kalilsn/delete-temp-files
Browse files Browse the repository at this point in the history
Delete temporary files after pdf generation
  • Loading branch information
kalilsn authored Mar 25, 2024
2 parents f23f388 + 37dc4ff commit d77ce75
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit d77ce75

Please sign in to comment.