Skip to content

Commit

Permalink
Attachments API / Set content-length and close input stream when retr…
Browse files Browse the repository at this point in the history
…ieving a metadata resource
  • Loading branch information
josegar74 committed Dec 12, 2024
1 parent 3389c9e commit 126e92f
Showing 1 changed file with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* =============================================================================
* === Copyright (C) 2001-2023 Food and Agriculture Organization of the
* === Copyright (C) 2001-2024 Food and Agriculture Organization of the
* === United Nations (FAO-UN), United Nations World Food Programme (WFP)
* === and United Nations Environment Programme (UNEP)
* ===
Expand Down Expand Up @@ -68,6 +68,7 @@
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -171,8 +172,7 @@ public List<MetadataResource> getAllResources(
@RequestParam(required = false, defaultValue = FilesystemStore.DEFAULT_FILTER) String filter,
@Parameter(hidden = true) HttpServletRequest request) throws Exception {
ServiceContext context = ApiUtils.createServiceContext(request);
List<MetadataResource> list = store.getResources(context, metadataUuid, sort, filter, approved);
return list;
return store.getResources(context, metadataUuid, sort, filter, approved);
}

@io.swagger.v3.oas.annotations.Operation(summary = "Delete all uploaded metadata resources")
Expand Down Expand Up @@ -291,7 +291,11 @@ public void getResource(
MIN_IMAGE_SIZE, MAX_IMAGE_SIZE, size));
}
} else {
StreamUtils.copy(Files.newInputStream(file.getPath()), response.getOutputStream());
response.setContentLengthLong(Files.size(file.getPath()));

try (InputStream inputStream = Files.newInputStream(file.getPath())) {
StreamUtils.copy(inputStream, response.getOutputStream());
}
}
}
}
Expand Down

0 comments on commit 126e92f

Please sign in to comment.