Browse Source

fix regex for html -> pdf path replacement

Futa Arai 9 tháng trước cách đây
mục cha
commit
4668984f72

+ 17 - 6
apps/pdf-converter/src/service/pdf-convert.ts

@@ -225,15 +225,26 @@ class PdfConvertService implements OnInit {
    * @returns writable stream
    * @returns writable stream
    */
    */
   private getPdfWritable(): Writable {
   private getPdfWritable(): Writable {
+    const escapedRoot = this.tmpOutputRootDir.replace(/\//g, '\\/');
     return new Writable({
     return new Writable({
       objectMode: true,
       objectMode: true,
       write: async (pageInfo: PageInfo, encoding, callback) => {
       write: async (pageInfo: PageInfo, encoding, callback) => {
-        const fileOutputPath = pageInfo.htmlFilePath
-          .replace(
-            new RegExp(`^(${this.tmpOutputRootDir}(?:[^/]+)?)(/html)`),
-            '$1',
-          )
-          .replace(/\.html$/, '.pdf');
+        const pattern = new RegExp(
+          `^${escapedRoot}(?:\\/([0-9]+))?\\/html\\/(.+?)\\.html$`,
+        );
+
+        const match = pageInfo.htmlFilePath.match(pattern);
+        if (match == null) {
+          // Skip to next pageInfo if path doesn't match expected layout
+          callback();
+          return;
+        }
+
+        // match[1] → optional numeric dir, match[2] → basename (without extension)
+        const numericSegment = match[1] ? `/${match[1]}` : '';
+        const baseName = match[2];
+
+        const fileOutputPath = `${this.tmpOutputRootDir}${numericSegment}/${baseName}.pdf`;
         const fileOutputParentPath = this.getParentPath(fileOutputPath);
         const fileOutputParentPath = this.getParentPath(fileOutputPath);
 
 
         try {
         try {