Skip to content

Commit

Permalink
fix: 更新条目时对于https的封面下载失败时没有保留原封面不更新 (#718)
Browse files Browse the repository at this point in the history
* build: upgrade to v0.18.3

* fix: 更新条目时对于https的封面下载失败时没有保留原封面不更新
  • Loading branch information
chivehao authored Oct 29, 2024
1 parent 132c821 commit f341464
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

更新日志文档,版本顺序从新到旧,最新版本在最前(上)面。

# 0.18.3

## 问题修复

- 更新条目时对于https的封面下载失败时没有保留原封面不更新

# 0.18.2

- 条目拉取后没有自动将封面下载到本地
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=0.18.2
version=0.18.3
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static run.ikaros.api.core.attachment.AttachmentConst.COVER_DIRECTORY_ID;

import java.nio.charset.StandardCharsets;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -11,13 +12,15 @@
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import run.ikaros.api.core.attachment.AttachmentUploadCondition;
import run.ikaros.api.infra.utils.FileUtils;
import run.ikaros.api.store.enums.AttachmentReferenceType;
import run.ikaros.server.core.attachment.service.AttachmentService;
import run.ikaros.server.core.subject.SubjectOperator;
import run.ikaros.server.core.subject.event.SubjectRemoveEvent;
import run.ikaros.server.core.subject.event.SubjectUpdateEvent;
import run.ikaros.server.infra.utils.ByteArrUtils;
import run.ikaros.server.store.entity.AttachmentEntity;
import run.ikaros.server.store.entity.AttachmentReferenceEntity;
import run.ikaros.server.store.entity.SubjectEntity;
Expand Down Expand Up @@ -105,8 +108,16 @@ public Mono<Void> onSubjectCoverUpdate(SubjectUpdateEvent event) {
.then(Mono.just(newCover))
.filter(StringUtils::isNotBlank)
.filter(url -> url.startsWith("http"))
.publishOn(Schedulers.boundedElastic())
.flatMap(url -> {
byte[] bytes = restTemplate.getForObject(url, byte[].class);
if (bytes == null || !ByteArrUtils.isBinaryData(bytes)) {
log.warn("Download subject cover fail for url: {}", url);
if (ByteArrUtils.isStringData(bytes)) {
log.warn("Response: {}", new String(bytes, StandardCharsets.UTF_8));
}
return Mono.empty();
}
DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
return attachmentService.upload(AttachmentUploadCondition.builder()
.parentId(COVER_DIRECTORY_ID)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package run.ikaros.server.infra.utils;

import java.nio.charset.StandardCharsets;

public class ByteArrUtils {
/**
* 判断字节数组为二进制数据.
*/
public static boolean isBinaryData(byte[] data) {
for (byte b : data) {
if (b < 32 && b != 9 && b != 10 && b != 13) {
return true; // 存在控制字符,可能是二进制数据
}
}
return false; // 没有控制字符,可能是字符串
}

/**
* 判断字节数组为字符串数据.
*/
public static boolean isStringData(byte[] data) {
try {
String str = new String(data, StandardCharsets.UTF_8);
// 检查解码后的字符串是否包含非可打印字符
return str.chars().allMatch(c -> c >= 32 || c == 9 || c == 10 || c == 13);
} catch (Exception e) {
return false; // 解码失败,认为是二进制数据
}
}

}

0 comments on commit f341464

Please sign in to comment.