From 8d44e07c32439926bdaa38b9c37f030fa0efc559 Mon Sep 17 00:00:00 2001 From: CalDescent Date: Fri, 12 Nov 2021 17:37:33 +0000 Subject: [PATCH] Fixes issues relating to reading resources containing a single file --- .../qortal/api/resource/ArbitraryResource.java | 16 ++++++++++++++-- .../qortal/arbitrary/ArbitraryDataReader.java | 10 +++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/qortal/api/resource/ArbitraryResource.java b/src/main/java/org/qortal/api/resource/ArbitraryResource.java index 3cd4467d..68a759db 100644 --- a/src/main/java/org/qortal/api/resource/ArbitraryResource.java +++ b/src/main/java/org/qortal/api/resource/ArbitraryResource.java @@ -24,6 +24,7 @@ import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import org.apache.commons.lang3.ArrayUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.qortal.api.*; @@ -485,11 +486,22 @@ public class ArbitraryResource { continue; } } + java.nio.file.Path outputPath = arbitraryDataReader.getFilePath(); + + if (filepath == null || filepath.isEmpty()) { + // No file path supplied - so check if this is a single file resource + String[] files = ArrayUtils.removeElement(outputPath.toFile().list(), ".qortal"); + if (files.length == 1) { + // This is a single file resource + filepath = files[0]; + } + } // TODO: limit file size that can be read into memory - java.nio.file.Path path = Paths.get(arbitraryDataReader.getFilePath().toString(), filepath); + java.nio.file.Path path = Paths.get(outputPath.toString(), filepath); if (!Files.exists(path)) { - throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_CRITERIA); + String message = String.format("No file exists at filepath: %s", filepath); + throw ApiExceptionFactory.INSTANCE.createCustomException(request, ApiError.INVALID_CRITERIA, message); } byte[] data = Files.readAllBytes(path); response.setContentType(context.getMimeType(path.toString())); diff --git a/src/main/java/org/qortal/arbitrary/ArbitraryDataReader.java b/src/main/java/org/qortal/arbitrary/ArbitraryDataReader.java index 4261e715..38b5ce26 100644 --- a/src/main/java/org/qortal/arbitrary/ArbitraryDataReader.java +++ b/src/main/java/org/qortal/arbitrary/ArbitraryDataReader.java @@ -167,9 +167,13 @@ public class ArbitraryDataReader { private void createUncompressedDirectory() { try { + // Create parent directory Files.createDirectories(this.uncompressedPath.getParent()); + // Ensure child directory doesn't already exist + FileUtils.deleteDirectory(this.uncompressedPath.toFile()); + } catch (IOException e) { - throw new IllegalStateException("Unable to create temp directory"); + throw new IllegalStateException("Unable to create uncompressed directory"); } } @@ -403,8 +407,8 @@ public class ArbitraryDataReader { if (source == null || !source.exists()) { throw new IllegalStateException("Source directory doesn't exist"); } - if (dest == null || !dest.exists()) { - throw new IllegalStateException("Destination directory doesn't exist"); + if (dest == null) { + throw new IllegalStateException("Destination is null"); } // Ensure destination directory doesn't exist FileUtils.deleteDirectory(dest);