-
-
Notifications
You must be signed in to change notification settings - Fork 489
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
Datastore / Metadata files / FME API Support #6688
Draft
fxprunayre
wants to merge
2
commits into
main
Choose a base branch
from
423-fme
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
375 changes: 375 additions & 0 deletions
375
core/src/main/java/org/fao/geonet/api/records/attachments/FMEStore.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
core/src/main/resources/config-store/config-fme-overrides.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
fme.api.url=${FME_API_URL:#{null}} | ||
fme.token=${FME_TOKEN:#{null}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ Copyright (C) 2001-2016 Food and Agriculture Organization of the | ||
~ United Nations (FAO-UN), United Nations World Food Programme (WFP) | ||
~ and United Nations Environment Programme (UNEP) | ||
~ | ||
~ This program is free software; you can redistribute it and/or modify | ||
~ it under the terms of the GNU General Public License as published by | ||
~ the Free Software Foundation; either version 2 of the License, or (at | ||
~ your option) any later version. | ||
~ | ||
~ This program is distributed in the hope that it will be useful, but | ||
~ WITHOUT ANY WARRANTY; without even the implied warranty of | ||
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
~ General Public License for more details. | ||
~ | ||
~ You should have received a copy of the GNU General Public License | ||
~ along with this program; if not, write to the Free Software | ||
~ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA | ||
~ | ||
~ Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2, | ||
~ Rome - Italy. email: geonetwork@osgeo.org | ||
--> | ||
<beans | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="http://www.springframework.org/schema/beans" | ||
xmlns:context="http://www.springframework.org/schema/context" | ||
xsi:schemaLocation=" | ||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd | ||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd | ||
"> | ||
|
||
<context:property-placeholder location="classpath:config-store/config-fme-overrides.properties" | ||
file-encoding="UTF-8" | ||
ignore-unresolvable="true" | ||
/> | ||
|
||
<bean id="filesystemStore" | ||
class="org.fao.geonet.api.records.attachments.FMEStore"> | ||
<property name="fmeApiUrl" value="${fme.api.url}"/> | ||
<property name="fmeToken" value="${fme.token}"/> | ||
</bean> | ||
|
||
<bean id="resourceStore" | ||
class="org.fao.geonet.api.records.attachments.ResourceLoggerStore"> | ||
<constructor-arg index="0" ref="filesystemStore"/> | ||
</bean> | ||
|
||
<bean id="resources" class="org.fao.geonet.resources.FileResources"/> | ||
</beans> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ | |
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import jeeves.server.UserSession; | ||
import jeeves.server.context.ServiceContext; | ||
import org.apache.commons.io.IOUtils; | ||
import org.fao.geonet.ApplicationContextHolder; | ||
import org.fao.geonet.api.ApiParams; | ||
import org.fao.geonet.api.ApiUtils; | ||
|
@@ -61,13 +62,17 @@ | |
|
||
import javax.annotation.PostConstruct; | ||
import javax.imageio.ImageIO; | ||
import javax.servlet.ServletOutputStream; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.awt.image.BufferedImage; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardOpenOption; | ||
import java.util.List; | ||
|
||
/** | ||
|
@@ -160,6 +165,9 @@ 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); | ||
|
||
ApiUtils.canViewRecord(metadataUuid, request); | ||
|
||
List<MetadataResource> list = store.getResources(context, metadataUuid, sort, filter, approved); | ||
return list; | ||
} | ||
|
@@ -241,47 +249,62 @@ public MetadataResource putResourceFromURL( | |
} | ||
|
||
@io.swagger.v3.oas.annotations.Operation(summary = "Get a metadata resource") | ||
// @PreAuthorize("permitAll") | ||
@RequestMapping(value = "/{resourceId:.+}", method = RequestMethod.GET) | ||
@ResponseStatus(value = HttpStatus.OK) | ||
@ApiResponses(value = {@ApiResponse(responseCode = "201", description = "Record attachment."), | ||
@ApiResponse(responseCode = "403", description = "Operation not allowed. " | ||
+ "User needs to be able to download the resource.")}) | ||
@ResponseBody | ||
public HttpEntity<byte[]> getResource( | ||
public void getResource( | ||
@Parameter(description = "The metadata UUID", required = true, example = "43d7c186-2187-4bcd-8843-41e575a5ef56") @PathVariable String metadataUuid, | ||
@Parameter(description = "The resource identifier (ie. filename)", required = true) @PathVariable String resourceId, | ||
@Parameter(description = "Use approved version or not", example = "true") @RequestParam(required = false, defaultValue = "true") Boolean approved, | ||
@Parameter(description = "Size (only applies to images). From 1px to 2048px.", example = "200") @RequestParam(required = false) Integer size, | ||
@Parameter(hidden = true) HttpServletRequest request) throws Exception { | ||
@Parameter(hidden = true) HttpServletRequest request, | ||
@Parameter(hidden = true) HttpServletResponse response) throws Exception { | ||
ServiceContext context = ApiUtils.createServiceContext(request); | ||
try (Store.ResourceHolder file = store.getResource(context, metadataUuid, resourceId, approved)) { | ||
|
||
ApiUtils.canViewRecord(metadataUuid, request); | ||
|
||
MultiValueMap<String, String> headers = new HttpHeaders(); | ||
headers.add("Content-Disposition", "inline; filename=\"" + file.getMetadata().getFilename() + "\""); | ||
headers.add("Cache-Control", "no-cache"); | ||
String contentType = getFileContentType(file.getPath()); | ||
headers.add("Content-Type", contentType); | ||
|
||
if (contentType.startsWith("image/") && size != null) { | ||
if (size >= MIN_IMAGE_SIZE && size <= MAX_IMAGE_SIZE) { | ||
BufferedImage image = ImageIO.read(file.getPath().toFile()); | ||
BufferedImage resized = ImageUtil.resize(image, size); | ||
ByteArrayOutputStream output = new ByteArrayOutputStream(); | ||
ImageIO.write(resized, "png", output); | ||
output.flush(); | ||
byte[] imagesB = output.toByteArray(); | ||
output.close(); | ||
return new HttpEntity<>(imagesB, headers); | ||
|
||
ApiUtils.canViewRecord(metadataUuid, request); | ||
|
||
ServletOutputStream out = response.getOutputStream(); | ||
|
||
if (store instanceof FMEStore | ||
|| (store instanceof ResourceLoggerStore && ((ResourceLoggerStore) store).getDecoratedStore() instanceof FMEStore)) { | ||
response.setHeader("Content-Disposition", "inline; filename=\"" + resourceId + "\""); | ||
response.setHeader("Cache-Control", "no-cache"); | ||
|
||
store.streamResource(context, metadataUuid, resourceId, approved, out); | ||
} else { | ||
try (Store.ResourceHolder file = store.getResource(context, metadataUuid, resourceId, approved)) { | ||
response.setHeader("Content-Disposition", "inline; filename=\"" + file.getMetadata().getFilename() + "\""); | ||
response.setHeader("Cache-Control", "no-cache"); | ||
|
||
Path path = file.getPath(); | ||
String contentType = getFileContentType(path); | ||
response.setHeader("Content-Type", contentType); | ||
|
||
if (contentType.startsWith("image/") && size != null) { | ||
if (size >= MIN_IMAGE_SIZE && size <= MAX_IMAGE_SIZE) { | ||
BufferedImage image = ImageIO.read(path.toFile()); | ||
BufferedImage resized = ImageUtil.resize(image, size); | ||
ByteArrayOutputStream output = new ByteArrayOutputStream(); | ||
ImageIO.write(resized, "png", output); | ||
output.flush(); | ||
byte[] imagesB = output.toByteArray(); | ||
output.close(); | ||
out.write(imagesB); | ||
out.flush(); | ||
out.close(); | ||
} else { | ||
throw new IllegalArgumentException(String.format( | ||
"Image can only be resized from %d to %d. You requested %d.", | ||
MIN_IMAGE_SIZE, MAX_IMAGE_SIZE, size)); | ||
} | ||
} else { | ||
throw new IllegalArgumentException(String.format( | ||
"Image can only be resized from %d to %d. You requested %d.", | ||
MIN_IMAGE_SIZE, MAX_IMAGE_SIZE, size)); | ||
IOUtils.copy(Files.newInputStream(path, StandardOpenOption.READ), out); | ||
out.flush(); | ||
out.close(); | ||
Comment on lines
+305
to
+306
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Closing the HttpServletResponse's outputStream is not needed. The servlet container will do it. |
||
} | ||
} else { | ||
return new HttpEntity<>(Files.readAllBytes(file.getPath()), headers); | ||
} | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be better to stream the file to the servlet's output stream instead of loading it in memory as
byte[]
.The temporary
output
file with the resized image should be deleted after used.