mirror of
				https://github.com/Frooodle/Stirling-PDF.git
				synced 2025-11-01 01:21:18 +01:00 
			
		
		
		
	dynamic port for UI from 8080 up (8081 etc) (#3042)
# Description of Changes Please provide a summary of the changes, including: - What was changed - Why the change was made - Any challenges encountered Closes #(issue_number) --- ## Checklist ### General - [ ] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [ ] I have read the [Stirling-PDF Developer Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md) (if applicable) - [ ] I have read the [How to add new languages to Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md) (if applicable) - [ ] I have performed a self-review of my own code - [ ] My changes generate no new warnings ### Documentation - [ ] I have updated relevant docs on [Stirling-PDF's doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) (if functionality has heavily changed) - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only) ### UI Changes (if applicable) - [ ] Screenshots or videos demonstrating the UI changes are attached (e.g., as comments or direct attachments in the PR) ### Testing (if applicable) - [ ] I have tested my changes locally. Refer to the [Testing Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md#6-testing) for more details.
This commit is contained in:
		
							parent
							
								
									ee6fbdd61f
								
							
						
					
					
						commit
						69da443096
					
				@ -1,7 +1,6 @@
 | 
			
		||||
package stirling.software.SPDF;
 | 
			
		||||
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.net.ServerSocket;
 | 
			
		||||
import java.net.URISyntaxException;
 | 
			
		||||
import java.nio.file.Files;
 | 
			
		||||
import java.nio.file.Path;
 | 
			
		||||
@ -29,6 +28,7 @@ import stirling.software.SPDF.UI.WebBrowser;
 | 
			
		||||
import stirling.software.SPDF.config.ConfigInitializer;
 | 
			
		||||
import stirling.software.SPDF.config.InstallationPathConfig;
 | 
			
		||||
import stirling.software.SPDF.model.ApplicationProperties;
 | 
			
		||||
import stirling.software.SPDF.utils.UrlUtils;
 | 
			
		||||
 | 
			
		||||
@Slf4j
 | 
			
		||||
@EnableScheduling
 | 
			
		||||
@ -64,6 +64,12 @@ public class SPDFApplication {
 | 
			
		||||
            app.setHeadless(false);
 | 
			
		||||
            props.put("java.awt.headless", "false");
 | 
			
		||||
            props.put("spring.main.web-application-type", "servlet");
 | 
			
		||||
 | 
			
		||||
            int desiredPort = 8080;
 | 
			
		||||
            String port = UrlUtils.findAvailablePort(desiredPort);
 | 
			
		||||
            props.put("server.port", port);
 | 
			
		||||
            System.setProperty("server.port", port);
 | 
			
		||||
            log.info("Desktop UI mode: Using port {}", port);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        app.setAdditionalProfiles(getActiveProfile(args));
 | 
			
		||||
@ -160,7 +166,17 @@ public class SPDFApplication {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Value("${server.port:8080}")
 | 
			
		||||
    public void setServerPortStatic(String port) {
 | 
			
		||||
    public void setServerPort(String port) {
 | 
			
		||||
        if ("auto".equalsIgnoreCase(port)) {
 | 
			
		||||
            // Use Spring Boot's automatic port assignment (server.port=0)
 | 
			
		||||
            SPDFApplication.serverPortStatic =
 | 
			
		||||
                    "0"; // This will let Spring Boot assign an available port
 | 
			
		||||
        } else {
 | 
			
		||||
            SPDFApplication.serverPortStatic = port;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void setServerPortStatic(String port) {
 | 
			
		||||
        if ("auto".equalsIgnoreCase(port)) {
 | 
			
		||||
            // Use Spring Boot's automatic port assignment (server.port=0)
 | 
			
		||||
            SPDFApplication.serverPortStatic =
 | 
			
		||||
@ -197,36 +213,11 @@ public class SPDFApplication {
 | 
			
		||||
        return new String[] {"default"};
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static boolean isPortAvailable(int port) {
 | 
			
		||||
        try (ServerSocket socket = new ServerSocket(port)) {
 | 
			
		||||
            return true;
 | 
			
		||||
        } catch (IOException e) {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Optionally keep this method if you want to provide a manual port-incrementation fallback.
 | 
			
		||||
    private static String findAvailablePort(int startPort) {
 | 
			
		||||
        int port = startPort;
 | 
			
		||||
        while (!isPortAvailable(port)) {
 | 
			
		||||
            port++;
 | 
			
		||||
        }
 | 
			
		||||
        return String.valueOf(port);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static String getStaticBaseUrl() {
 | 
			
		||||
        return baseUrlStatic;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public String getNonStaticBaseUrl() {
 | 
			
		||||
        return baseUrlStatic;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static String getStaticPort() {
 | 
			
		||||
        return serverPortStatic;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public String getNonStaticPort() {
 | 
			
		||||
        return serverPortStatic;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -62,7 +62,6 @@ public class RuntimePathConfig {
 | 
			
		||||
        String weasyPrintPath = isDocker ? "/opt/venv/bin/weasyprint" : "weasyprint";
 | 
			
		||||
        String unoConvertPath = isDocker ? "/opt/venv/bin/unoconvert" : "unoconvert";
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        // Check for custom operation paths
 | 
			
		||||
        Operations operations = properties.getSystem().getCustomPaths().getOperations();
 | 
			
		||||
        if (operations != null) {
 | 
			
		||||
@ -82,5 +81,4 @@ public class RuntimePathConfig {
 | 
			
		||||
    private boolean isRunningInDocker() {
 | 
			
		||||
        return Files.exists(Paths.get("/.dockerenv"));
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -52,7 +52,8 @@ public class CompressController {
 | 
			
		||||
        this.pdfDocumentFactory = pdfDocumentFactory;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void compressImagesInPDF(Path pdfFile, double initialScaleFactor, boolean grayScale) throws Exception {
 | 
			
		||||
    private void compressImagesInPDF(Path pdfFile, double initialScaleFactor, boolean grayScale)
 | 
			
		||||
            throws Exception {
 | 
			
		||||
        byte[] fileBytes = Files.readAllBytes(pdfFile);
 | 
			
		||||
        try (PDDocument doc = Loader.loadPDF(fileBytes)) {
 | 
			
		||||
            double scaleFactor = initialScaleFactor;
 | 
			
		||||
 | 
			
		||||
@ -1,5 +1,8 @@
 | 
			
		||||
package stirling.software.SPDF.utils;
 | 
			
		||||
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.net.ServerSocket;
 | 
			
		||||
 | 
			
		||||
import jakarta.servlet.http.HttpServletRequest;
 | 
			
		||||
 | 
			
		||||
public class UrlUtils {
 | 
			
		||||
@ -14,4 +17,20 @@ public class UrlUtils {
 | 
			
		||||
 | 
			
		||||
        return scheme + "://" + serverName + ":" + serverPort + contextPath;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static boolean isPortAvailable(int port) {
 | 
			
		||||
        try (ServerSocket socket = new ServerSocket(port)) {
 | 
			
		||||
            return true;
 | 
			
		||||
        } catch (IOException e) {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static String findAvailablePort(int startPort) {
 | 
			
		||||
        int port = startPort;
 | 
			
		||||
        while (!isPortAvailable(port)) {
 | 
			
		||||
            port++;
 | 
			
		||||
        }
 | 
			
		||||
        return String.valueOf(port);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -31,7 +31,7 @@ public class SPDFApplicationTest {
 | 
			
		||||
    private ApplicationProperties applicationProperties;
 | 
			
		||||
 | 
			
		||||
    @InjectMocks
 | 
			
		||||
    private SPDFApplication SPDFApplication;
 | 
			
		||||
    private SPDFApplication sPDFApplication;
 | 
			
		||||
 | 
			
		||||
    @BeforeEach
 | 
			
		||||
    public void setUp() {
 | 
			
		||||
@ -49,8 +49,4 @@ public class SPDFApplicationTest {
 | 
			
		||||
        assertEquals("8080", SPDFApplication.getStaticPort());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testGetNonStaticPort() {
 | 
			
		||||
        assertEquals("8080", SPDFApplication.getNonStaticPort());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user