Skip to content

Commit

Permalink
fix: missing enclosure attribute length (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
guqing authored Dec 16, 2024
1 parent 04a39c9 commit 65b9f23
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
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();
}
}

0 comments on commit 65b9f23

Please sign in to comment.