mirror of
https://github.com/bisq-network/bisq.git
synced 2025-02-24 15:10:44 +01:00
Merge pull request #5900 from stejbac/fix-resync-blocks-from-resources
Fix resync BSQ blocks from resources
This commit is contained in:
commit
601163eeb3
2 changed files with 44 additions and 17 deletions
|
@ -24,6 +24,12 @@ import com.google.common.io.Files;
|
|||
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.IOException;
|
|||
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 class FileUtil {
|
|||
}
|
||||
}
|
||||
|
||||
public static List<String> 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<Path> 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.
|
||||
|
|
|
@ -21,6 +21,7 @@ import bisq.core.dao.state.GenesisTxInfo;
|
|||
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.Inject;
|
|||
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 @@ public class BsqBlocksStorageService {
|
|||
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<String> 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();
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue