fix(media): 修正转码媒资 id 解析与存量分集补登记

This commit is contained in:
Zhou Weidong committed 2026-10-22 14:59:16 +08:00
1 parent a35b02dd18
commit 5ef5dd8eaf
11 files changed
+427 -31

No files matched your search

@@ -9,13 +9,17 @@ import com.fruitshorts.media.dal.mapper.MediaAssetMapper;
import com.fruitshorts.media.dal.mapper.MediaUploadTaskMapper;
import com.fruitshorts.media.service.MediaService;
import com.fruitshorts.media.service.MediaStorageService;
import com.fruitshorts.media.service.EpisodeVideoSourcePort;
import com.fruitshorts.media.service.TranscodeService;
import com.fruitshorts.media.vo.TranscodeTriggerRespVO;
import com.fruitshorts.media.vo.UploadCompleteReqVO;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@@ -33,12 +37,19 @@ import static org.mockito.Mockito.verify;
class MediaServiceTranscodePipelineTest {
private static final Long ASSET_ID = 88L;
private static final Long EPISODE_ID = 31L;
private static final Long DRAMA_ID = 7L;
private static final String SOURCE_URL = "/files/episode/202610/video.mp4";
private static final String OBJECT_KEY = "episode/202610/video.mp4";
@TempDir
Path tempDir;
private MediaAssetMapper mediaAssetMapper;
private MediaUploadTaskMapper uploadTaskMapper;
private MediaStorageService storageService;
private TranscodeService transcodeService;
private EpisodeVideoSourcePort episodeVideoSourcePort;
private MediaService mediaService;
@BeforeEach
@@ -47,9 +58,10 @@ class MediaServiceTranscodePipelineTest {
uploadTaskMapper = mock(MediaUploadTaskMapper.class);
storageService = mock(MediaStorageService.class);
transcodeService = mock(TranscodeService.class);
episodeVideoSourcePort = mock(EpisodeVideoSourcePort.class);
mediaService = new MediaService(
new MediaProperties(), storageService, mediaAssetMapper,
uploadTaskMapper, transcodeService);
uploadTaskMapper, transcodeService, episodeVideoSourcePort);
}
@Test
@@ -87,6 +99,7 @@ class MediaServiceTranscodePipelineTest {
@Test
void should_return_not_found_when_triggering_transcode_for_missing_media() {
given(mediaAssetMapper.selectOne(any())).willReturn(null);
given(episodeVideoSourcePort.resolve(404L)).willReturn(Optional.empty());
assertThatThrownBy(() -> mediaService.triggerTranscode(404L))
.isInstanceOf(com.fruitshorts.common.exception.ServiceException.class)
@@ -110,6 +123,48 @@ class MediaServiceTranscodePipelineTest {
verify(mediaAssetMapper).updateById(asset);
}
@Test
void should_resolve_existing_media_asset_by_episode_video_url() {
MediaAsset asset = asset("video.mp4");
given(mediaAssetMapper.selectOne(any())).willReturn(null).willReturn(asset);
given(storageService.objectKeyOf(SOURCE_URL)).willReturn(OBJECT_KEY);
given(episodeVideoSourcePort.resolve(EPISODE_ID))
.willReturn(Optional.of(episodeSource()));
given(transcodeService.trigger(ASSET_ID, SOURCE_URL))
.willReturn(new TranscodeTriggerRespVO(100L, TranscodeService.STATUS_QUEUED));
TranscodeTriggerRespVO result = mediaService.triggerTranscode(EPISODE_ID);
assertThat(result.getTaskId()).isEqualTo(100L);
verify(episodeVideoSourcePort).link(EPISODE_ID, ASSET_ID);
verify(transcodeService).trigger(ASSET_ID, SOURCE_URL);
}
@Test
void should_register_legacy_media_asset_when_episode_file_exists() throws Exception {
Path file = tempDir.resolve("video.mp4");
Files.writeString(file, "legacy-video");
given(mediaAssetMapper.selectOne(any())).willReturn(null);
doAnswer(invocation -> {
MediaAsset inserted = invocation.getArgument(0);
inserted.setId(ASSET_ID);
return 1;
}).when(mediaAssetMapper).insert(any(MediaAsset.class));
given(storageService.objectKeyOf(SOURCE_URL)).willReturn(OBJECT_KEY);
given(storageService.fileFor(OBJECT_KEY)).willReturn(file);
given(episodeVideoSourcePort.resolve(EPISODE_ID))
.willReturn(Optional.of(episodeSource()));
given(transcodeService.trigger(ASSET_ID, SOURCE_URL))
.willReturn(new TranscodeTriggerRespVO(100L, TranscodeService.STATUS_QUEUED));
TranscodeTriggerRespVO result = mediaService.triggerTranscode(EPISODE_ID);
assertThat(result.getTaskId()).isEqualTo(100L);
verify(episodeVideoSourcePort).link(EPISODE_ID, ASSET_ID);
verify(mediaAssetMapper).insert(any(MediaAsset.class));
verify(transcodeService).trigger(ASSET_ID, SOURCE_URL);
}
private void givenCompletedUpload(String fileName) {
String objectKey = "episode/202610/" + fileName;
MediaUploadTask task = new MediaUploadTask();
@@ -143,4 +198,9 @@ class MediaServiceTranscodePipelineTest {
asset.setStatus("uploaded");
return asset;
}
private EpisodeVideoSourcePort.EpisodeVideoSource episodeSource() {
return new EpisodeVideoSourcePort.EpisodeVideoSource(
EPISODE_ID, DRAMA_ID, ASSET_ID, SOURCE_URL, 120);
}
}