Skip to content

Commit

Permalink
#324 Refactor podcast episode retrieval and media file
Browse files Browse the repository at this point in the history
handling
  • Loading branch information
kagemomiji committed Dec 10, 2023
1 parent cb4308f commit dbcfbe7
Showing 1 changed file with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,17 @@ public List<PodcastEpisode> getEpisodes(Integer channelId) {
return podcastEpisodeRepository.findByChannel(channel).stream()
.filter(filterAllowed)
.map(ep -> {
ep.getMediaFile();
MediaFile mediaFile = ep.getMediaFile();
if (mediaFile != null) {
// Refresh media file to check if it still exists
mediaFileService.refreshMediaFile(mediaFile);
if (!mediaFile.isPresent() && ep.getStatus() != PodcastStatus.DELETED) {
// If media file is not present anymore, set episode status to deleted
ep.setStatus(PodcastStatus.DELETED);
ep.setErrorMessage(null);
podcastEpisodeRepository.save(ep);
}
}
return ep;
})
.sorted(Comparator.comparing(PodcastEpisode::getPublishDate, Comparator.nullsLast(Comparator.reverseOrder())))
Expand Down Expand Up @@ -400,8 +410,22 @@ public List<PodcastEpisode> getNewestEpisodes(int count) {
*/
public PodcastEpisode getEpisode(int episodeId, boolean includeDeleted) {
return podcastEpisodeRepository.findById(episodeId)
.filter(episode -> includeDeleted || episode.getStatus() != PodcastStatus.DELETED)
.orElse(null);
.map(ep -> {
MediaFile mediaFile = ep.getMediaFile();
if (mediaFile != null) {
// Refresh media file to check if it still exists
mediaFileService.refreshMediaFile(mediaFile);
if (!mediaFile.isPresent() && ep.getStatus() != PodcastStatus.DELETED) {
// If media file is not present anymore, set episode status to deleted
ep.setStatus(PodcastStatus.DELETED);
ep.setErrorMessage(null);
podcastEpisodeRepository.save(ep);
}
}
return ep;
})
.filter(episode -> includeDeleted || episode.getStatus() != PodcastStatus.DELETED)
.orElse(null);
}

/**
Expand Down

0 comments on commit dbcfbe7

Please sign in to comment.