mirror of
https://github.com/Qortal/qortal.git
synced 2025-06-19 14:11:22 +00:00
Fixes issues relating to reading resources containing a single file
This commit is contained in:
parent
d99fae4340
commit
8d44e07c32
@ -24,6 +24,7 @@ import javax.ws.rs.core.Context;
|
|||||||
import javax.ws.rs.core.MediaType;
|
import javax.ws.rs.core.MediaType;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.ArrayUtils;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.qortal.api.*;
|
import org.qortal.api.*;
|
||||||
@ -485,11 +486,22 @@ public class ArbitraryResource {
|
|||||||
continue;
|
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
|
// 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)) {
|
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);
|
byte[] data = Files.readAllBytes(path);
|
||||||
response.setContentType(context.getMimeType(path.toString()));
|
response.setContentType(context.getMimeType(path.toString()));
|
||||||
|
@ -167,9 +167,13 @@ public class ArbitraryDataReader {
|
|||||||
|
|
||||||
private void createUncompressedDirectory() {
|
private void createUncompressedDirectory() {
|
||||||
try {
|
try {
|
||||||
|
// Create parent directory
|
||||||
Files.createDirectories(this.uncompressedPath.getParent());
|
Files.createDirectories(this.uncompressedPath.getParent());
|
||||||
|
// Ensure child directory doesn't already exist
|
||||||
|
FileUtils.deleteDirectory(this.uncompressedPath.toFile());
|
||||||
|
|
||||||
} catch (IOException e) {
|
} 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()) {
|
if (source == null || !source.exists()) {
|
||||||
throw new IllegalStateException("Source directory doesn't exist");
|
throw new IllegalStateException("Source directory doesn't exist");
|
||||||
}
|
}
|
||||||
if (dest == null || !dest.exists()) {
|
if (dest == null) {
|
||||||
throw new IllegalStateException("Destination directory doesn't exist");
|
throw new IllegalStateException("Destination is null");
|
||||||
}
|
}
|
||||||
// Ensure destination directory doesn't exist
|
// Ensure destination directory doesn't exist
|
||||||
FileUtils.deleteDirectory(dest);
|
FileUtils.deleteDirectory(dest);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user