Fixes issues relating to reading resources containing a single file

This commit is contained in:
CalDescent 2021-11-12 17:37:33 +00:00
parent d99fae4340
commit 8d44e07c32
2 changed files with 21 additions and 5 deletions

View File

@ -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()));

View File

@ -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);