* fix plex library synchronization * add basic plex movie synchronization * proxy plex movie thumbnails (posters) * add plex path replacements * use transcoded plex artwork * remove unsynchronized plex movies on save; queue plex library scan on save * log plex path replacements * prefer buttons instead of menus * lock plex libraries before sync * add movie to collection from paged view * fix plex import memory use; quick add seasons/shows * quick add episode to collection * add favicon * add search page * disable plex for now
66 lines
2.8 KiB
C#
66 lines
2.8 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using ErsatzTV.Core;
|
|
using ErsatzTV.Core.Domain;
|
|
using ErsatzTV.Core.Interfaces.Repositories;
|
|
using LanguageExt;
|
|
using static LanguageExt.Prelude;
|
|
|
|
namespace ErsatzTV.Application.Plex.Commands
|
|
{
|
|
public class
|
|
UpdatePlexPathReplacementsHandler : MediatR.IRequestHandler<UpdatePlexPathReplacements, Either<BaseError, Unit>>
|
|
{
|
|
private readonly IMediaSourceRepository _mediaSourceRepository;
|
|
|
|
public UpdatePlexPathReplacementsHandler(IMediaSourceRepository mediaSourceRepository) =>
|
|
_mediaSourceRepository = mediaSourceRepository;
|
|
|
|
public Task<Either<BaseError, Unit>> Handle(
|
|
UpdatePlexPathReplacements request,
|
|
CancellationToken cancellationToken) =>
|
|
Validate(request)
|
|
.MapT(pms => MergePathReplacements(request, pms))
|
|
.Bind(v => v.ToEitherAsync());
|
|
|
|
private Task<Unit> MergePathReplacements(UpdatePlexPathReplacements request, PlexMediaSource plexMediaSource)
|
|
{
|
|
plexMediaSource.PathReplacements ??= new List<PlexPathReplacement>();
|
|
|
|
var incoming = request.PathReplacements.Map(Project).ToList();
|
|
|
|
var toAdd = incoming.Filter(r => r.Id < 1).ToList();
|
|
var toRemove = plexMediaSource.PathReplacements.Filter(r => incoming.All(pr => pr.Id != r.Id)).ToList();
|
|
var toUpdate = incoming.Except(toAdd).ToList();
|
|
|
|
plexMediaSource.PathReplacements.AddRange(toAdd);
|
|
toRemove.ForEach(pr => plexMediaSource.PathReplacements.Remove(pr));
|
|
foreach (PlexPathReplacement pathReplacement in toUpdate)
|
|
{
|
|
Optional(plexMediaSource.PathReplacements.SingleOrDefault(pr => pr.Id == pathReplacement.Id))
|
|
.IfSome(
|
|
pr =>
|
|
{
|
|
pr.PlexPath = pathReplacement.PlexPath;
|
|
pr.LocalPath = pathReplacement.LocalPath;
|
|
});
|
|
}
|
|
|
|
return _mediaSourceRepository.Update(plexMediaSource).ToUnit();
|
|
}
|
|
|
|
private static PlexPathReplacement Project(PlexPathReplacementItem vm) =>
|
|
new() { Id = vm.Id, PlexPath = vm.PlexPath, LocalPath = vm.LocalPath };
|
|
|
|
private Task<Validation<BaseError, PlexMediaSource>> Validate(UpdatePlexPathReplacements request) =>
|
|
PlexMediaSourceMustExist(request);
|
|
|
|
private Task<Validation<BaseError, PlexMediaSource>> PlexMediaSourceMustExist(
|
|
UpdatePlexPathReplacements request) =>
|
|
_mediaSourceRepository.GetPlex(request.PlexMediaSourceId)
|
|
.Map(v => v.ToValidation<BaseError>($"Plex media source {request.PlexMediaSourceId} does not exist."));
|
|
}
|
|
}
|