diff --git a/CHANGELOG.MD b/CHANGELOG.MD index 0802301b8..48d099151 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -5,7 +5,8 @@ # 0.18.1 - 修复Console快速拉取条目后无法定位到对应条目的问题 -- 条目封面字段更新后如果是http开头则自动下载到本地 +- 修复条目封面字段更新后如果是http开头则自动下载到本地的问题 +- 配置文件新增是否开启lucene引擎索引初始化配置 # 0.18.0 diff --git a/gradle.properties b/gradle.properties index 2804593a3..0d7beedff 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1 +1 @@ -version=0.18.0 +version=0.18.1 diff --git a/server/src/main/java/run/ikaros/server/core/attachment/listener/AttachmentSubjectCoverChangeListener.java b/server/src/main/java/run/ikaros/server/core/attachment/listener/AttachmentSubjectCoverChangeListener.java index 365bfc1a6..0ecb415ae 100644 --- a/server/src/main/java/run/ikaros/server/core/attachment/listener/AttachmentSubjectCoverChangeListener.java +++ b/server/src/main/java/run/ikaros/server/core/attachment/listener/AttachmentSubjectCoverChangeListener.java @@ -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; @@ -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; @@ -101,51 +101,25 @@ public Mono 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 -> @@ -161,6 +135,30 @@ public Mono 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 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); + } } + diff --git a/server/src/main/java/run/ikaros/server/core/subject/service/impl/SubjectSyncServiceImpl.java b/server/src/main/java/run/ikaros/server/core/subject/service/impl/SubjectSyncServiceImpl.java index ecfbb397f..5925052cf 100644 --- a/server/src/main/java/run/ikaros/server/core/subject/service/impl/SubjectSyncServiceImpl.java +++ b/server/src/main/java/run/ikaros/server/core/subject/service/impl/SubjectSyncServiceImpl.java @@ -145,6 +145,15 @@ public Mono 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 = diff --git a/server/src/main/java/run/ikaros/server/search/IndicesConfiguration.java b/server/src/main/java/run/ikaros/server/search/IndicesConfiguration.java new file mode 100644 index 000000000..ccf6f17b0 --- /dev/null +++ b/server/src/main/java/run/ikaros/server/search/IndicesConfiguration.java @@ -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 { +} diff --git a/server/src/main/java/run/ikaros/server/search/IndicesInitializer.java b/server/src/main/java/run/ikaros/server/search/IndicesInitializer.java index 5ba1849bc..a2c134e9f 100644 --- a/server/src/main/java/run/ikaros/server/search/IndicesInitializer.java +++ b/server/src/main/java/run/ikaros/server/search/IndicesInitializer.java @@ -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(); } diff --git a/server/src/main/java/run/ikaros/server/search/IndicesProperties.java b/server/src/main/java/run/ikaros/server/search/IndicesProperties.java new file mode 100644 index 000000000..a2ec8dfb3 --- /dev/null +++ b/server/src/main/java/run/ikaros/server/search/IndicesProperties.java @@ -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; + } +} diff --git a/server/src/main/java/run/ikaros/server/store/repository/SubjectRepository.java b/server/src/main/java/run/ikaros/server/store/repository/SubjectRepository.java index 59e4921a9..4a525f188 100644 --- a/server/src/main/java/run/ikaros/server/store/repository/SubjectRepository.java +++ b/server/src/main/java/run/ikaros/server/store/repository/SubjectRepository.java @@ -10,7 +10,11 @@ public interface SubjectRepository extends R2dbcRepository { - @NotNull Mono existsById(@NotNull Long id); + @NotNull + Mono existsById(@NotNull Long id); + + Mono findByNsfwAndTypeAndNameAndSummary(Boolean nsfw, SubjectType type, + String name, String summary); Flux findAllBy(Pageable pageable);