From a3081ef4a9abee9682a17f1323f510acdc999084 Mon Sep 17 00:00:00 2001 From: Yiding He Date: Tue, 24 Aug 2021 11:28:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jodconverter/util/ConfigUtils.java | 46 ++++++++++++++----- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/office-plugin/src/main/java/org/artofsolving/jodconverter/util/ConfigUtils.java b/office-plugin/src/main/java/org/artofsolving/jodconverter/util/ConfigUtils.java index 8a808808..c37528b1 100644 --- a/office-plugin/src/main/java/org/artofsolving/jodconverter/util/ConfigUtils.java +++ b/office-plugin/src/main/java/org/artofsolving/jodconverter/util/ConfigUtils.java @@ -29,23 +29,45 @@ public class ConfigUtils { return userDir; } + // 获取环境变量,如果找不到则返回默认值 + @SuppressWarnings("SameParameterValue") + private static String env(String key, String def) { + String value = System.getenv(key); + return value == null ? def : value; + } - public static String getOfficePluginPath() { - String userDir = System.getenv("KKFILEVIEW_BIN_FOLDER"); - if (userDir == null) { - userDir = System.getProperty("user.dir"); - } - if (userDir.endsWith("bin")) { - userDir = userDir.substring(0, userDir.length() - 4); - } else { - String separator = File.separator; - if (!userDir.contains(OFFICE_PLUGIN_NAME)) { - userDir = userDir + separator + OFFICE_PLUGIN_NAME; + // 返回参数列表中第一个真实存在的路径,或者 null + private static String firstExists(File... paths) { + for (File path : paths) { + if (path.exists()) { + return path.getAbsolutePath(); } } - return userDir; + return null; } + public static String getOfficePluginPath() { + String userDir = System.getProperty("user.dir"); + String binFolder = env("KKFILEVIEW_BIN_FOLDER", userDir); + + File pluginPath = new File(binFolder); + + // 如果指定了 bin 或其父目录,则返回父目录 + // 否则在当前目录和父目录中寻找 office-plugin + if (new File(pluginPath, "bin").exists()) { + return pluginPath.getAbsolutePath(); + + } else if (pluginPath.exists() && pluginPath.getName().equals("bin")) { + return pluginPath.getParentFile().getAbsolutePath(); + + } else { + return firstExists( + new File(pluginPath, OFFICE_PLUGIN_NAME), + new File(pluginPath.getParentFile(), OFFICE_PLUGIN_NAME) + ); + } + } + public static String getCustomizedConfigPath() { String homePath = getHomePath(); String separator = java.io.File.separator;