Skip to content

Commit

Permalink
#2718 Mapping path static Resources - if path from system settings do…
Browse files Browse the repository at this point in the history
…es not exists then path from context application; base path from CATALINA_HOME if set relative path;
  • Loading branch information
Limraj committed Nov 26, 2023
1 parent 351d2c5 commit 193a4ec
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 28 deletions.
54 changes: 41 additions & 13 deletions src/org/scada_lts/utils/UploadFileUtils.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -316,12 +318,13 @@ private static List<Path> getImageSystemFilePaths(Supplier<String> 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;
}

Expand All @@ -335,14 +338,13 @@ private static Path getImageSystemFileToWritePath(Supplier<String> 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,
Expand All @@ -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;
}
Expand All @@ -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<Path> notExistsPath() {
return a -> {
LOG.error("Could not create path! : " + a);
};
}

private static void createPath(Path path, Consumer<Path> notExistsPath, Consumer<Path> 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);
}
}
}
34 changes: 19 additions & 15 deletions src/org/scada_lts/web/mvc/controller/ViewEditController.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,11 @@ private void uploadFile(HttpServletRequest request, ViewEditForm form, Map<Strin
String errorMessage = LocalizableMessage.getMessage(Common.getBundle(request),"viewEdit.upload.failed");
if (file != null) {
if(isToUploads(file))
upload(request, form, file);
try {
upload(request, form, file);
} catch (Exception ex) {
errors.put("errorMessage", ex.getMessage());
}
else {
LOG.warn("Image file is invalid.");
errors.put("errorMessage", errorMessage);
Expand All @@ -249,8 +253,6 @@ private void upload(HttpServletRequest request, ViewEditForm form, MultipartFile

// Make sure the directory exists.
File dir = path.toFile();
dir.mkdirs();

String fileName = file.getOriginalFilename();
if(fileName != null) {
saveFile(form, bytes, dir, fileName.toLowerCase());
Expand Down Expand Up @@ -291,18 +293,20 @@ private int getNextImageId(File uploadDir) {
nextImageId = 1;

String[] names = uploadDir.list();
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 */
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 */
}
}
}
}
Expand Down

0 comments on commit 193a4ec

Please sign in to comment.