Fixed slow validation issue caused by loading the entire resource into memory.

This commit is contained in:
CalDescent 2023-05-08 12:17:44 +01:00
parent 05b4ecd4ed
commit fc10b61193
2 changed files with 14 additions and 4 deletions

View File

@ -107,7 +107,7 @@ public enum Service {
}
// Require valid JSON
byte[] data = FilesystemUtils.getSingleFileContents(path);
byte[] data = FilesystemUtils.getSingleFileContents(path, 25*1024);
String json = new String(data, StandardCharsets.UTF_8);
try {
objectMapper.readTree(json);
@ -201,7 +201,9 @@ public enum Service {
return ValidationResult.OK;
}
byte[] data = FilesystemUtils.getSingleFileContents(path);
// Load the first 25KB of data. This only needs to be long enough to check the prefix
// and also to allow for possible additional future validation of smaller files.
byte[] data = FilesystemUtils.getSingleFileContents(path, 25*1024);
long size = FilesystemUtils.getDirectorySize(path);
// Validate max size if needed

View File

@ -228,12 +228,18 @@ public class FilesystemUtils {
* @throws IOException
*/
public static byte[] getSingleFileContents(Path path) throws IOException {
return getSingleFileContents(path, null);
}
public static byte[] getSingleFileContents(Path path, Integer maxLength) throws IOException {
byte[] data = null;
// TODO: limit the file size that can be loaded into memory
// If the path is a file, read the contents directly
if (path.toFile().isFile()) {
data = Files.readAllBytes(path);
int fileSize = (int)path.toFile().length();
maxLength = maxLength != null ? Math.min(maxLength, fileSize) : fileSize;
data = FilesystemUtils.readFromFile(path.toString(), 0, maxLength);
}
// Or if it's a directory, only load file contents if there is a single file inside it
@ -242,7 +248,9 @@ public class FilesystemUtils {
if (files.length == 1) {
Path filePath = Paths.get(path.toString(), files[0]);
if (filePath.toFile().isFile()) {
data = Files.readAllBytes(filePath);
int fileSize = (int)filePath.toFile().length();
maxLength = maxLength != null ? Math.min(maxLength, fileSize) : fileSize;
data = FilesystemUtils.readFromFile(filePath.toString(), 0, maxLength);
}
}
}