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

fix: missing enclosure attribute length #49

Merged
merged 1 commit into from
Dec 16, 2024
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
5 changes: 4 additions & 1 deletion app/src/main/java/run/halo/feed/RssCacheManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ private Mono<String> generateRssXml(Mono<RSS2> loader) {
.doOnNext(prop -> builder.withExtractRssTags(prop.getRssExtraTags()));

return Mono.when(rssMono, generatorMono, extractTagsMono)
.then(Mono.fromSupplier(builder::toXmlString));
// toXmlString is a blocking operation
.then(Mono.fromCallable(builder::toXmlString)
.subscribeOn(Schedulers.boundedElastic())
);
}

@EventListener(PluginConfigUpdatedEvent.class)
Expand Down
44 changes: 42 additions & 2 deletions app/src/main/java/run/halo/feed/RssXmlBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,28 @@
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.lang.NonNull;
import org.springframework.util.CollectionUtils;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UriUtils;
import reactor.netty.http.client.HttpClient;
import run.halo.feed.telemetry.TelemetryEndpoint;

@Slf4j
public class RssXmlBuilder {
static final String UA =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) "
+ "Chrome/131.0.0.0 Safari/537.36";
private final WebClient webClient = WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(HttpClient.create()
.followRedirect(true))
)
.build();

private RSS2 rss2;
private String generator = "Halo v2.0";
private String extractRssTags;
Expand Down Expand Up @@ -174,10 +189,17 @@ private void createItemElementToChannel(Element channel, RSS2.Item item) {
}

if (StringUtils.isNotBlank(item.getEnclosureUrl())) {
itemElement.addElement("enclosure")
var enclosureElement = itemElement.addElement("enclosure")
.addAttribute("url", item.getEnclosureUrl())
.addAttribute("length", item.getEnclosureLength())
.addAttribute("type", item.getEnclosureType());

var enclosureLength = item.getEnclosureLength();
if (StringUtils.isBlank(enclosureLength)) {
// https://www.rssboard.org/rss-validator/docs/error/MissingAttribute.html
var fileBytes = getFileSizeBytes(item.getEnclosureUrl());
enclosureLength = String.valueOf(fileBytes);
}
enclosureElement.addAttribute("length", enclosureLength);
}

nullSafeList(item.getCategories())
Expand Down Expand Up @@ -258,4 +280,22 @@ static String instantToString(Instant instant) {
return instant.atOffset(ZoneOffset.UTC)
.format(DateTimeFormatter.RFC_1123_DATE_TIME);
}

@NonNull
private Long getFileSizeBytes(String url) {
return webClient.get()
.uri(url)
.header(HttpHeaders.USER_AGENT, UA)
.retrieve()
.toBodilessEntity()
.map(HttpEntity::getHeaders)
.mapNotNull(headers -> headers.getFirst(HttpHeaders.CONTENT_LENGTH))
.map(Long::parseLong)
.doOnError(e -> log.debug("Failed to get file size from url: {}", url,
Throwables.getRootCause(e))
)
.onErrorReturn(0L)
.blockOptional()
.orElseThrow();
}
}
Loading