diff --git a/src/org/scada_lts/utils/UploadFileUtils.java b/src/org/scada_lts/utils/UploadFileUtils.java index 3793dee850..a6915a031f 100644 --- a/src/org/scada_lts/utils/UploadFileUtils.java +++ b/src/org/scada_lts/utils/UploadFileUtils.java @@ -1,6 +1,7 @@ package org.scada_lts.utils; import com.serotonin.ShouldNeverHappenException; +import com.serotonin.mango.Common; import com.serotonin.mango.view.DynamicImage; import com.serotonin.mango.view.ImageSet; import com.serotonin.mango.view.ViewGraphic; @@ -19,6 +20,7 @@ import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; +import java.util.function.Consumer; import java.util.function.Predicate; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -316,12 +318,13 @@ private static List getImageSystemFilePaths(Supplier getLocalPath, if (!StringUtils.isEmpty(normalizePath) && (normalizePath.endsWith(normalizeFolder) || normalizePath.endsWith(normalizeFolder + File.separator))) { Path path = getAbsoluteResourcePath(normalizePath); - createIfNotExists(path); - paths.add(path); + createPath(path, notExistsPath(), paths::add); } Path path = getAppContextSystemFilePath(normalizeFolder); - createIfNotExists(path); - paths.add(path); + createPath(path, notExistsPath(), paths::add); + if(paths.isEmpty()) { + throw new IllegalStateException(Common.getMessage("Could not create paths!" )); + } return paths; } @@ -335,14 +338,13 @@ private static Path getImageSystemFileToWritePath(Supplier getLocalPath, } else { path = getAppContextSystemFilePath(normalizedFolder); } - createIfNotExists(path); - return path; - } - - private static void createIfNotExists(Path path) { - if(!Files.exists(path)) { - path.toFile().mkdirs(); + if(!existsPath(path)) { + path = getAppContextSystemFilePath(normalizedFolder); + createPath(path, a -> { + throw new IllegalStateException(Common.getMessage("Could not create paths! :" + a)); + }, a -> {}); } + return path; } private static void loadGraphics(ViewGraphicLoader loader, @@ -363,7 +365,8 @@ else if (graphic.isDynamicImage()) private static Path getAbsoluteResourcePath(String path) { Path normalizedPath = normalizePath(path); if (!path.equals(normalizedPath.toString())) { - return Path.of(basePath() + File.separator + normalizeSeparator(path)); + Path basePath = basePath(); + return Path.of(basePath + File.separator + normalizeSeparator(path)); } else { return normalizedPath; } @@ -378,6 +381,31 @@ public static String normalizeSeparator(String path) { } private static Path basePath() { - return new File("../").getAbsoluteFile().toPath().normalize(); + String catalinaHome = System.getenv("CATALINA_HOME"); + return Paths.get(catalinaHome); + } + + private static Consumer notExistsPath() { + return a -> { + LOG.error("Could not create path! : " + a); + }; + } + + private static void createPath(Path path, Consumer notExistsPath, Consumer existsPath) { + if(existsPath(path) || path.toFile().mkdirs()) { + existsPath.accept(path); + } else { + notExistsPath.accept(path); + } + } + + private static boolean existsPath(Path path) { + if(!Files.exists(path) && Files.notExists(path)) { + return false; + } else if (!Files.exists(path) && !Files.notExists(path)) { + return false; + } else { + return Files.exists(path); + } } } diff --git a/src/org/scada_lts/web/mvc/controller/ViewEditController.java b/src/org/scada_lts/web/mvc/controller/ViewEditController.java index b0a797c360..3a8dec4b4e 100644 --- a/src/org/scada_lts/web/mvc/controller/ViewEditController.java +++ b/src/org/scada_lts/web/mvc/controller/ViewEditController.java @@ -228,7 +228,11 @@ private void uploadFile(HttpServletRequest request, ViewEditForm form, Map= nextImageId) - nextImageId = index + 1; - } catch (NumberFormatException e) { /* no op */ + if(names != null) { + int index, dot; + for (int i = 0; i < names.length; i++) { + dot = names[i].lastIndexOf('.'); + try { + if (dot == -1) + index = Integer.parseInt(names[i]); + else + index = Integer.parseInt(names[i].substring(0, + dot)); + if (index >= nextImageId) + nextImageId = index + 1; + } catch (NumberFormatException e) { /* no op */ + } } } }