From d48ba91f539471a7bd7ad5e8b5eb1332ba918baa Mon Sep 17 00:00:00 2001 From: "AAVN\\pvquan" Date: Wed, 25 Dec 2024 14:45:26 +0700 Subject: [PATCH] Refactor FileUtils --- .../com/axonivy/market/util/FileUtils.java | 29 +++++++++---------- .../impl/ReleasePreviewServiceImplTest.java | 2 -- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/marketplace-service/src/main/java/com/axonivy/market/util/FileUtils.java b/marketplace-service/src/main/java/com/axonivy/market/util/FileUtils.java index a60d119c..59ce28e3 100644 --- a/marketplace-service/src/main/java/com/axonivy/market/util/FileUtils.java +++ b/marketplace-service/src/main/java/com/axonivy/market/util/FileUtils.java @@ -12,6 +12,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.Comparator; +import java.util.stream.Stream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -21,9 +22,7 @@ public class FileUtils { public static File createFile(String fileName) throws IOException { File file = new File(fileName); File parentDir = file.getParentFile(); - if (parentDir != null && !parentDir.exists() && !parentDir.mkdirs()) { - throw new IOException("Failed to create directory: " + parentDir.getAbsolutePath()); - } + createDirectoryFromFile(parentDir); if (!file.exists() && !file.createNewFile()) { throw new IOException("Failed to create file: " + file.getAbsolutePath()); } @@ -46,22 +45,16 @@ public static void unzip(MultipartFile file, String location) throws IOException Path entryPath = Paths.get(entry.getName()).normalize(); Path resolvedPath = extractDir.toPath().resolve(entryPath).normalize(); - // Ensure the resolved path is within the target directory if (!resolvedPath.startsWith(extractDir.toPath())) { throw new IOException("Entry is outside the target dir: " + entry.getName()); } File outFile = resolvedPath.toFile(); if (entry.isDirectory()) { - if (!outFile.mkdirs() && !outFile.isDirectory()) { - throw new IOException("Failed to create directory: " + outFile); - } + createDirectoryFromFile(outFile); } else { File parentDir = outFile.getParentFile(); - if (parentDir != null && !parentDir.exists() && !parentDir.mkdirs()) { - throw new IOException("Failed to create parent directory: " + parentDir); - } - + createDirectoryFromFile(parentDir); try (FileOutputStream fos = new FileOutputStream(outFile)) { byte[] buffer = new byte[1024]; int length; @@ -77,6 +70,11 @@ public static void unzip(MultipartFile file, String location) throws IOException } } + private static void createDirectoryFromFile(File file) throws IOException { + if (file != null && !file.mkdirs() && !file.isDirectory()) { + throw new IOException("Failed to create directory: " + file); + } + } public static void prepareUnZipDirectory(Path directory) throws IOException { clearDirectory(directory); @@ -85,10 +83,11 @@ public static void prepareUnZipDirectory(Path directory) throws IOException { public static void clearDirectory(Path path) throws IOException { if (Files.exists(path)) { - Files.walk(path) - .sorted(Comparator.reverseOrder()) - .map(Path::toFile) - .forEach(java.io.File::delete); + try (Stream paths = Files.walk(path)) { + paths.sorted(Comparator.reverseOrder()) + .map(Path::toFile) + .forEach(File::delete); + } } } diff --git a/marketplace-service/src/test/java/com/axonivy/market/service/impl/ReleasePreviewServiceImplTest.java b/marketplace-service/src/test/java/com/axonivy/market/service/impl/ReleasePreviewServiceImplTest.java index 480f3d0a..cf18025d 100644 --- a/marketplace-service/src/test/java/com/axonivy/market/service/impl/ReleasePreviewServiceImplTest.java +++ b/marketplace-service/src/test/java/com/axonivy/market/service/impl/ReleasePreviewServiceImplTest.java @@ -21,8 +21,6 @@ import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; -; - @ExtendWith(MockitoExtension.class) class ReleasePreviewServiceImplTest {