diff --git a/common/src/main/java/bisq/common/file/FileUtil.java b/common/src/main/java/bisq/common/file/FileUtil.java index f69c2df88d3..1f5771e5010 100644 --- a/common/src/main/java/bisq/common/file/FileUtil.java +++ b/common/src/main/java/bisq/common/file/FileUtil.java @@ -24,6 +24,12 @@ import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; + +import java.nio.file.FileSystem; +import java.nio.file.FileSystems; import java.nio.file.Path; import java.nio.file.Paths; @@ -33,9 +39,12 @@ import java.io.InputStream; import java.util.Arrays; +import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; import lombok.extern.slf4j.Slf4j; @@ -168,6 +177,35 @@ public static void resourceToFile(String resourcePath, } } + public static List listResourceDirectory(String directoryName) throws IOException, ResourceNotFoundException { + URL url = Thread.currentThread().getContextClassLoader().getResource(directoryName); + if (url == null) { + throw new ResourceNotFoundException(directoryName); + } + URI uri; + try { + uri = url.toURI(); + } catch (URISyntaxException e) { + throw new IOException(e); + } + if (url.getProtocol().equals("file")) { + File dir = new File(uri); + String[] filenames = dir.list(); + if (filenames != null) { + return List.of(filenames); + } + } else if (url.getProtocol().equals("jar")) { + try (FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.emptyMap()); + Stream filePaths = java.nio.file.Files.walk(fileSystem.getPath(directoryName), 1)) { //NOPMD + return filePaths + .skip(1) + .map(path -> path.getFileName().toString()) + .collect(Collectors.toUnmodifiableList()); + } + } + throw new IOException("Failed to list resource directory: " + directoryName); + } + public static void renameFile(File oldFile, File newFile) throws IOException { if (Utilities.isWindows()) { // Work around an issue on Windows whereby you can't rename over existing files. diff --git a/core/src/main/java/bisq/core/dao/state/storage/BsqBlocksStorageService.java b/core/src/main/java/bisq/core/dao/state/storage/BsqBlocksStorageService.java index 6b6002e0fc4..cecce4ac136 100644 --- a/core/src/main/java/bisq/core/dao/state/storage/BsqBlocksStorageService.java +++ b/core/src/main/java/bisq/core/dao/state/storage/BsqBlocksStorageService.java @@ -21,6 +21,7 @@ import bisq.core.dao.state.model.blockchain.Block; import bisq.common.config.Config; +import bisq.common.file.FileUtil; import bisq.common.proto.persistable.PersistenceProtoResolver; import protobuf.BaseBlock; @@ -29,10 +30,6 @@ import javax.inject.Named; import javax.inject.Singleton; -import org.apache.commons.io.FileUtils; - -import java.net.URL; - import java.io.File; import java.util.LinkedList; @@ -116,27 +113,19 @@ void copyFromResources(String postFix) { return; } - URL dirUrl = getClass().getClassLoader().getResource(resourceDir); - if (dirUrl == null) { - log.info("Directory {} in resources does not exist.", resourceDir); - return; - } - File dir = new File(dirUrl.toURI()); - String[] fileNames = dir.list(); - if (fileNames == null) { - log.info("No files in directory. {}", dir.getAbsolutePath()); + List fileNames = FileUtil.listResourceDirectory(resourceDir); + if (fileNames.isEmpty()) { + log.info("No files in directory. {}", resourceDir); return; } if (!storageDir.exists()) { storageDir.mkdir(); } for (String fileName : fileNames) { - URL url = getClass().getClassLoader().getResource(resourceDir + File.separator + fileName); - File resourceFile = new File(url.toURI()); File destinationFile = new File(storageDir, fileName); - FileUtils.copyFile(resourceFile, destinationFile); + FileUtil.resourceToFile(resourceDir + "/" + fileName, destinationFile); } - log.info("Copying {} resource files took {} ms", fileNames.length, System.currentTimeMillis() - ts); + log.info("Copying {} resource files took {} ms", fileNames.size(), System.currentTimeMillis() - ts); } catch (Throwable e) { e.printStackTrace(); }