|
|
@@ -59,11 +59,15 @@ class PdfConvertService implements OnInit {
|
|
|
/**
|
|
|
* Register or update job inside jobList with given jobId, expirationDate, and status.
|
|
|
* If job is new, start reading html files and convert them to pdf.
|
|
|
- * @param jobId id of PageBulkExportJob
|
|
|
+ * @param orgId organization ID for GROWI.cloud
|
|
|
+ * @param appId application ID for GROWI.cloud
|
|
|
+ * @param jobId PageBulkExportJob ID
|
|
|
* @param expirationDate expiration date of job
|
|
|
* @param status status of job
|
|
|
*/
|
|
|
- async registerOrUpdateJob(jobId: string, expirationDate: Date, status: JobStatusSharedWithGrowi): Promise<void> {
|
|
|
+ async registerOrUpdateJob(
|
|
|
+ orgId: string | undefined, appId: string | undefined, jobId: string, expirationDate: Date, status: JobStatusSharedWithGrowi,
|
|
|
+ ): Promise<void> {
|
|
|
const isJobNew = !(jobId in this.jobList);
|
|
|
|
|
|
if (isJobNew) {
|
|
|
@@ -83,7 +87,7 @@ class PdfConvertService implements OnInit {
|
|
|
}
|
|
|
|
|
|
if (isJobNew && status !== JobStatus.FAILED) {
|
|
|
- this.readHtmlAndConvertToPdfUntilFinish(jobId);
|
|
|
+ this.readHtmlAndConvertToPdfUntilFinish(orgId, appId, jobId);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -134,9 +138,11 @@ class PdfConvertService implements OnInit {
|
|
|
/**
|
|
|
* Read html files from shared fs path, convert them to pdf, and save them to shared fs path.
|
|
|
* Repeat this until all html files are converted to pdf or job fails.
|
|
|
- * @param jobId id of PageBulkExportJob
|
|
|
+ * @param orgId organization ID for GROWI.cloud
|
|
|
+ * @param appId application ID for GROWI.cloud
|
|
|
+ * @param jobId PageBulkExportJob ID
|
|
|
*/
|
|
|
- private async readHtmlAndConvertToPdfUntilFinish(jobId: string): Promise<void> {
|
|
|
+ private async readHtmlAndConvertToPdfUntilFinish(orgId: string | undefined, appId: string | undefined, jobId: string): Promise<void> {
|
|
|
while (!this.isJobCompleted(jobId)) {
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
await new Promise(resolve => setTimeout(resolve, 10 * 1000));
|
|
|
@@ -146,7 +152,7 @@ class PdfConvertService implements OnInit {
|
|
|
throw new Error('Job expired');
|
|
|
}
|
|
|
|
|
|
- const htmlReadable = this.getHtmlReadable(jobId);
|
|
|
+ const htmlReadable = this.getHtmlReadable(orgId, appId, jobId);
|
|
|
const pdfWritable = this.getPdfWritable();
|
|
|
this.jobList[jobId].currentStream = htmlReadable;
|
|
|
|
|
|
@@ -165,11 +171,14 @@ class PdfConvertService implements OnInit {
|
|
|
|
|
|
/**
|
|
|
* Get readable stream that reads html files from shared fs path
|
|
|
- * @param jobId id of PageBulkExportJob
|
|
|
+ * @param orgId organization ID for GROWI.cloud
|
|
|
+ * @param appId application ID for GROWI.cloud
|
|
|
+ * @param jobId PageBulkExportJob ID
|
|
|
* @returns readable stream
|
|
|
*/
|
|
|
- private getHtmlReadable(jobId: string): Readable {
|
|
|
- const htmlFileEntries = fs.readdirSync(path.join(this.tmpHtmlDir, jobId), { recursive: true, withFileTypes: true }).filter(entry => entry.isFile());
|
|
|
+ private getHtmlReadable(orgId: string | undefined, appId: string | undefined, jobId: string): Readable {
|
|
|
+ const jobHtmlDir = path.join(this.tmpHtmlDir, orgId ?? '', appId ?? '', jobId);
|
|
|
+ const htmlFileEntries = fs.readdirSync(jobHtmlDir, { recursive: true, withFileTypes: true }).filter(entry => entry.isFile());
|
|
|
let index = 0;
|
|
|
|
|
|
const jobList = this.jobList;
|
|
|
@@ -215,7 +224,9 @@ class PdfConvertService implements OnInit {
|
|
|
await fs.promises.rm(pageInfo.htmlFilePath, { force: true });
|
|
|
}
|
|
|
catch (err) {
|
|
|
- callback(err);
|
|
|
+ if (err instanceof Error) {
|
|
|
+ callback(err);
|
|
|
+ }
|
|
|
return;
|
|
|
}
|
|
|
callback();
|
|
|
@@ -229,9 +240,9 @@ class PdfConvertService implements OnInit {
|
|
|
* @returns converted pdf
|
|
|
*/
|
|
|
private async convertHtmlToPdf(htmlString: string): Promise<Buffer> {
|
|
|
- const executeConvert = async(retries: number) => {
|
|
|
+ const executeConvert = async(retries: number): Promise<Buffer> => {
|
|
|
try {
|
|
|
- return this.puppeteerCluster.execute(htmlString);
|
|
|
+ return this.puppeteerCluster?.execute(htmlString);
|
|
|
}
|
|
|
catch (err) {
|
|
|
if (retries > 0) {
|