mirror of https://github.com/ErsatzTV/ErsatzTV.git
7 changed files with 139 additions and 7 deletions
@ -0,0 +1,3 @@ |
|||||||
|
namespace ErsatzTV.Application.Libraries; |
||||||
|
|
||||||
|
public record QueueLibraryScanByLibraryId(int LibraryId) : IRequest<bool>; |
||||||
@ -0,0 +1,73 @@ |
|||||||
|
using System.Threading.Channels; |
||||||
|
using ErsatzTV.Application.Emby; |
||||||
|
using ErsatzTV.Application.Jellyfin; |
||||||
|
using ErsatzTV.Application.MediaSources; |
||||||
|
using ErsatzTV.Application.Plex; |
||||||
|
using ErsatzTV.Core.Domain; |
||||||
|
using ErsatzTV.Core.Interfaces.Locking; |
||||||
|
using ErsatzTV.Infrastructure.Data; |
||||||
|
using ErsatzTV.Infrastructure.Extensions; |
||||||
|
using Microsoft.EntityFrameworkCore; |
||||||
|
using Microsoft.Extensions.Logging; |
||||||
|
|
||||||
|
namespace ErsatzTV.Application.Libraries; |
||||||
|
|
||||||
|
public class QueueLibraryScanByLibraryIdHandler( |
||||||
|
IDbContextFactory<TvContext> dbContextFactory, |
||||||
|
IEntityLocker locker, |
||||||
|
ChannelWriter<IScannerBackgroundServiceRequest> scannerWorker, |
||||||
|
ILogger<QueueLibraryScanByLibraryIdHandler> logger) |
||||||
|
: IRequestHandler<QueueLibraryScanByLibraryId, bool> |
||||||
|
{ |
||||||
|
public async Task<bool> Handle(QueueLibraryScanByLibraryId request, CancellationToken cancellationToken) |
||||||
|
{ |
||||||
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken); |
||||||
|
|
||||||
|
Option<Library> maybeLibrary = await dbContext.Libraries |
||||||
|
.AsNoTracking() |
||||||
|
.SelectOneAsync(l => l.Id, l => l.Id == request.LibraryId); |
||||||
|
|
||||||
|
foreach (Library library in maybeLibrary) |
||||||
|
{ |
||||||
|
if (locker.LockLibrary(library.Id)) |
||||||
|
{ |
||||||
|
logger.LogDebug("Queued library scan for library id {Id}", library.Id); |
||||||
|
|
||||||
|
switch (library) |
||||||
|
{ |
||||||
|
case LocalLibrary: |
||||||
|
await scannerWorker.WriteAsync(new ForceScanLocalLibrary(library.Id), cancellationToken); |
||||||
|
break; |
||||||
|
case PlexLibrary: |
||||||
|
await scannerWorker.WriteAsync( |
||||||
|
new SynchronizePlexLibraries(library.MediaSourceId), |
||||||
|
cancellationToken); |
||||||
|
await scannerWorker.WriteAsync( |
||||||
|
new ForceSynchronizePlexLibraryById(library.Id, false), |
||||||
|
cancellationToken); |
||||||
|
break; |
||||||
|
case JellyfinLibrary: |
||||||
|
await scannerWorker.WriteAsync( |
||||||
|
new SynchronizeJellyfinLibraries(library.MediaSourceId), |
||||||
|
cancellationToken); |
||||||
|
await scannerWorker.WriteAsync( |
||||||
|
new ForceSynchronizeJellyfinLibraryById(library.Id, false), |
||||||
|
cancellationToken); |
||||||
|
break; |
||||||
|
case EmbyLibrary: |
||||||
|
await scannerWorker.WriteAsync( |
||||||
|
new SynchronizeEmbyLibraries(library.MediaSourceId), |
||||||
|
cancellationToken); |
||||||
|
await scannerWorker.WriteAsync( |
||||||
|
new ForceSynchronizeEmbyLibraryById(library.Id, false), |
||||||
|
cancellationToken); |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,3 @@ |
|||||||
|
namespace ErsatzTV.Application.Playouts; |
||||||
|
|
||||||
|
public record GetPlayoutIdByChannelNumber(string ChannelNumber) : IRequest<Option<int>>; |
||||||
@ -0,0 +1,18 @@ |
|||||||
|
using ErsatzTV.Infrastructure.Data; |
||||||
|
using Microsoft.EntityFrameworkCore; |
||||||
|
|
||||||
|
namespace ErsatzTV.Application.Playouts; |
||||||
|
|
||||||
|
public class GetPlayoutIdByChannelNumberHandler(IDbContextFactory<TvContext> dbContextFactory) |
||||||
|
: IRequestHandler<GetPlayoutIdByChannelNumber, Option<int>> |
||||||
|
{ |
||||||
|
public async Task<Option<int>> Handle(GetPlayoutIdByChannelNumber request, CancellationToken cancellationToken) |
||||||
|
{ |
||||||
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken); |
||||||
|
return await dbContext.Playouts |
||||||
|
.Filter(p => p.Channel.Number == request.ChannelNumber) |
||||||
|
.Map(p => p.Id) |
||||||
|
.ToListAsync(cancellationToken) |
||||||
|
.Map(list => list.HeadOrNone()); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,17 @@ |
|||||||
|
using ErsatzTV.Application.Libraries; |
||||||
|
using MediatR; |
||||||
|
using Microsoft.AspNetCore.Mvc; |
||||||
|
|
||||||
|
namespace ErsatzTV.Controllers.Api; |
||||||
|
|
||||||
|
[ApiController] |
||||||
|
public class LibrariesController(IMediator mediator) |
||||||
|
{ |
||||||
|
[HttpPost("/api/libraries/{id:int}/scan")] |
||||||
|
public async Task<IActionResult> ResetPlayout(int id) |
||||||
|
{ |
||||||
|
return await mediator.Send(new QueueLibraryScanByLibraryId(id)) |
||||||
|
? new OkResult() |
||||||
|
: new NotFoundResult(); |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue