From fbfe16e7847e22ef3ebd96d94b997f2487146bd6 Mon Sep 17 00:00:00 2001 From: Athou Date: Sat, 20 Aug 2022 11:35:37 +0200 Subject: [PATCH] return relative urls to rely less on publicUrl where possible (#1016) --- .../java/com/commafeed/backend/feed/FeedUtils.java | 12 ++++++------ .../java/com/commafeed/frontend/model/Entry.java | 11 +++++------ .../com/commafeed/frontend/model/Subscription.java | 4 ++-- .../commafeed/frontend/resource/CategoryREST.java | 14 ++++---------- .../com/commafeed/frontend/resource/FeedREST.java | 13 ++----------- 5 files changed, 19 insertions(+), 35 deletions(-) diff --git a/commafeed-server/src/main/java/com/commafeed/backend/feed/FeedUtils.java b/commafeed-server/src/main/java/com/commafeed/backend/feed/FeedUtils.java index a73f84f3..65f99ac6 100644 --- a/commafeed-server/src/main/java/com/commafeed/backend/feed/FeedUtils.java +++ b/commafeed-server/src/main/java/com/commafeed/backend/feed/FeedUtils.java @@ -474,11 +474,11 @@ public class FeedUtils { return url.startsWith("/") || url.startsWith("#") || !url.matches("^\\w+\\:\\/\\/.*"); } - public static String getFaviconUrl(FeedSubscription subscription, String publicUrl) { - return removeTrailingSlash(publicUrl) + "/rest/feed/favicon/" + subscription.getId(); + public static String getFaviconUrl(FeedSubscription subscription) { + return "rest/feed/favicon/" + subscription.getId(); } - public static String proxyImages(String content, String publicUrl) { + public static String proxyImages(String content) { if (StringUtils.isBlank(content)) { return content; } @@ -488,7 +488,7 @@ public class FeedUtils { for (Element element : elements) { String href = element.attr("src"); if (href != null) { - String proxy = proxyImage(href, publicUrl); + String proxy = proxyImage(href); element.attr("src", proxy); } } @@ -496,11 +496,11 @@ public class FeedUtils { return doc.body().html(); } - public static String proxyImage(String url, String publicUrl) { + public static String proxyImage(String url) { if (StringUtils.isBlank(url)) { return url; } - return removeTrailingSlash(publicUrl) + "/rest/server/proxy?u=" + imageProxyEncoder(url); + return "rest/server/proxy?u=" + imageProxyEncoder(url); } public static String rot13(String msg) { diff --git a/commafeed-server/src/main/java/com/commafeed/frontend/model/Entry.java b/commafeed-server/src/main/java/com/commafeed/frontend/model/Entry.java index 61922f0e..eac70ad6 100644 --- a/commafeed-server/src/main/java/com/commafeed/frontend/model/Entry.java +++ b/commafeed-server/src/main/java/com/commafeed/frontend/model/Entry.java @@ -105,7 +105,7 @@ public class Entry implements Serializable { @ApiModelProperty(value = "tags", required = true) private List tags; - public static Entry build(FeedEntryStatus status, String publicUrl, boolean proxyImages) { + public static Entry build(FeedEntryStatus status, boolean proxyImages) { Entry entry = new Entry(); FeedEntry feedEntry = status.getEntry(); @@ -124,23 +124,22 @@ public class Entry implements Serializable { entry.setFeedId(String.valueOf(sub.getId())); entry.setFeedUrl(sub.getFeed().getUrl()); entry.setFeedLink(sub.getFeed().getLink()); - entry.setIconUrl(FeedUtils.getFaviconUrl(sub, publicUrl)); + entry.setIconUrl(FeedUtils.getFaviconUrl(sub)); entry.setTags(status.getTags().stream().map(FeedEntryTag::getName).collect(Collectors.toList())); if (content != null) { entry.setRtl(FeedUtils.isRTL(feedEntry)); entry.setTitle(content.getTitle()); - entry.setContent(proxyImages ? FeedUtils.proxyImages(content.getContent(), publicUrl) : content.getContent()); + entry.setContent(proxyImages ? FeedUtils.proxyImages(content.getContent()) : content.getContent()); entry.setAuthor(content.getAuthor()); entry.setEnclosureType(content.getEnclosureType()); entry.setEnclosureUrl(proxyImages && StringUtils.contains(content.getEnclosureType(), "image") - ? FeedUtils.proxyImage(content.getEnclosureUrl(), publicUrl) + ? FeedUtils.proxyImage(content.getEnclosureUrl()) : content.getEnclosureUrl()); entry.setMediaDescription(content.getMediaDescription()); - entry.setMediaThumbnailUrl( - proxyImages ? FeedUtils.proxyImage(content.getMediaThumbnailUrl(), publicUrl) : content.getMediaThumbnailUrl()); + entry.setMediaThumbnailUrl(proxyImages ? FeedUtils.proxyImage(content.getMediaThumbnailUrl()) : content.getMediaThumbnailUrl()); entry.setMediaThumbnailWidth(content.getMediaThumbnailWidth()); entry.setMediaThumbnailHeight(content.getMediaThumbnailHeight()); diff --git a/commafeed-server/src/main/java/com/commafeed/frontend/model/Subscription.java b/commafeed-server/src/main/java/com/commafeed/frontend/model/Subscription.java index 4c19db7b..56285bf0 100644 --- a/commafeed-server/src/main/java/com/commafeed/frontend/model/Subscription.java +++ b/commafeed-server/src/main/java/com/commafeed/frontend/model/Subscription.java @@ -62,7 +62,7 @@ public class Subscription implements Serializable { @ApiModelProperty(value = "JEXL string evaluated on new entries to mark them as read if they do not match") private String filter; - public static Subscription build(FeedSubscription subscription, String publicUrl, UnreadCount unreadCount) { + public static Subscription build(FeedSubscription subscription, UnreadCount unreadCount) { Date now = new Date(); FeedCategory category = subscription.getCategory(); Feed feed = subscription.getFeed(); @@ -74,7 +74,7 @@ public class Subscription implements Serializable { sub.setErrorCount(feed.getErrorCount()); sub.setFeedUrl(feed.getUrl()); sub.setFeedLink(feed.getLink()); - sub.setIconUrl(FeedUtils.getFaviconUrl(subscription, publicUrl)); + sub.setIconUrl(FeedUtils.getFaviconUrl(subscription)); sub.setLastRefresh(feed.getLastUpdated()); sub.setNextRefresh((feed.getDisabledUntil() != null && feed.getDisabledUntil().before(now)) ? null : feed.getDisabledUntil()); sub.setUnread(unreadCount.getUnreadCount()); diff --git a/commafeed-server/src/main/java/com/commafeed/frontend/resource/CategoryREST.java b/commafeed-server/src/main/java/com/commafeed/frontend/resource/CategoryREST.java index c7c83fba..32413acc 100644 --- a/commafeed-server/src/main/java/com/commafeed/frontend/resource/CategoryREST.java +++ b/commafeed-server/src/main/java/com/commafeed/frontend/resource/CategoryREST.java @@ -146,18 +146,14 @@ public class CategoryREST { offset, limit + 1, order, true, onlyIds, tag); for (FeedEntryStatus status : list) { - entries.getEntries() - .add(Entry.build(status, config.getApplicationSettings().getPublicUrl(), - config.getApplicationSettings().getImageProxyEnabled())); + entries.getEntries().add(Entry.build(status, config.getApplicationSettings().getImageProxyEnabled())); } } else if (STARRED.equals(id)) { entries.setName("Starred"); List starred = feedEntryStatusDAO.findStarred(user, newerThanDate, offset, limit + 1, order, !onlyIds); for (FeedEntryStatus status : starred) { - entries.getEntries() - .add(Entry.build(status, config.getApplicationSettings().getPublicUrl(), - config.getApplicationSettings().getImageProxyEnabled())); + entries.getEntries().add(Entry.build(status, config.getApplicationSettings().getImageProxyEnabled())); } } else { FeedCategory parent = feedCategoryDAO.findById(user, Long.valueOf(id)); @@ -169,9 +165,7 @@ public class CategoryREST { offset, limit + 1, order, true, onlyIds, tag); for (FeedEntryStatus status : list) { - entries.getEntries() - .add(Entry.build(status, config.getApplicationSettings().getPublicUrl(), - config.getApplicationSettings().getImageProxyEnabled())); + entries.getEntries().add(Entry.build(status, config.getApplicationSettings().getImageProxyEnabled())); } entries.setName(parent.getName()); } else { @@ -476,7 +470,7 @@ public class CategoryREST { if (id == null && subscription.getCategory() == null || subscription.getCategory() != null && Objects.equals(subscription.getCategory().getId(), id)) { UnreadCount uc = unreadCount.get(subscription.getId()); - Subscription sub = Subscription.build(subscription, config.getApplicationSettings().getPublicUrl(), uc); + Subscription sub = Subscription.build(subscription, uc); category.getFeeds().add(sub); } } diff --git a/commafeed-server/src/main/java/com/commafeed/frontend/resource/FeedREST.java b/commafeed-server/src/main/java/com/commafeed/frontend/resource/FeedREST.java index 4506e471..6f4a0d42 100644 --- a/commafeed-server/src/main/java/com/commafeed/frontend/resource/FeedREST.java +++ b/commafeed-server/src/main/java/com/commafeed/frontend/resource/FeedREST.java @@ -177,9 +177,7 @@ public class FeedREST { entryKeywords, newerThanDate, offset, limit + 1, order, true, onlyIds, null); for (FeedEntryStatus status : list) { - entries.getEntries() - .add(Entry.build(status, config.getApplicationSettings().getPublicUrl(), - config.getApplicationSettings().getImageProxyEnabled())); + entries.getEntries().add(Entry.build(status, config.getApplicationSettings().getImageProxyEnabled())); } boolean hasMore = entries.getEntries().size() > limit; @@ -346,7 +344,7 @@ public class FeedREST { return Response.status(Status.NOT_FOUND).build(); } UnreadCount unreadCount = feedSubscriptionService.getUnreadCount(user).get(id); - return Response.ok(Subscription.build(sub, config.getApplicationSettings().getPublicUrl(), unreadCount)).build(); + return Response.ok(Subscription.build(sub, unreadCount)).build(); } @GET @@ -522,13 +520,6 @@ public class FeedREST { @Timed public Response importOpml(@ApiParam(hidden = true) @SecurityCheck User user, @ApiParam(value = "ompl file", required = true) @FormDataParam("file") InputStream input) { - - String publicUrl = config.getApplicationSettings().getPublicUrl(); - if (StringUtils.isBlank(publicUrl)) { - throw new WebApplicationException( - Response.status(Status.INTERNAL_SERVER_ERROR).entity("Set the public URL in the admin section.").build()); - } - if (CommaFeedApplication.USERNAME_DEMO.equals(user.getName())) { return Response.status(Status.FORBIDDEN).entity("Import is disabled for the demo account").build(); }