From 1c6f8a161067d4b5aef7b36e4ffd714434ad6b19 Mon Sep 17 00:00:00 2001
From: Gustavo Trott <gustavo@trott.com.br>
Date: Tue, 30 Mar 2021 18:29:34 -0300
Subject: [PATCH] Moves default pdfToSvg conversion timeout to config

---
 .../presentation/imp/SvgImageCreatorImp.java  | 26 +++++++++++--------
 .../grails-app/conf/bigbluebutton.properties  |  5 ++++
 .../grails-app/conf/spring/doc-conversion.xml |  1 +
 3 files changed, 21 insertions(+), 11 deletions(-)

diff --git a/bbb-common-web/src/main/java/org/bigbluebutton/presentation/imp/SvgImageCreatorImp.java b/bbb-common-web/src/main/java/org/bigbluebutton/presentation/imp/SvgImageCreatorImp.java
index 1793f3e396..e100e2a9e4 100755
--- a/bbb-common-web/src/main/java/org/bigbluebutton/presentation/imp/SvgImageCreatorImp.java
+++ b/bbb-common-web/src/main/java/org/bigbluebutton/presentation/imp/SvgImageCreatorImp.java
@@ -29,8 +29,7 @@ public class SvgImageCreatorImp implements SvgImageCreator {
     private SwfSlidesGenerationProgressNotifier notifier;
     private long imageTagThreshold;
     private long pathsThreshold;
-    private String convTimeout = "60s";
-    private int WAIT_FOR_SEC = 60;
+    private int convPdfToSvgTimeout = 60;
 	private String BLANK_SVG;
 
     @Override
@@ -64,14 +63,14 @@ public class SvgImageCreatorImp implements SvgImageCreator {
             dest = imagePresentationDir.getAbsolutePath() + File.separator + "slide-1.pdf";
 
             NuProcessBuilder convertImgToSvg = new NuProcessBuilder(
-                    Arrays.asList("timeout", convTimeout, "convert", source, "-auto-orient", dest));
+                    Arrays.asList("timeout", convPdfToSvgTimeout + "s", "convert", source, "-auto-orient", dest));
 
             Png2SvgConversionHandler pHandler = new Png2SvgConversionHandler();
             convertImgToSvg.setProcessListener(pHandler);
 
             NuProcess process = convertImgToSvg.start();
             try {
-                process.waitFor(WAIT_FOR_SEC, TimeUnit.SECONDS);
+                process.waitFor(convPdfToSvgTimeout, TimeUnit.SECONDS);
                 done = true;
             } catch (InterruptedException e) {
                 done = false;
@@ -97,7 +96,7 @@ public class SvgImageCreatorImp implements SvgImageCreator {
 
         NuProcess process = convertPdfToSvg.start();
         try {
-            process.waitFor(WAIT_FOR_SEC, TimeUnit.SECONDS);
+            process.waitFor(convPdfToSvgTimeout, TimeUnit.SECONDS);
             done = true;
         } catch (InterruptedException e) {
             log.error("Interrupted Exception while generating SVG slides {}", pres.getName(), e);
@@ -159,20 +158,20 @@ public class SvgImageCreatorImp implements SvgImageCreator {
             convertPdfToPng.setProcessListener(pngHandler);
             NuProcess pngProcess = convertPdfToPng.start();
             try {
-                pngProcess.waitFor(WAIT_FOR_SEC, TimeUnit.SECONDS);
+                pngProcess.waitFor(convPdfToSvgTimeout, TimeUnit.SECONDS);
             } catch (InterruptedException e) {
                 log.error("Interrupted Exception while generating PNG image {}", pres.getName(), e);
             }
 
             // Step 2: Convert a PNG image to SVG
-            NuProcessBuilder convertPngToSvg = new NuProcessBuilder(Arrays.asList("timeout", convTimeout, "convert",
+            NuProcessBuilder convertPngToSvg = new NuProcessBuilder(Arrays.asList("timeout", convPdfToSvgTimeout + "s", "convert",
                         tempPng.getAbsolutePath(), destsvg.getAbsolutePath()));
 
             Png2SvgConversionHandler svgHandler = new Png2SvgConversionHandler();
             convertPngToSvg.setProcessListener(svgHandler);
             NuProcess svgProcess = convertPngToSvg.start();
             try {
-                svgProcess.waitFor(WAIT_FOR_SEC, TimeUnit.SECONDS);
+                svgProcess.waitFor(convPdfToSvgTimeout, TimeUnit.SECONDS);
             } catch (InterruptedException e) {
                 log.error("Interrupted Exception while generating SVG image {}", pres.getName(), e);
             }
@@ -184,7 +183,7 @@ public class SvgImageCreatorImp implements SvgImageCreator {
 
             // Step 3: Add SVG namespace to the destionation file
             // Check : https://phabricator.wikimedia.org/T43174
-            NuProcessBuilder addNameSpaceToSVG = new NuProcessBuilder(Arrays.asList("timeout", convTimeout,
+            NuProcessBuilder addNameSpaceToSVG = new NuProcessBuilder(Arrays.asList("timeout", convPdfToSvgTimeout + "s",
                         "/bin/sh", "-c",
                         "sed -i "
                                 + "'4s|>| xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" version=\"1.2\">|' "
@@ -194,7 +193,7 @@ public class SvgImageCreatorImp implements SvgImageCreator {
             addNameSpaceToSVG.setProcessListener(namespaceHandler);
             NuProcess namespaceProcess = addNameSpaceToSVG.start();
             try {
-                namespaceProcess.waitFor(WAIT_FOR_SEC, TimeUnit.SECONDS);
+                namespaceProcess.waitFor(convPdfToSvgTimeout, TimeUnit.SECONDS);
             } catch (InterruptedException e) {
                 log.error("Interrupted Exception while adding SVG namespace {}", pres.getName(), e);
             }
@@ -232,7 +231,7 @@ public class SvgImageCreatorImp implements SvgImageCreator {
             rawCommand += " && cat " + destFile;
             rawCommand += " | egrep 'data:image/png;base64|<path' | sed 's/  / /g' | cut -d' ' -f 1 | sort | uniq -cw 2";
         }
-        return new NuProcessBuilder(Arrays.asList("timeout", convTimeout, "/bin/sh", "-c", rawCommand));
+        return new NuProcessBuilder(Arrays.asList("timeout", convPdfToSvgTimeout + "s", "/bin/sh", "-c", rawCommand));
     }
 
     private File determineSvgImagesDirectory(File presentationFile) {
@@ -278,4 +277,9 @@ public class SvgImageCreatorImp implements SvgImageCreator {
         SwfSlidesGenerationProgressNotifier notifier) {
       this.notifier = notifier;
     }
+
+    public void setConvPdfToSvgTimeout(int convPdfToSvgTimeout) {
+        this.convPdfToSvgTimeout = convPdfToSvgTimeout;
+    }
+
 }
diff --git a/bigbluebutton-web/grails-app/conf/bigbluebutton.properties b/bigbluebutton-web/grails-app/conf/bigbluebutton.properties
index c618b764ca..150fc25a7b 100755
--- a/bigbluebutton-web/grails-app/conf/bigbluebutton.properties
+++ b/bigbluebutton-web/grails-app/conf/bigbluebutton.properties
@@ -98,6 +98,11 @@ numConversionThreads=5
 #------------------------------------
 numFileProcessorThreads=2
 
+#------------------------------------
+# Timeout(secs) to wait for pdf to svg conversion (time for each page)
+#------------------------------------
+svgConversionTimeout=60
+
 #------------------------------------
 # Timeout(secs) to wait for conversion script execution
 #------------------------------------
diff --git a/bigbluebutton-web/grails-app/conf/spring/doc-conversion.xml b/bigbluebutton-web/grails-app/conf/spring/doc-conversion.xml
index c5ab919208..b98c13fa43 100755
--- a/bigbluebutton-web/grails-app/conf/spring/doc-conversion.xml
+++ b/bigbluebutton-web/grails-app/conf/spring/doc-conversion.xml
@@ -86,6 +86,7 @@ with BigBlueButton; if not, see <http://www.gnu.org/licenses/>.
         <property name="imageTagThreshold" value="${imageTagThreshold}"/>
         <property name="pathsThreshold" value="${placementsThreshold}"/>
         <property name="blankSvg" value="${BLANK_SVG}"/>
+        <property name="convPdfToSvgTimeout" value="${svgConversionTimeout}"/>
     </bean>
 
     <bean id="generatedSlidesInfoHelper" class="org.bigbluebutton.presentation.GeneratedSlidesInfoHelperImp"/>
-- 
GitLab