-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: video subtitle attachment relation new when AttachmentReference…
…SaveEvent and EpisodeAttachmentUpdateEvent (#496)
- Loading branch information
Showing
7 changed files
with
151 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 0 additions & 20 deletions
20
api/src/main/java/run/ikaros/api/core/subject/Subtitle.java
This file was deleted.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
...r/src/main/java/run/ikaros/server/core/attachment/event/AttachmentReferenceSaveEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package run.ikaros.server.core.attachment.event; | ||
|
||
import lombok.Getter; | ||
import org.springframework.context.ApplicationEvent; | ||
import run.ikaros.server.store.entity.AttachmentReferenceEntity; | ||
|
||
@Getter | ||
public class AttachmentReferenceSaveEvent extends ApplicationEvent { | ||
|
||
private final AttachmentReferenceEntity entity; | ||
|
||
public AttachmentReferenceSaveEvent(Object source, AttachmentReferenceEntity entity) { | ||
super(source); | ||
this.entity = entity; | ||
} | ||
|
||
} |
98 changes: 98 additions & 0 deletions
98
...n/java/run/ikaros/server/core/attachment/listener/AttachmentRelVideoSubtitleListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package run.ikaros.server.core.attachment.listener; | ||
|
||
import static run.ikaros.api.store.enums.AttachmentType.File; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.stereotype.Component; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
import run.ikaros.api.core.attachment.VideoSubtitle; | ||
import run.ikaros.api.infra.utils.FileUtils; | ||
import run.ikaros.api.store.enums.AttachmentReferenceType; | ||
import run.ikaros.api.store.enums.AttachmentRelationType; | ||
import run.ikaros.server.core.attachment.event.AttachmentReferenceSaveEvent; | ||
import run.ikaros.server.core.attachment.event.EpisodeAttachmentUpdateEvent; | ||
import run.ikaros.server.store.entity.AttachmentEntity; | ||
import run.ikaros.server.store.entity.AttachmentReferenceEntity; | ||
import run.ikaros.server.store.entity.AttachmentRelationEntity; | ||
import run.ikaros.server.store.repository.AttachmentRelationRepository; | ||
import run.ikaros.server.store.repository.AttachmentRepository; | ||
|
||
@Slf4j | ||
@Component | ||
public class AttachmentRelVideoSubtitleListener { | ||
private final AttachmentRepository attachmentRepository; | ||
private final AttachmentRelationRepository attachmentRelationRepository; | ||
|
||
public AttachmentRelVideoSubtitleListener( | ||
AttachmentRepository attachmentRepository, | ||
AttachmentRelationRepository attachmentRelationRepository) { | ||
this.attachmentRepository = attachmentRepository; | ||
this.attachmentRelationRepository = attachmentRelationRepository; | ||
} | ||
|
||
/** | ||
* 监听附件关系保存事件{@link AttachmentReferenceSaveEvent}, | ||
* 如果保存的类型是剧集引用,则查询一次对应的附件是否存在同名字幕, | ||
* 如果存在则在附件关系表新增一条视频字幕类型的关系记录. | ||
*/ | ||
@EventListener(AttachmentReferenceSaveEvent.class) | ||
public Mono<Void> onAttachmentReferenceSaveEvent(AttachmentReferenceSaveEvent event) { | ||
AttachmentReferenceEntity entity = event.getEntity(); | ||
if (!AttachmentReferenceType.EPISODE.equals(entity.getType())) { | ||
return Mono.empty(); | ||
} | ||
Long attachmentId = entity.getAttachmentId(); | ||
|
||
return findAttachmentSubtitlesAndSaveRelationIfNotExists(attachmentId); | ||
} | ||
|
||
|
||
/** | ||
* 监听剧集资源匹配更新事件{@link EpisodeAttachmentUpdateEvent}, 查询对应的剧集附件, | ||
* 在数据库中,是否存在相同名称的字幕文件, | ||
* 如果存在则新增一条附件间关系{@link run.ikaros.api.store.enums.AttachmentRelationType#VIDEO_SUBTITLE}. | ||
*/ | ||
@EventListener(EpisodeAttachmentUpdateEvent.class) | ||
public Mono<Void> onSubjectAdd(EpisodeAttachmentUpdateEvent event) { | ||
Long attachmentId = event.getAttachmentId(); | ||
return findAttachmentSubtitlesAndSaveRelationIfNotExists(attachmentId); | ||
} | ||
|
||
private Mono<Void> findAttachmentSubtitlesAndSaveRelationIfNotExists(Long attachmentId) { | ||
return attachmentRepository.findById(attachmentId) | ||
.map(AttachmentEntity::getName) | ||
.flatMapMany(this::findAllAttachmentSubtitles) | ||
.map(VideoSubtitle::getAttachmentId) | ||
.flatMap(relationAttId -> attachmentRelationRepository | ||
.existsByTypeAndAttachmentIdAndRelationAttachmentId( | ||
AttachmentRelationType.VIDEO_SUBTITLE, attachmentId, relationAttId) | ||
.filter(exists -> !exists) | ||
.map(exists -> AttachmentRelationEntity.builder() | ||
.type(AttachmentRelationType.VIDEO_SUBTITLE) | ||
.attachmentId(attachmentId) | ||
.relationAttachmentId(relationAttId) | ||
.build()) | ||
.flatMap(attachmentRelationEntity -> attachmentRelationRepository | ||
.save(attachmentRelationEntity) | ||
.doOnSuccess(entity -> log.debug("Save new attachment relation record" | ||
+ " for type={} attId={} relAttId={}", | ||
AttachmentRelationType.VIDEO_SUBTITLE, | ||
attachmentId, relationAttId)))) | ||
.then(); | ||
} | ||
|
||
private Flux<VideoSubtitle> findAllAttachmentSubtitles(String attachmentName) { | ||
String postfix = FileUtils.parseFilePostfix(attachmentName); | ||
attachmentName = attachmentName.substring(0, attachmentName.indexOf(postfix)); | ||
return attachmentRepository.findAllByTypeAndNameLike(File, attachmentName + "%") | ||
.filter(attachmentEntity -> attachmentEntity.getName().endsWith("ass")) | ||
.map(attachmentEntity -> VideoSubtitle.builder() | ||
.attachmentId(attachmentEntity.getId()) | ||
.name(attachmentEntity.getName()) | ||
.url(attachmentEntity.getUrl()) | ||
.build()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters