fix(core): handle not found on Storage.size

This commit is contained in:
tchiotludo
2021-05-28 14:52:24 +02:00
parent 5cc771dd5b
commit d110782f50
5 changed files with 23 additions and 8 deletions

View File

@@ -21,7 +21,7 @@ import java.util.regex.Pattern;
public interface StorageInterface {
InputStream get(URI uri) throws FileNotFoundException;
Long size(URI uri) throws IOException;
Long size(URI uri) throws FileNotFoundException;
URI put(URI uri, InputStream data) throws IOException;

View File

@@ -6,6 +6,7 @@ import org.apache.commons.lang3.tuple.Pair;
import java.io.*;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Comparator;
@@ -30,7 +31,6 @@ public class LocalStorage implements StorageInterface {
return Paths.get(config.getBasePath().toAbsolutePath().toString(), uri.toString());
}
@SuppressWarnings("ResultOfMethodCallIgnored")
private void createDirectory(URI append) {
File file;
@@ -57,22 +57,26 @@ public class LocalStorage implements StorageInterface {
}
@Override
public Long size(URI uri) throws IOException {
return Files.size(getPath(URI.create(uri.getPath())));
public Long size(URI uri) throws FileNotFoundException {
try {
return Files.size(getPath(URI.create(uri.getPath())));
} catch (NoSuchFileException e) {
throw new FileNotFoundException("Unable to find file at '" + uri + "'");
} catch (IOException e) {
throw new FileNotFoundException("Unable to find file at '" + uri + "' with message '" + e.getMessage() + "'");
}
}
@Override
public URI put(URI uri, InputStream data) throws IOException {
this.createDirectory(uri);
try (OutputStream outStream = new FileOutputStream(getPath(uri).toFile())) {
try (data; OutputStream outStream = new FileOutputStream(getPath(uri).toFile())) {
byte[] buffer = new byte[8 * 1024];
int bytesRead;
while ((bytesRead = data.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
} finally {
data.close();
}
return URI.create("kestra://" + uri.getPath());

View File

@@ -69,6 +69,10 @@ class LocalStorageTest {
assertThat(storageInterface.size(new URI("/file/storage/put.yml")), is(77L));
assertThrows(FileNotFoundException.class, () -> {
assertThat(storageInterface.size(new URI("/file/storage/muissing.yml")), is(76L));
});
boolean delete = storageInterface.delete(put);
assertThat(delete, is(true));

View File

@@ -14,6 +14,7 @@ import io.micronaut.http.hateoas.JsonError;
import io.micronaut.http.hateoas.Link;
import lombok.extern.slf4j.Slf4j;
import java.io.FileNotFoundException;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collections;
@@ -117,6 +118,11 @@ public class ErrorController {
return jsonError(request, e, HttpStatus.NOT_FOUND, "Not Found");
}
@Error(global = true)
public HttpResponse<JsonError> notFound(HttpRequest<?> request, FileNotFoundException e) {
return jsonError(request, e, HttpStatus.NOT_FOUND, "Not Found");
}
private static HttpResponse<JsonError> jsonError(JsonError jsonError, HttpStatus status, String reason) {
return HttpResponse
.<JsonError>status(status, reason)

View File

@@ -44,6 +44,7 @@ import org.reactivestreams.Publisher;
import io.micronaut.core.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
@@ -371,7 +372,7 @@ public class ExecutionController {
public HttpResponse<FileMetas> filesize(
String executionId,
@QueryValue(value = "path") URI path
) throws IOException {
) throws FileNotFoundException {
HttpResponse<FileMetas> httpResponse =this.validateFile(executionId, path, "/api/v1/executions/{executionId}/file/metas?path=" + path);
if (httpResponse != null) {
return httpResponse;