From f77aaa7b9e39b25afdc11f3fb883f5b02d8464b7 Mon Sep 17 00:00:00 2001 From: Aziteee Date: Mon, 11 Nov 2024 12:01:12 +0800 Subject: [PATCH] fix: fix the issue that some video links cannot be parsed correctly --- .../hyperlink/handler/HyperLinkBilibiliParser.java | 13 ++++++++++++- .../halo/editor/hyperlink/handler/ParserType.java | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main/java/run/halo/editor/hyperlink/handler/HyperLinkBilibiliParser.java b/src/main/java/run/halo/editor/hyperlink/handler/HyperLinkBilibiliParser.java index ba53c51..3ca6ea1 100644 --- a/src/main/java/run/halo/editor/hyperlink/handler/HyperLinkBilibiliParser.java +++ b/src/main/java/run/halo/editor/hyperlink/handler/HyperLinkBilibiliParser.java @@ -4,6 +4,8 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.RequiredArgsConstructor; +import org.springframework.core.io.buffer.DataBuffer; +import org.springframework.core.io.buffer.DataBufferUtils; import org.springframework.http.HttpHeaders; import org.springframework.http.client.reactive.ReactorClientHttpConnector; import org.springframework.web.reactive.function.client.WebClient; @@ -11,6 +13,7 @@ import run.halo.editor.hyperlink.HttpClientFactory; import run.halo.editor.hyperlink.dto.HyperLinkBaseDTO; import java.net.URI; +import java.nio.charset.StandardCharsets; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -51,7 +54,15 @@ public Mono getHyperLinkDetail(URI linkURI) { httpHeaders.set(HttpHeaders.CONTENT_TYPE, "application/json"); }) .retrieve() - .bodyToMono(String.class)); + .bodyToFlux(DataBuffer.class) + .flatMap(dataBuffer -> { + String content = dataBuffer.toString(StandardCharsets.UTF_8); + DataBufferUtils.release(dataBuffer); + return Mono.just(content); + }) + .reduce(new StringBuilder(), StringBuilder::append) + .filter(stringBuilder -> !stringBuilder.isEmpty()) + .map(StringBuilder::toString)); } public String getQueryParam(URI linkURI) { diff --git a/src/main/java/run/halo/editor/hyperlink/handler/ParserType.java b/src/main/java/run/halo/editor/hyperlink/handler/ParserType.java index 204bca2..16f70f4 100644 --- a/src/main/java/run/halo/editor/hyperlink/handler/ParserType.java +++ b/src/main/java/run/halo/editor/hyperlink/handler/ParserType.java @@ -10,7 +10,7 @@ public enum ParserType { DEFAULT("default", HyperLinkDefaultParser.class), QQMUSIC("(i.)?y.qq.com", HyperLinkQQMusicParser.class), - BILIBILI("[www|m].bilibili.com", HyperLinkBilibiliParser.class); + BILIBILI("(www|m).bilibili.com", HyperLinkBilibiliParser.class); private final String host; private final Class> type;