|
4 | 4 | import lombok.extern.slf4j.Slf4j;
|
5 | 5 | import org.apache.commons.lang3.StringUtils;
|
6 | 6 | import org.springframework.context.event.EventListener;
|
| 7 | +import org.springframework.core.io.buffer.DataBufferFactory; |
| 8 | +import org.springframework.core.io.buffer.DefaultDataBufferFactory; |
7 | 9 | import org.springframework.stereotype.Component;
|
| 10 | +import org.springframework.web.client.RestTemplate; |
8 | 11 | import reactor.core.publisher.Mono;
|
9 | 12 | import run.ikaros.api.core.attachment.AttachmentConst;
|
| 13 | +import run.ikaros.api.core.attachment.AttachmentUploadCondition; |
| 14 | +import run.ikaros.api.infra.utils.FileUtils; |
10 | 15 | import run.ikaros.api.store.enums.AttachmentReferenceType;
|
11 | 16 | import run.ikaros.api.store.enums.AttachmentType;
|
12 | 17 | import run.ikaros.server.core.attachment.service.AttachmentService;
|
| 18 | +import run.ikaros.server.core.subject.SubjectOperator; |
13 | 19 | import run.ikaros.server.core.subject.event.SubjectRemoveEvent;
|
14 | 20 | import run.ikaros.server.core.subject.event.SubjectUpdateEvent;
|
15 | 21 | import run.ikaros.server.store.entity.AttachmentEntity;
|
16 | 22 | import run.ikaros.server.store.entity.AttachmentReferenceEntity;
|
17 | 23 | import run.ikaros.server.store.entity.SubjectEntity;
|
18 | 24 | import run.ikaros.server.store.repository.AttachmentReferenceRepository;
|
19 | 25 | import run.ikaros.server.store.repository.AttachmentRepository;
|
| 26 | +import run.ikaros.server.store.repository.SubjectRepository; |
20 | 27 |
|
21 | 28 | @Slf4j
|
22 | 29 | @Component
|
23 | 30 | public class AttachmentSubjectCoverChangeListener {
|
24 | 31 | private final AttachmentRepository attachmentRepository;
|
25 | 32 | private final AttachmentService attachmentService;
|
26 | 33 | private final AttachmentReferenceRepository attachmentReferenceRepository;
|
| 34 | + private final RestTemplate restTemplate = new RestTemplate(); |
| 35 | + private final SubjectOperator subjectOperator; |
| 36 | + private final SubjectRepository subjectRepository; |
27 | 37 |
|
28 | 38 | /**
|
29 | 39 | * Construct.
|
30 | 40 | */
|
31 | 41 | public AttachmentSubjectCoverChangeListener(
|
32 | 42 | AttachmentRepository attachmentRepository,
|
33 | 43 | AttachmentService attachmentService,
|
34 |
| - AttachmentReferenceRepository attachmentReferenceRepository) { |
| 44 | + AttachmentReferenceRepository attachmentReferenceRepository, |
| 45 | + SubjectOperator subjectOperator, SubjectRepository subjectRepository) { |
35 | 46 | this.attachmentRepository = attachmentRepository;
|
36 | 47 | this.attachmentService = attachmentService;
|
37 | 48 | this.attachmentReferenceRepository = attachmentReferenceRepository;
|
| 49 | + this.subjectOperator = subjectOperator; |
| 50 | + this.subjectRepository = subjectRepository; |
38 | 51 | }
|
39 | 52 |
|
40 | 53 |
|
@@ -75,7 +88,7 @@ public Mono<Void> onSubjectCoverUpdate(SubjectUpdateEvent event) {
|
75 | 88 |
|
76 | 89 | String oldCover = oldEntity.getCover();
|
77 | 90 | String newCover = newEntity.getCover();
|
78 |
| - if (oldCover.equals(newCover)) { |
| 91 | + if (oldCover.equals(newCover) && !oldCover.startsWith("http")) { |
79 | 92 | return Mono.empty();
|
80 | 93 | }
|
81 | 94 |
|
@@ -111,6 +124,43 @@ public Mono<Void> onSubjectCoverUpdate(SubjectUpdateEvent event) {
|
111 | 124 | entity)))
|
112 | 125 | )
|
113 | 126 |
|
| 127 | + // 当是网络url的时候,附件是找不到的,此时为空走这里的逻辑 |
| 128 | + // 条目三方同步会发布更新事件 |
| 129 | + |
| 130 | + .then(Mono.just(newCover)) |
| 131 | + .filter(StringUtils::isNotBlank) |
| 132 | + .filter(url -> url.startsWith("http")) |
| 133 | + .flatMap(url -> { |
| 134 | + String coverFileName = StringUtils.isNotBlank(newEntity.getNameCn()) |
| 135 | + ? newEntity.getNameCn() : newEntity.getName(); |
| 136 | + coverFileName = |
| 137 | + System.currentTimeMillis() + "-" + coverFileName |
| 138 | + + "." + FileUtils.parseFilePostfix(FileUtils.parseFileName(url)); |
| 139 | + byte[] bytes = restTemplate.getForObject(url, byte[].class); |
| 140 | + DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory(); |
| 141 | + return attachmentService.upload(AttachmentUploadCondition.builder() |
| 142 | + .parentId(AttachmentConst.COVER_DIRECTORY_ID) |
| 143 | + .name(coverFileName) |
| 144 | + .dataBufferFlux(Mono.just(dataBufferFactory.wrap(bytes)).flux()) |
| 145 | + .build()); |
| 146 | + }) |
| 147 | + .flatMap(attachment -> |
| 148 | + subjectRepository.findById(newEntity.getId()) |
| 149 | + .map(entity -> entity.setCover(attachment.getUrl())) |
| 150 | + .flatMap(subjectRepository::save) |
| 151 | + .flatMap(entity -> |
| 152 | + attachmentReferenceRepository.findByTypeAndAttachmentIdAndReferenceId( |
| 153 | + AttachmentReferenceType.SUBJECT, attachment.getId(), entity.getId()) |
| 154 | + .switchIfEmpty(Mono.just(AttachmentReferenceEntity.builder() |
| 155 | + .type(AttachmentReferenceType.SUBJECT) |
| 156 | + .attachmentId(attachment.getId()) |
| 157 | + .referenceId(entity.getId()) |
| 158 | + .build())) |
| 159 | + .flatMap(attachmentReferenceRepository::save) |
| 160 | + ) |
| 161 | + |
| 162 | + ) |
| 163 | + |
114 | 164 | .then();
|
115 | 165 | }
|
116 | 166 | }
|
0 commit comments