Stream custom live channels using your own media
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
2.0 KiB

using ErsatzTV.Core.Domain;
using ErsatzTV.Infrastructure.Data;
using ErsatzTV.Infrastructure.Extensions;
using Microsoft.EntityFrameworkCore;
namespace ErsatzTV.Application.Troubleshooting.Queries;
public class GetTroubleshootingSubtitlesHandler(IDbContextFactory<TvContext> dbContextFactory)
: IRequestHandler<GetTroubleshootingSubtitles, List<SubtitleViewModel>>
{
public async Task<List<SubtitleViewModel>> Handle(
GetTroubleshootingSubtitles request,
CancellationToken cancellationToken)
{
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
Option<MediaItem> maybeMediaItem = await dbContext.MediaItems
.AsNoTracking()
.Include(mi => (mi as Movie).MovieMetadata)
.ThenInclude(mm => mm.Subtitles)
.Include(mi => (mi as Episode).EpisodeMetadata)
.ThenInclude(mm => mm.Subtitles)
.Include(mi => (mi as OtherVideo).OtherVideoMetadata)
.ThenInclude(mm => mm.Subtitles)
.SelectOneAsync(mi => mi.Id, mi => mi.Id == request.MediaItemId);
foreach (MediaItem mediaItem in maybeMediaItem)
{
List<Subtitle> subtitles = GetSubtitles(mediaItem);
// remove text subtitles that are embedded but have not been extracted
subtitles.RemoveAll(s => s.SubtitleKind is SubtitleKind.Embedded && !s.IsImage && !s.IsExtracted);
return subtitles.Map(ProjectToViewModel).ToList();
}
return [];
}
private static List<Subtitle> GetSubtitles(MediaItem mediaItem) =>
mediaItem switch
{
Episode e => e.EpisodeMetadata.Head().Subtitles,
Movie m => m.MovieMetadata.Head().Subtitles,
OtherVideo ov => ov.OtherVideoMetadata.Head().Subtitles,
_ => []
};
private static SubtitleViewModel ProjectToViewModel(Subtitle subtitle) =>
new(subtitle.Id, subtitle.Language, subtitle.Title, subtitle.Codec);
}