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: 条目拉取后没有自动将封面下载到本地 #715

Merged
merged 3 commits into from
Oct 22, 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
3 changes: 2 additions & 1 deletion CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
# 0.18.1

- 修复Console快速拉取条目后无法定位到对应条目的问题
- 条目封面字段更新后如果是http开头则自动下载到本地
- 修复条目封面字段更新后如果是http开头则自动下载到本地的问题
- 配置文件新增是否开启lucene引擎索引初始化配置

# 0.18.0

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.0
version=0.18.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package run.ikaros.server.core.attachment.listener;

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

import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -9,11 +11,9 @@
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import reactor.core.publisher.Mono;
import run.ikaros.api.core.attachment.AttachmentConst;
import run.ikaros.api.core.attachment.AttachmentUploadCondition;
import run.ikaros.api.infra.utils.FileUtils;
import run.ikaros.api.store.enums.AttachmentReferenceType;
import run.ikaros.api.store.enums.AttachmentType;
import run.ikaros.server.core.attachment.service.AttachmentService;
import run.ikaros.server.core.subject.SubjectOperator;
import run.ikaros.server.core.subject.event.SubjectRemoveEvent;
Expand Down Expand Up @@ -101,51 +101,25 @@ public Mono<Void> onSubjectCoverUpdate(SubjectUpdateEvent event) {
oldCoverAttId
).doOnSuccess(unused ->
log.debug("Delete attachment Reference by type and att id and sub id.")))
// update new attachment that move to cover dir
.then(attachmentRepository.findByTypeAndParentIdAndName(AttachmentType.Directory,
AttachmentConst.ROOT_DIRECTORY_ID, AttachmentConst.COVER_DIR_NAME))
.map(AttachmentEntity::getId)
.flatMap(coverDirAttId -> attachmentRepository.findByUrl(newCover)
.filter(entity -> !coverDirAttId.equals(entity.getParentId()))
.map(entity -> entity.setParentId(coverDirAttId)))
.flatMap(attachmentRepository::save)
.map(AttachmentEntity::getId)
.flatMap(attId -> attachmentReferenceRepository
.findByTypeAndAttachmentIdAndReferenceId(AttachmentReferenceType.SUBJECT,
attId, newEntity.getId())
.switchIfEmpty(attachmentReferenceRepository.save(
AttachmentReferenceEntity.builder()
.type(AttachmentReferenceType.SUBJECT)
.attachmentId(attId)
.referenceId(newEntity.getId())
.build()
).doOnSuccess(entity ->
log.debug("Create attachment Reference by type and att id and sub id: [{}].",
entity)))
)

// 当是网络url的时候,附件是找不到的,此时为空走这里的逻辑
// 条目三方同步会发布更新事件

.then(Mono.just(newCover))
.filter(StringUtils::isNotBlank)
.filter(url -> url.startsWith("http"))
.flatMap(url -> {
String coverFileName = StringUtils.isNotBlank(newEntity.getNameCn())
? newEntity.getNameCn() : newEntity.getName();
coverFileName =
System.currentTimeMillis() + "-" + coverFileName
+ "." + FileUtils.parseFilePostfix(FileUtils.parseFileName(url));
byte[] bytes = restTemplate.getForObject(url, byte[].class);
DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
return attachmentService.upload(AttachmentUploadCondition.builder()
.parentId(AttachmentConst.COVER_DIRECTORY_ID)
.name(coverFileName)
.parentId(COVER_DIRECTORY_ID)
.name(getCoverName(newEntity))
.dataBufferFlux(Mono.just(dataBufferFactory.wrap(bytes)).flux())
.build());
})
.flatMap(attachment ->
subjectRepository.findById(newEntity.getId())
subjectRepository.findByNsfwAndTypeAndNameAndSummary(
newEntity.getNsfw(), newEntity.getType(),
newEntity.getName(), newEntity.getSummary())
.map(entity -> entity.setCover(attachment.getUrl()))
.flatMap(subjectRepository::save)
.flatMap(entity ->
Expand All @@ -161,6 +135,30 @@ public Mono<Void> onSubjectCoverUpdate(SubjectUpdateEvent event) {

)

.then(moveCover2CoverDir(newEntity))

.then();
}

private String getCoverName(SubjectEntity subjectEntity) {
final String url = subjectEntity.getCover();
String coverFileName = StringUtils.isNotBlank(subjectEntity.getNameCn())
? subjectEntity.getNameCn() : subjectEntity.getName();
coverFileName =
System.currentTimeMillis() + "-" + coverFileName
+ "." + FileUtils.parseFilePostfix(FileUtils.parseFileName(url));
return coverFileName;
}

/**
* update new attachment that move to cover dir.
*/
private Mono<AttachmentEntity> moveCover2CoverDir(SubjectEntity newEntity) {
return attachmentRepository.findByUrl(newEntity.getCover())
.filter(entity -> !entity.getParentId().equals(COVER_DIRECTORY_ID))
.map(entity -> entity.setParentId(COVER_DIRECTORY_ID)
.setName(getCoverName(newEntity)))
.flatMap(attachmentRepository::save);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ public Mono<Void> sync(@Nullable Long subjectId, SubjectSyncPlatform platform,
.flatMap(entity -> copyProperties(subject, entity, "id"));
}
})
.map(entity -> {
if (entity.getCreateTime() == null) {
entity.setCreateTime(LocalDateTime.now());
}
if (entity.getUpdateTime() == null) {
entity.setUpdateTime(LocalDateTime.now());
}
return entity;
})
.flatMap(entity -> subjectRepository.save(entity)
.map(newEntity -> {
SubjectUpdateEvent event =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package run.ikaros.server.search;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(IndicesProperties.class)
public class IndicesConfiguration {
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,22 @@
public class IndicesInitializer {

private final IndicesService indicesService;
private final IndicesProperties indicesProperties;

public IndicesInitializer(IndicesService indicesService) {
public IndicesInitializer(IndicesService indicesService, IndicesProperties indicesProperties) {
this.indicesService = indicesService;
this.indicesProperties = indicesProperties;
}

/**
* Init indices.
*/
@Async
@EventListener(SchemeInitializedEvent.class)
public void whenSchemeInitialized(SchemeInitializedEvent event) throws InterruptedException {
if (!indicesProperties.getInitializer().isEnabled()) {
return;
}
initSubjectIndices();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package run.ikaros.server.search;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* Properties for indices.
*
* @see IndicesConfiguration
*/
@Data
@ConfigurationProperties(prefix = "ikaros.indices")
public class IndicesProperties {
private final Initializer initializer = new Initializer();

@Data
public static class Initializer {
private boolean enabled = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@

public interface SubjectRepository extends R2dbcRepository<SubjectEntity, Long> {

@NotNull Mono<Boolean> existsById(@NotNull Long id);
@NotNull
Mono<Boolean> existsById(@NotNull Long id);

Mono<SubjectEntity> findByNsfwAndTypeAndNameAndSummary(Boolean nsfw, SubjectType type,
String name, String summary);

Flux<SubjectEntity> findAllBy(Pageable pageable);

Expand Down
Loading