Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 4.2.x] Attachments API / Set content-length and close input stream when retrieving a metadata resource #8557

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading