Skip to content
Merged
Show file tree
Hide file tree
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
24 changes: 9 additions & 15 deletions app/src/main/java/run/halo/feed/RelativeLinkProcessor.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package run.halo.feed;

import static run.halo.feed.RssUtils.genRelativeThumbUri;

import com.google.common.base.Throwables;
import java.net.URI;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.util.Assert;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UriUtils;
import run.halo.app.core.attachment.ThumbnailSize;
import run.halo.app.infra.utils.PathUtils;
import run.halo.feed.telemetry.TelemetryEndpoint;

import java.net.URI;
import java.nio.charset.StandardCharsets;

@Slf4j
public class RelativeLinkProcessor {
private final URI externalUri;
Expand Down Expand Up @@ -66,8 +65,6 @@ private String doProcessForHtml(String html) {
processElementAttr(embeds, "src", false);

return document.body().html();
// var outputHtml = document.body().html();
// return StringEscapeUtils.unescapeHtml4(outputHtml);
}

private void processElementAttr(Elements elements, String attrKey, boolean canThumb) {
Expand All @@ -89,10 +86,7 @@ boolean isNotTelemetryLink(String uri) {
}

private String genThumbUrl(String url, ThumbnailSize size) {
return processLink("/apis/api.storage.halo.run/v1alpha1/thumbnails/-/via-uri?uri="
+ UriUtils.encode(url, StandardCharsets.UTF_8)
+ "&size=" + size.name().toLowerCase()
);
return processLink(genRelativeThumbUri(url, size));
}

private String processLink(String link) {
Expand All @@ -101,18 +95,18 @@ private String processLink(String link) {
}
var contextPath = StringUtils.defaultIfBlank(externalUri.getPath(), "/");
var linkUri = UriComponentsBuilder.fromUriString(URI.create(link).toASCIIString())
.build(true);
.build(true);
var builder = UriComponentsBuilder.fromUriString(externalUri.toString());
if (shouldAppendPath(contextPath, link)) {
builder.pathSegment(linkUri.getPathSegments().toArray(new String[0]));
} else {
builder.replacePath(linkUri.getPath());
}
return builder.query(linkUri.getQuery())
.fragment(linkUri.getFragment())
.build(true)
.toUri()
.toString();
.fragment(linkUri.getFragment())
.build(true)
.toUri()
.toString();
}

private static boolean shouldAppendPath(String contextPath, String link) {
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/run/halo/feed/RssUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package run.halo.feed;

import java.nio.charset.StandardCharsets;
import lombok.experimental.UtilityClass;
import org.springframework.web.util.UriUtils;
import run.halo.app.core.attachment.ThumbnailSize;

@UtilityClass
public class RssUtils {

public static String genRelativeThumbUri(String url, ThumbnailSize size) {
return "/apis/api.storage.halo.run/v1alpha1/thumbnails/-/via-uri?uri="
+ UriUtils.encode(url, StandardCharsets.UTF_8)
+ "&size=" + size.name().toLowerCase();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package run.halo.feed.provider;

import static run.halo.feed.RssUtils.genRelativeThumbUri;

import java.util.List;
import java.util.function.Function;
import lombok.Data;
Expand Down Expand Up @@ -91,10 +93,7 @@ public Mono<RSS2> handler(ServerRequest request) {
}

private String genThumbUrl(String url) {
return externalLinkProcessor.processLink(
"/apis/api.storage.halo.run/v1alpha1/thumbnails/-/via-uri?uri=" + url + "&size="
+ ThumbnailSize.M.name().toLowerCase()
);
return externalLinkProcessor.processLink(genRelativeThumbUri(url, ThumbnailSize.M));
}

protected Flux<PostWithContent> listPosts(ServerRequest request) {
Expand Down
Loading