-
-
Notifications
You must be signed in to change notification settings - Fork 512
Logo improvements #9322
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
Open
fxprunayre
wants to merge
9
commits into
main
Choose a base branch
from
44-logoapi
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.
Open
Logo improvements #9322
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e6d5b3a
Logo improvements
fxprunayre a105949
Logo improvements / Review feedback.
fxprunayre db575e4
Logo improvements / Test fix and comments.
fxprunayre 528cff4
Logo improvements / Test fix for PDF
fxprunayre f1d9811
Logo improvements / Some sonar comments.
fxprunayre a7ef951
Logo improvements / Editor group dropdown logo path updated.
fxprunayre 4fef3cd
Logo improvements / Fallback PNG was not transparent.
fxprunayre 4663910
Logo improvements / Add defensive response headers for SVG logos
josegar74 895855a
Logo improvements / Harvester API / Serve logo directly without a red…
josegar74 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 hidden or 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 hidden or 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
102 changes: 102 additions & 0 deletions
102
services/src/main/java/org/fao/geonet/api/LogoUtils.java
This file contains hidden or 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,102 @@ | ||
| /* | ||
| * Copyright (C) 2001-2026 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 | ||
| */ | ||
|
|
||
| package org.fao.geonet.api; | ||
|
|
||
| import jeeves.server.context.ServiceContext; | ||
| import org.apache.commons.io.FileUtils; | ||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.fao.geonet.api.records.attachments.AttachmentsApi; | ||
| import org.fao.geonet.resources.Resources; | ||
| import org.springframework.web.context.request.WebRequest; | ||
|
|
||
| import javax.servlet.http.HttpServletResponse; | ||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.attribute.FileTime; | ||
|
|
||
| public final class LogoUtils { | ||
| public static final String API_GET_LOGO_NOTE = "If last-modified header " | ||
| + "is present it is used to check if the logo has been modified since " | ||
| + "the header date. If it hasn't been modified returns an empty 304 Not" | ||
| + " Modified response. If modified returns the image. If there is " | ||
| + "no logo then returns a transparent 1x1 px PNG image."; | ||
|
|
||
| private static final int SIX_HOURS = 60 * 60 * 6; | ||
|
|
||
| private static final String TRANSPARENT_1_X_1_PNG_BASE64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR" | ||
| + "42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=="; | ||
|
|
||
| private static final byte[] TRANSPARENT_1_X_1_PNG = org.apache.commons.codec.binary.Base64.decodeBase64(TRANSPARENT_1_X_1_PNG_BASE64); | ||
|
|
||
| private LogoUtils() { | ||
| } | ||
|
|
||
| public static Resources.ResourceHolder getImage(Resources resources, | ||
| ServiceContext serviceContext, | ||
| String logoRef) throws IOException { | ||
| final Path logosDir = resources.locateLogosDir(serviceContext); | ||
| final Path harvesterLogosDir = resources.locateHarvesterLogosDir(serviceContext); | ||
| Resources.ResourceHolder image = null; | ||
| if (isLocalLogoRef(logoRef)) { | ||
| image = resources.getImage(serviceContext, logoRef, logosDir); | ||
| if (image == null) { | ||
| image = resources.getImage(serviceContext, logoRef, harvesterLogosDir); | ||
| } | ||
| } | ||
| return image; | ||
| } | ||
|
|
||
| public static void writeImageOrTransparentLogo(WebRequest webRequest, | ||
| HttpServletResponse response, | ||
| Resources.ResourceHolder image) throws IOException { | ||
| if (image != null) { | ||
| FileTime lastModifiedTime = image.getLastModifiedTime(); | ||
| response.setDateHeader("Expires", System.currentTimeMillis() + SIX_HOURS * 1000L); | ||
| if (webRequest.checkNotModified(lastModifiedTime.toMillis())) { | ||
| return; | ||
| } | ||
| response.setContentType(AttachmentsApi.getFileContentType(image.getPath().getFileName().toString())); | ||
| response.setContentLength((int) Files.size(image.getPath())); | ||
| response.addHeader("Cache-Control", "max-age=" + SIX_HOURS + ", public"); | ||
| FileUtils.copyFile(image.getPath().toFile(), response.getOutputStream()); | ||
| return; | ||
| } | ||
|
|
||
| if (webRequest.checkNotModified(0L)) { | ||
| return; | ||
| } | ||
| response.setContentType("image/png"); | ||
| response.setContentLength(TRANSPARENT_1_X_1_PNG.length); | ||
| response.addHeader("Cache-Control", "max-age=" + SIX_HOURS + ", public"); | ||
| response.getOutputStream().write(TRANSPARENT_1_X_1_PNG); | ||
| } | ||
|
|
||
| private static boolean isLocalLogoRef(String logoRef) { | ||
| return StringUtils.isNotBlank(logoRef) | ||
| && !logoRef.startsWith("http://") | ||
| && !logoRef.startsWith("https://") | ||
| && !logoRef.startsWith("https//"); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.