mirror of https://github.com/ErsatzTV/ErsatzTV.git
49 changed files with 8483 additions and 29 deletions
@ -0,0 +1,11 @@
@@ -0,0 +1,11 @@
|
||||
using System.Collections.Generic; |
||||
using ErsatzTV.Core.Search; |
||||
using LanguageExt; |
||||
|
||||
namespace ErsatzTV.Application.MediaCards |
||||
{ |
||||
public record OtherVideoCardResultsViewModel( |
||||
int Count, |
||||
List<OtherVideoCardViewModel> Cards, |
||||
Option<SearchPageMap> PageMap); |
||||
} |
@ -0,0 +1,17 @@
@@ -0,0 +1,17 @@
|
||||
namespace ErsatzTV.Application.MediaCards |
||||
{ |
||||
public record OtherVideoCardViewModel |
||||
( |
||||
int OtherVideoId, |
||||
string Title, |
||||
string Subtitle, |
||||
string SortTitle) : MediaCardViewModel( |
||||
OtherVideoId, |
||||
Title, |
||||
Subtitle, |
||||
SortTitle, |
||||
null) |
||||
{ |
||||
public int CustomIndex { get; set; } |
||||
} |
||||
} |
@ -0,0 +1,8 @@
@@ -0,0 +1,8 @@
|
||||
using ErsatzTV.Core; |
||||
using LanguageExt; |
||||
|
||||
namespace ErsatzTV.Application.MediaCollections.Commands |
||||
{ |
||||
public record AddOtherVideoToCollection |
||||
(int CollectionId, int OtherVideoId) : MediatR.IRequest<Either<BaseError, Unit>>; |
||||
} |
@ -0,0 +1,80 @@
@@ -0,0 +1,80 @@
|
||||
using System.Threading; |
||||
using System.Threading.Channels; |
||||
using System.Threading.Tasks; |
||||
using ErsatzTV.Application.Playouts.Commands; |
||||
using ErsatzTV.Core; |
||||
using ErsatzTV.Core.Domain; |
||||
using ErsatzTV.Core.Interfaces.Repositories; |
||||
using ErsatzTV.Infrastructure.Data; |
||||
using ErsatzTV.Infrastructure.Extensions; |
||||
using LanguageExt; |
||||
using Microsoft.EntityFrameworkCore; |
||||
|
||||
namespace ErsatzTV.Application.MediaCollections.Commands |
||||
{ |
||||
public class AddOtherVideoToCollectionHandler : |
||||
MediatR.IRequestHandler<AddOtherVideoToCollection, Either<BaseError, Unit>> |
||||
{ |
||||
private readonly ChannelWriter<IBackgroundServiceRequest> _channel; |
||||
private readonly IDbContextFactory<TvContext> _dbContextFactory; |
||||
private readonly IMediaCollectionRepository _mediaCollectionRepository; |
||||
|
||||
public AddOtherVideoToCollectionHandler( |
||||
IDbContextFactory<TvContext> dbContextFactory, |
||||
IMediaCollectionRepository mediaCollectionRepository, |
||||
ChannelWriter<IBackgroundServiceRequest> channel) |
||||
{ |
||||
_dbContextFactory = dbContextFactory; |
||||
_mediaCollectionRepository = mediaCollectionRepository; |
||||
_channel = channel; |
||||
} |
||||
|
||||
public async Task<Either<BaseError, Unit>> Handle( |
||||
AddOtherVideoToCollection request, |
||||
CancellationToken cancellationToken) |
||||
{ |
||||
await using TvContext dbContext = _dbContextFactory.CreateDbContext(); |
||||
Validation<BaseError, Parameters> validation = await Validate(dbContext, request); |
||||
return await validation.Apply(parameters => ApplyAddOtherVideoRequest(dbContext, parameters)); |
||||
} |
||||
|
||||
private async Task<Unit> ApplyAddOtherVideoRequest(TvContext dbContext, Parameters parameters) |
||||
{ |
||||
parameters.Collection.MediaItems.Add(parameters.OtherVideo); |
||||
if (await dbContext.SaveChangesAsync() > 0) |
||||
{ |
||||
// rebuild all playouts that use this collection
|
||||
foreach (int playoutId in await _mediaCollectionRepository |
||||
.PlayoutIdsUsingCollection(parameters.Collection.Id)) |
||||
{ |
||||
await _channel.WriteAsync(new BuildPlayout(playoutId, true)); |
||||
} |
||||
} |
||||
|
||||
return Unit.Default; |
||||
} |
||||
|
||||
private static async Task<Validation<BaseError, Parameters>> Validate( |
||||
TvContext dbContext, |
||||
AddOtherVideoToCollection request) => |
||||
(await CollectionMustExist(dbContext, request), await ValidateOtherVideo(dbContext, request)) |
||||
.Apply((collection, episode) => new Parameters(collection, episode)); |
||||
|
||||
private static Task<Validation<BaseError, Collection>> CollectionMustExist( |
||||
TvContext dbContext, |
||||
AddOtherVideoToCollection request) => |
||||
dbContext.Collections |
||||
.Include(c => c.MediaItems) |
||||
.SelectOneAsync(c => c.Id, c => c.Id == request.CollectionId) |
||||
.Map(o => o.ToValidation<BaseError>("Collection does not exist.")); |
||||
|
||||
private static Task<Validation<BaseError, OtherVideo>> ValidateOtherVideo( |
||||
TvContext dbContext, |
||||
AddOtherVideoToCollection request) => |
||||
dbContext.OtherVideos |
||||
.SelectOneAsync(m => m.Id, e => e.Id == request.OtherVideoId) |
||||
.Map(o => o.ToValidation<BaseError>("OtherVideo does not exist")); |
||||
|
||||
private record Parameters(Collection Collection, OtherVideo OtherVideo); |
||||
} |
||||
} |
@ -0,0 +1,8 @@
@@ -0,0 +1,8 @@
|
||||
using ErsatzTV.Application.MediaCards; |
||||
using MediatR; |
||||
|
||||
namespace ErsatzTV.Application.Search.Queries |
||||
{ |
||||
public record QuerySearchIndexOtherVideos |
||||
(string Query, int PageNumber, int PageSize) : IRequest<OtherVideoCardResultsViewModel>; |
||||
} |
@ -0,0 +1,44 @@
@@ -0,0 +1,44 @@
|
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Threading; |
||||
using System.Threading.Tasks; |
||||
using ErsatzTV.Application.MediaCards; |
||||
using ErsatzTV.Core.Interfaces.Repositories; |
||||
using ErsatzTV.Core.Interfaces.Search; |
||||
using ErsatzTV.Core.Search; |
||||
using LanguageExt; |
||||
using MediatR; |
||||
using static ErsatzTV.Application.MediaCards.Mapper; |
||||
|
||||
namespace ErsatzTV.Application.Search.Queries |
||||
{ |
||||
public class |
||||
QuerySearchIndexOtherVideosHandler : IRequestHandler<QuerySearchIndexOtherVideos, |
||||
OtherVideoCardResultsViewModel> |
||||
{ |
||||
private readonly IOtherVideoRepository _otherVideoRepository; |
||||
private readonly ISearchIndex _searchIndex; |
||||
|
||||
public QuerySearchIndexOtherVideosHandler(ISearchIndex searchIndex, IOtherVideoRepository otherVideoRepository) |
||||
{ |
||||
_searchIndex = searchIndex; |
||||
_otherVideoRepository = otherVideoRepository; |
||||
} |
||||
|
||||
public async Task<OtherVideoCardResultsViewModel> Handle( |
||||
QuerySearchIndexOtherVideos request, |
||||
CancellationToken cancellationToken) |
||||
{ |
||||
SearchResult searchResult = await _searchIndex.Search( |
||||
request.Query, |
||||
(request.PageNumber - 1) * request.PageSize, |
||||
request.PageSize); |
||||
|
||||
List<OtherVideoCardViewModel> items = await _otherVideoRepository |
||||
.GetOtherVideosForCards(searchResult.Items.Map(i => i.Id).ToList()) |
||||
.Map(list => list.Map(ProjectToViewModel).ToList()); |
||||
|
||||
return new OtherVideoCardResultsViewModel(searchResult.TotalCount, items, searchResult.PageMap); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,10 @@
@@ -0,0 +1,10 @@
|
||||
using System.Collections.Generic; |
||||
|
||||
namespace ErsatzTV.Core.Domain |
||||
{ |
||||
public class OtherVideo : MediaItem |
||||
{ |
||||
public List<OtherVideoMetadata> OtherVideoMetadata { get; set; } |
||||
public List<MediaVersion> MediaVersions { get; set; } |
||||
} |
||||
} |
@ -0,0 +1,8 @@
@@ -0,0 +1,8 @@
|
||||
namespace ErsatzTV.Core.Domain |
||||
{ |
||||
public class OtherVideoMetadata : Metadata |
||||
{ |
||||
public int OtherVideoId { get; set; } |
||||
public OtherVideo OtherVideo { get; set; } |
||||
} |
||||
} |
@ -0,0 +1,15 @@
@@ -0,0 +1,15 @@
|
||||
using System.Threading.Tasks; |
||||
using ErsatzTV.Core.Domain; |
||||
using LanguageExt; |
||||
|
||||
namespace ErsatzTV.Core.Interfaces.Metadata |
||||
{ |
||||
public interface IOtherVideoFolderScanner |
||||
{ |
||||
Task<Either<BaseError, Unit>> ScanFolder( |
||||
LibraryPath libraryPath, |
||||
string ffprobePath, |
||||
decimal progressMin, |
||||
decimal progressMax); |
||||
} |
||||
} |
@ -0,0 +1,19 @@
@@ -0,0 +1,19 @@
|
||||
using System.Collections.Generic; |
||||
using System.Threading.Tasks; |
||||
using ErsatzTV.Core.Domain; |
||||
using ErsatzTV.Core.Metadata; |
||||
using LanguageExt; |
||||
|
||||
namespace ErsatzTV.Core.Interfaces.Repositories |
||||
{ |
||||
public interface IOtherVideoRepository |
||||
{ |
||||
Task<Either<BaseError, MediaItemScanResult<OtherVideo>>> GetOrAdd(LibraryPath libraryPath, string path); |
||||
Task<IEnumerable<string>> FindOtherVideoPaths(LibraryPath libraryPath); |
||||
Task<List<int>> DeleteByPath(LibraryPath libraryPath, string path); |
||||
Task<bool> AddTag(OtherVideoMetadata metadata, Tag tag); |
||||
Task<List<OtherVideoMetadata>> GetOtherVideosForCards(List<int> ids); |
||||
// Task<int> GetOtherVideoCount(int artistId);
|
||||
// Task<List<OtherVideoMetadata>> GetPagedOtherVideos(int artistId, int pageNumber, int pageSize);
|
||||
} |
||||
} |
@ -0,0 +1,197 @@
@@ -0,0 +1,197 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.IO; |
||||
using System.Linq; |
||||
using System.Threading.Tasks; |
||||
using ErsatzTV.Core.Domain; |
||||
using ErsatzTV.Core.Errors; |
||||
using ErsatzTV.Core.Interfaces.Images; |
||||
using ErsatzTV.Core.Interfaces.Metadata; |
||||
using ErsatzTV.Core.Interfaces.Repositories; |
||||
using ErsatzTV.Core.Interfaces.Search; |
||||
using LanguageExt; |
||||
using MediatR; |
||||
using Microsoft.Extensions.Logging; |
||||
using static LanguageExt.Prelude; |
||||
using Unit = LanguageExt.Unit; |
||||
|
||||
namespace ErsatzTV.Core.Metadata |
||||
{ |
||||
public class OtherVideoFolderScanner : LocalFolderScanner, IOtherVideoFolderScanner |
||||
{ |
||||
private readonly ILocalFileSystem _localFileSystem; |
||||
private readonly ILocalMetadataProvider _localMetadataProvider; |
||||
private readonly IMediator _mediator; |
||||
private readonly ISearchIndex _searchIndex; |
||||
private readonly ISearchRepository _searchRepository; |
||||
private readonly IOtherVideoRepository _otherVideoRepository; |
||||
private readonly ILibraryRepository _libraryRepository; |
||||
private readonly ILogger<OtherVideoFolderScanner> _logger; |
||||
|
||||
public OtherVideoFolderScanner( |
||||
ILocalFileSystem localFileSystem, |
||||
ILocalStatisticsProvider localStatisticsProvider, |
||||
ILocalMetadataProvider localMetadataProvider, |
||||
IMetadataRepository metadataRepository, |
||||
IImageCache imageCache, |
||||
IMediator mediator, |
||||
ISearchIndex searchIndex, |
||||
ISearchRepository searchRepository, |
||||
IOtherVideoRepository otherVideoRepository, |
||||
ILibraryRepository libraryRepository, |
||||
ILogger<OtherVideoFolderScanner> logger) : base( |
||||
localFileSystem, |
||||
localStatisticsProvider, |
||||
metadataRepository, |
||||
imageCache, |
||||
logger) |
||||
{ |
||||
_localFileSystem = localFileSystem; |
||||
_localMetadataProvider = localMetadataProvider; |
||||
_mediator = mediator; |
||||
_searchIndex = searchIndex; |
||||
_searchRepository = searchRepository; |
||||
_otherVideoRepository = otherVideoRepository; |
||||
_libraryRepository = libraryRepository; |
||||
_logger = logger; |
||||
} |
||||
|
||||
public async Task<Either<BaseError, Unit>> ScanFolder( |
||||
LibraryPath libraryPath, |
||||
string ffprobePath, |
||||
decimal progressMin, |
||||
decimal progressMax) |
||||
{ |
||||
decimal progressSpread = progressMax - progressMin; |
||||
|
||||
if (!_localFileSystem.IsLibraryPathAccessible(libraryPath)) |
||||
{ |
||||
return new MediaSourceInaccessible(); |
||||
} |
||||
|
||||
var foldersCompleted = 0; |
||||
|
||||
var folderQueue = new Queue<string>(); |
||||
foreach (string folder in _localFileSystem.ListSubdirectories(libraryPath.Path) |
||||
.Filter(ShouldIncludeFolder) |
||||
.OrderBy(identity)) |
||||
{ |
||||
folderQueue.Enqueue(folder); |
||||
} |
||||
|
||||
while (folderQueue.Count > 0) |
||||
{ |
||||
decimal percentCompletion = (decimal)foldersCompleted / (foldersCompleted + folderQueue.Count); |
||||
await _mediator.Publish( |
||||
new LibraryScanProgress(libraryPath.LibraryId, progressMin + percentCompletion * progressSpread)); |
||||
|
||||
string otherVideoFolder = folderQueue.Dequeue(); |
||||
foldersCompleted++; |
||||
|
||||
var filesForEtag = _localFileSystem.ListFiles(otherVideoFolder).ToList(); |
||||
|
||||
var allFiles = filesForEtag |
||||
.Filter(f => VideoFileExtensions.Contains(Path.GetExtension(f))) |
||||
.Filter(f => !Path.GetFileName(f).StartsWith("._")) |
||||
.ToList(); |
||||
|
||||
foreach (string subdirectory in _localFileSystem.ListSubdirectories(otherVideoFolder) |
||||
.Filter(ShouldIncludeFolder) |
||||
.OrderBy(identity)) |
||||
{ |
||||
folderQueue.Enqueue(subdirectory); |
||||
} |
||||
|
||||
string etag = FolderEtag.Calculate(otherVideoFolder, _localFileSystem); |
||||
Option<LibraryFolder> knownFolder = libraryPath.LibraryFolders |
||||
.Filter(f => f.Path == otherVideoFolder) |
||||
.HeadOrNone(); |
||||
|
||||
// skip folder if etag matches
|
||||
if (!allFiles.Any() || await knownFolder.Map(f => f.Etag ?? string.Empty).IfNoneAsync(string.Empty) == etag) |
||||
{ |
||||
continue; |
||||
} |
||||
|
||||
_logger.LogDebug( |
||||
"UPDATE: Etag has changed for folder {Folder}", |
||||
otherVideoFolder); |
||||
|
||||
foreach (string file in allFiles.OrderBy(identity)) |
||||
{ |
||||
_logger.LogDebug("Other video found at {File}", file); |
||||
|
||||
Either<BaseError, MediaItemScanResult<OtherVideo>> maybeVideo = await _otherVideoRepository |
||||
.GetOrAdd(libraryPath, file) |
||||
.BindT(video => UpdateStatistics(video, ffprobePath)) |
||||
.BindT(UpdateMetadata); |
||||
|
||||
await maybeVideo.Match( |
||||
async result => |
||||
{ |
||||
if (result.IsAdded) |
||||
{ |
||||
await _searchIndex.AddItems(_searchRepository, new List<MediaItem> { result.Item }); |
||||
} |
||||
else if (result.IsUpdated) |
||||
{ |
||||
await _searchIndex.UpdateItems(_searchRepository, new List<MediaItem> { result.Item }); |
||||
} |
||||
|
||||
await _libraryRepository.SetEtag(libraryPath, knownFolder, otherVideoFolder, etag); |
||||
}, |
||||
error => |
||||
{ |
||||
_logger.LogWarning("Error processing other video at {Path}: {Error}", file, error.Value); |
||||
return Task.CompletedTask; |
||||
}); |
||||
} |
||||
} |
||||
|
||||
foreach (string path in await _otherVideoRepository.FindOtherVideoPaths(libraryPath)) |
||||
{ |
||||
if (!_localFileSystem.FileExists(path)) |
||||
{ |
||||
_logger.LogInformation("Removing missing other video at {Path}", path); |
||||
List<int> otherVideoIds = await _otherVideoRepository.DeleteByPath(libraryPath, path); |
||||
await _searchIndex.RemoveItems(otherVideoIds); |
||||
} |
||||
else if (Path.GetFileName(path).StartsWith("._")) |
||||
{ |
||||
_logger.LogInformation("Removing dot underscore file at {Path}", path); |
||||
List<int> otherVideoIds = await _otherVideoRepository.DeleteByPath(libraryPath, path); |
||||
await _searchIndex.RemoveItems(otherVideoIds); |
||||
} |
||||
} |
||||
|
||||
_searchIndex.Commit(); |
||||
return Unit.Default; |
||||
} |
||||
|
||||
private async Task<Either<BaseError, MediaItemScanResult<OtherVideo>>> UpdateMetadata( |
||||
MediaItemScanResult<OtherVideo> result) |
||||
{ |
||||
try |
||||
{ |
||||
OtherVideo otherVideo = result.Item; |
||||
if (!Optional(otherVideo.OtherVideoMetadata).Flatten().Any()) |
||||
{ |
||||
otherVideo.OtherVideoMetadata ??= new List<OtherVideoMetadata>(); |
||||
|
||||
string path = otherVideo.MediaVersions.Head().MediaFiles.Head().Path; |
||||
_logger.LogDebug("Refreshing {Attribute} for {Path}", "Fallback Metadata", path); |
||||
if (await _localMetadataProvider.RefreshFallbackMetadata(otherVideo)) |
||||
{ |
||||
result.IsUpdated = true; |
||||
} |
||||
} |
||||
|
||||
return result; |
||||
} |
||||
catch (Exception ex) |
||||
{ |
||||
return BaseError.New(ex.ToString()); |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,23 @@
@@ -0,0 +1,23 @@
|
||||
using ErsatzTV.Core.Domain; |
||||
using Microsoft.EntityFrameworkCore; |
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders; |
||||
|
||||
namespace ErsatzTV.Infrastructure.Data.Configurations |
||||
{ |
||||
public class OtherVideoConfiguration : IEntityTypeConfiguration<OtherVideo> |
||||
{ |
||||
public void Configure(EntityTypeBuilder<OtherVideo> builder) |
||||
{ |
||||
builder.ToTable("OtherVideo"); |
||||
|
||||
builder.HasMany(m => m.OtherVideoMetadata) |
||||
.WithOne(m => m.OtherVideo) |
||||
.HasForeignKey(m => m.OtherVideoId) |
||||
.OnDelete(DeleteBehavior.Cascade); |
||||
|
||||
builder.HasMany(m => m.MediaVersions) |
||||
.WithOne() |
||||
.OnDelete(DeleteBehavior.Cascade); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,22 @@
@@ -0,0 +1,22 @@
|
||||
using ErsatzTV.Core.Domain; |
||||
using Microsoft.EntityFrameworkCore; |
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders; |
||||
|
||||
namespace ErsatzTV.Infrastructure.Data.Configurations |
||||
{ |
||||
public class OtherVideoMetadataConfiguration : IEntityTypeConfiguration<OtherVideoMetadata> |
||||
{ |
||||
public void Configure(EntityTypeBuilder<OtherVideoMetadata> builder) |
||||
{ |
||||
builder.ToTable("OtherVideoMetadata"); |
||||
|
||||
builder.HasMany(mm => mm.Artwork) |
||||
.WithOne() |
||||
.OnDelete(DeleteBehavior.Cascade); |
||||
|
||||
builder.HasMany(mm => mm.Tags) |
||||
.WithOne() |
||||
.OnDelete(DeleteBehavior.Cascade); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,143 @@
@@ -0,0 +1,143 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Data; |
||||
using System.Linq; |
||||
using System.Threading.Tasks; |
||||
using Dapper; |
||||
using ErsatzTV.Core; |
||||
using ErsatzTV.Core.Domain; |
||||
using ErsatzTV.Core.Interfaces.Repositories; |
||||
using ErsatzTV.Core.Metadata; |
||||
using LanguageExt; |
||||
using Microsoft.EntityFrameworkCore; |
||||
using static LanguageExt.Prelude; |
||||
|
||||
namespace ErsatzTV.Infrastructure.Data.Repositories |
||||
{ |
||||
public class OtherVideoRepository : IOtherVideoRepository |
||||
{ |
||||
private readonly IDbConnection _dbConnection; |
||||
private readonly IDbContextFactory<TvContext> _dbContextFactory; |
||||
|
||||
public OtherVideoRepository(IDbConnection dbConnection, IDbContextFactory<TvContext> dbContextFactory) |
||||
{ |
||||
_dbConnection = dbConnection; |
||||
_dbContextFactory = dbContextFactory; |
||||
} |
||||
|
||||
public async Task<Either<BaseError, MediaItemScanResult<OtherVideo>>> GetOrAdd( |
||||
LibraryPath libraryPath, |
||||
string path) |
||||
{ |
||||
await using TvContext dbContext = _dbContextFactory.CreateDbContext(); |
||||
Option<OtherVideo> maybeExisting = await dbContext.OtherVideos |
||||
.AsNoTracking() |
||||
.Include(ov => ov.OtherVideoMetadata) |
||||
.ThenInclude(ovm => ovm.Artwork) |
||||
.Include(ov => ov.OtherVideoMetadata) |
||||
.ThenInclude(ovm => ovm.Tags) |
||||
.Include(ov => ov.LibraryPath) |
||||
.ThenInclude(lp => lp.Library) |
||||
.Include(ov => ov.MediaVersions) |
||||
.ThenInclude(ov => ov.MediaFiles) |
||||
.Include(ov => ov.MediaVersions) |
||||
.ThenInclude(ov => ov.Streams) |
||||
.Include(ov => ov.TraktListItems) |
||||
.ThenInclude(tli => tli.TraktList) |
||||
.OrderBy(i => i.MediaVersions.First().MediaFiles.First().Path) |
||||
.SingleOrDefaultAsync(i => i.MediaVersions.First().MediaFiles.First().Path == path); |
||||
|
||||
return await maybeExisting.Match( |
||||
mediaItem => |
||||
Right<BaseError, MediaItemScanResult<OtherVideo>>( |
||||
new MediaItemScanResult<OtherVideo>(mediaItem) { IsAdded = false }).AsTask(), |
||||
async () => await AddOtherVideo(dbContext, libraryPath.Id, path)); |
||||
} |
||||
|
||||
public Task<IEnumerable<string>> FindOtherVideoPaths(LibraryPath libraryPath) => |
||||
_dbConnection.QueryAsync<string>( |
||||
@"SELECT MF.Path
|
||||
FROM MediaFile MF |
||||
INNER JOIN MediaVersion MV on MF.MediaVersionId = MV.Id |
||||
INNER JOIN OtherVideo O on MV.OtherVideoId = O.Id |
||||
INNER JOIN MediaItem MI on O.Id = MI.Id |
||||
WHERE MI.LibraryPathId = @LibraryPathId",
|
||||
new { LibraryPathId = libraryPath.Id }); |
||||
|
||||
public async Task<List<int>> DeleteByPath(LibraryPath libraryPath, string path) |
||||
{ |
||||
List<int> ids = await _dbConnection.QueryAsync<int>( |
||||
@"SELECT O.Id
|
||||
FROM OtherVideo O |
||||
INNER JOIN MediaItem MI on O.Id = MI.Id |
||||
INNER JOIN MediaVersion MV on O.Id = MV.OtherVideoId |
||||
INNER JOIN MediaFile MF on MV.Id = MF.MediaVersionId |
||||
WHERE MI.LibraryPathId = @LibraryPathId AND MF.Path = @Path",
|
||||
new { LibraryPathId = libraryPath.Id, Path = path }).Map(result => result.ToList()); |
||||
|
||||
await using TvContext dbContext = _dbContextFactory.CreateDbContext(); |
||||
foreach (int otherVideoId in ids) |
||||
{ |
||||
OtherVideo othervide = await dbContext.OtherVideos.FindAsync(otherVideoId); |
||||
dbContext.OtherVideos.Remove(othervide); |
||||
} |
||||
|
||||
await dbContext.SaveChangesAsync(); |
||||
|
||||
return ids; |
||||
} |
||||
|
||||
public Task<bool> AddTag(OtherVideoMetadata metadata, Tag tag) => |
||||
_dbConnection.ExecuteAsync( |
||||
"INSERT INTO Tag (Name, OtherVideoMetadataId) VALUES (@Name, @MetadataId)", |
||||
new { tag.Name, MetadataId = metadata.Id }).Map(result => result > 0); |
||||
|
||||
public async Task<List<OtherVideoMetadata>> GetOtherVideosForCards(List<int> ids) |
||||
{ |
||||
await using TvContext dbContext = _dbContextFactory.CreateDbContext(); |
||||
return await dbContext.OtherVideoMetadata |
||||
.AsNoTracking() |
||||
.Filter(ovm => ids.Contains(ovm.OtherVideoId)) |
||||
.Include(ovm => ovm.OtherVideo) |
||||
.Include(ovm => ovm.Artwork) |
||||
.OrderBy(ovm => ovm.SortTitle) |
||||
.ToListAsync(); |
||||
} |
||||
|
||||
private static async Task<Either<BaseError, MediaItemScanResult<OtherVideo>>> AddOtherVideo( |
||||
TvContext dbContext, |
||||
int libraryPathId, |
||||
string path) |
||||
{ |
||||
try |
||||
{ |
||||
var otherVideo = new OtherVideo |
||||
{ |
||||
LibraryPathId = libraryPathId, |
||||
MediaVersions = new List<MediaVersion> |
||||
{ |
||||
new() |
||||
{ |
||||
MediaFiles = new List<MediaFile> |
||||
{ |
||||
new() { Path = path } |
||||
}, |
||||
Streams = new List<MediaStream>() |
||||
} |
||||
}, |
||||
TraktListItems = new List<TraktListItem>() |
||||
}; |
||||
|
||||
await dbContext.OtherVideos.AddAsync(otherVideo); |
||||
await dbContext.SaveChangesAsync(); |
||||
await dbContext.Entry(otherVideo).Reference(m => m.LibraryPath).LoadAsync(); |
||||
await dbContext.Entry(otherVideo.LibraryPath).Reference(lp => lp.Library).LoadAsync(); |
||||
return new MediaItemScanResult<OtherVideo>(otherVideo) { IsAdded = true }; |
||||
} |
||||
catch (Exception ex) |
||||
{ |
||||
return BaseError.New(ex.Message); |
||||
} |
||||
} |
||||
} |
||||
} |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,24 @@
@@ -0,0 +1,24 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations; |
||||
|
||||
namespace ErsatzTV.Infrastructure.Migrations |
||||
{ |
||||
public partial class Add_LocalLibrary_OtherVideos : Migration |
||||
{ |
||||
protected override void Up(MigrationBuilder migrationBuilder) |
||||
{ |
||||
// create local other videos library
|
||||
migrationBuilder.Sql( |
||||
@"INSERT INTO Library (Name, MediaKind, MediaSourceId)
|
||||
SELECT 'Other Videos', 4, Id FROM |
||||
(SELECT LMS.Id FROM LocalMediaSource LMS |
||||
INNER JOIN Library L on L.MediaSourceId = LMS.Id |
||||
INNER JOIN LocalLibrary LL on L.Id = LL.Id |
||||
WHERE L.Name = 'Movies')");
|
||||
migrationBuilder.Sql("INSERT INTO LocalLibrary (Id) Values (last_insert_rowid())"); |
||||
} |
||||
|
||||
protected override void Down(MigrationBuilder migrationBuilder) |
||||
{ |
||||
} |
||||
} |
||||
} |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,287 @@
@@ -0,0 +1,287 @@
|
||||
using System; |
||||
using Microsoft.EntityFrameworkCore.Migrations; |
||||
|
||||
namespace ErsatzTV.Infrastructure.Migrations |
||||
{ |
||||
public partial class Add_OtherVideo_OtherVideoMetadata : Migration |
||||
{ |
||||
protected override void Up(MigrationBuilder migrationBuilder) |
||||
{ |
||||
migrationBuilder.AddColumn<int>( |
||||
name: "OtherVideoMetadataId", |
||||
table: "Tag", |
||||
type: "INTEGER", |
||||
nullable: true); |
||||
|
||||
migrationBuilder.AddColumn<int>( |
||||
name: "OtherVideoMetadataId", |
||||
table: "Studio", |
||||
type: "INTEGER", |
||||
nullable: true); |
||||
|
||||
migrationBuilder.AddColumn<int>( |
||||
name: "OtherVideoMetadataId", |
||||
table: "MetadataGuid", |
||||
type: "INTEGER", |
||||
nullable: true); |
||||
|
||||
migrationBuilder.AddColumn<int>( |
||||
name: "OtherVideoId", |
||||
table: "MediaVersion", |
||||
type: "INTEGER", |
||||
nullable: true); |
||||
|
||||
migrationBuilder.AddColumn<int>( |
||||
name: "OtherVideoMetadataId", |
||||
table: "Genre", |
||||
type: "INTEGER", |
||||
nullable: true); |
||||
|
||||
migrationBuilder.AddColumn<int>( |
||||
name: "OtherVideoMetadataId", |
||||
table: "Artwork", |
||||
type: "INTEGER", |
||||
nullable: true); |
||||
|
||||
migrationBuilder.AddColumn<int>( |
||||
name: "OtherVideoMetadataId", |
||||
table: "Actor", |
||||
type: "INTEGER", |
||||
nullable: true); |
||||
|
||||
migrationBuilder.CreateTable( |
||||
name: "OtherVideo", |
||||
columns: table => new |
||||
{ |
||||
Id = table.Column<int>(type: "INTEGER", nullable: false) |
||||
.Annotation("Sqlite:Autoincrement", true) |
||||
}, |
||||
constraints: table => |
||||
{ |
||||
table.PrimaryKey("PK_OtherVideo", x => x.Id); |
||||
table.ForeignKey( |
||||
name: "FK_OtherVideo_MediaItem_Id", |
||||
column: x => x.Id, |
||||
principalTable: "MediaItem", |
||||
principalColumn: "Id", |
||||
onDelete: ReferentialAction.Cascade); |
||||
}); |
||||
|
||||
migrationBuilder.CreateTable( |
||||
name: "OtherVideoMetadata", |
||||
columns: table => new |
||||
{ |
||||
Id = table.Column<int>(type: "INTEGER", nullable: false) |
||||
.Annotation("Sqlite:Autoincrement", true), |
||||
OtherVideoId = table.Column<int>(type: "INTEGER", nullable: false), |
||||
MetadataKind = table.Column<int>(type: "INTEGER", nullable: false), |
||||
Title = table.Column<string>(type: "TEXT", nullable: true), |
||||
OriginalTitle = table.Column<string>(type: "TEXT", nullable: true), |
||||
SortTitle = table.Column<string>(type: "TEXT", nullable: true), |
||||
Year = table.Column<int>(type: "INTEGER", nullable: true), |
||||
ReleaseDate = table.Column<DateTime>(type: "TEXT", nullable: true), |
||||
DateAdded = table.Column<DateTime>(type: "TEXT", nullable: false), |
||||
DateUpdated = table.Column<DateTime>(type: "TEXT", nullable: false) |
||||
}, |
||||
constraints: table => |
||||
{ |
||||
table.PrimaryKey("PK_OtherVideoMetadata", x => x.Id); |
||||
table.ForeignKey( |
||||
name: "FK_OtherVideoMetadata_OtherVideo_OtherVideoId", |
||||
column: x => x.OtherVideoId, |
||||
principalTable: "OtherVideo", |
||||
principalColumn: "Id", |
||||
onDelete: ReferentialAction.Cascade); |
||||
}); |
||||
|
||||
migrationBuilder.CreateIndex( |
||||
name: "IX_Tag_OtherVideoMetadataId", |
||||
table: "Tag", |
||||
column: "OtherVideoMetadataId"); |
||||
|
||||
migrationBuilder.CreateIndex( |
||||
name: "IX_Studio_OtherVideoMetadataId", |
||||
table: "Studio", |
||||
column: "OtherVideoMetadataId"); |
||||
|
||||
migrationBuilder.CreateIndex( |
||||
name: "IX_MetadataGuid_OtherVideoMetadataId", |
||||
table: "MetadataGuid", |
||||
column: "OtherVideoMetadataId"); |
||||
|
||||
migrationBuilder.CreateIndex( |
||||
name: "IX_MediaVersion_OtherVideoId", |
||||
table: "MediaVersion", |
||||
column: "OtherVideoId"); |
||||
|
||||
migrationBuilder.CreateIndex( |
||||
name: "IX_Genre_OtherVideoMetadataId", |
||||
table: "Genre", |
||||
column: "OtherVideoMetadataId"); |
||||
|
||||
migrationBuilder.CreateIndex( |
||||
name: "IX_Artwork_OtherVideoMetadataId", |
||||
table: "Artwork", |
||||
column: "OtherVideoMetadataId"); |
||||
|
||||
migrationBuilder.CreateIndex( |
||||
name: "IX_Actor_OtherVideoMetadataId", |
||||
table: "Actor", |
||||
column: "OtherVideoMetadataId"); |
||||
|
||||
migrationBuilder.CreateIndex( |
||||
name: "IX_OtherVideoMetadata_OtherVideoId", |
||||
table: "OtherVideoMetadata", |
||||
column: "OtherVideoId"); |
||||
|
||||
migrationBuilder.AddForeignKey( |
||||
name: "FK_Actor_OtherVideoMetadata_OtherVideoMetadataId", |
||||
table: "Actor", |
||||
column: "OtherVideoMetadataId", |
||||
principalTable: "OtherVideoMetadata", |
||||
principalColumn: "Id", |
||||
onDelete: ReferentialAction.Restrict); |
||||
|
||||
migrationBuilder.AddForeignKey( |
||||
name: "FK_Artwork_OtherVideoMetadata_OtherVideoMetadataId", |
||||
table: "Artwork", |
||||
column: "OtherVideoMetadataId", |
||||
principalTable: "OtherVideoMetadata", |
||||
principalColumn: "Id", |
||||
onDelete: ReferentialAction.Cascade); |
||||
|
||||
migrationBuilder.AddForeignKey( |
||||
name: "FK_Genre_OtherVideoMetadata_OtherVideoMetadataId", |
||||
table: "Genre", |
||||
column: "OtherVideoMetadataId", |
||||
principalTable: "OtherVideoMetadata", |
||||
principalColumn: "Id", |
||||
onDelete: ReferentialAction.Restrict); |
||||
|
||||
migrationBuilder.AddForeignKey( |
||||
name: "FK_MediaVersion_OtherVideo_OtherVideoId", |
||||
table: "MediaVersion", |
||||
column: "OtherVideoId", |
||||
principalTable: "OtherVideo", |
||||
principalColumn: "Id", |
||||
onDelete: ReferentialAction.Cascade); |
||||
|
||||
migrationBuilder.AddForeignKey( |
||||
name: "FK_MetadataGuid_OtherVideoMetadata_OtherVideoMetadataId", |
||||
table: "MetadataGuid", |
||||
column: "OtherVideoMetadataId", |
||||
principalTable: "OtherVideoMetadata", |
||||
principalColumn: "Id", |
||||
onDelete: ReferentialAction.Restrict); |
||||
|
||||
migrationBuilder.AddForeignKey( |
||||
name: "FK_Studio_OtherVideoMetadata_OtherVideoMetadataId", |
||||
table: "Studio", |
||||
column: "OtherVideoMetadataId", |
||||
principalTable: "OtherVideoMetadata", |
||||
principalColumn: "Id", |
||||
onDelete: ReferentialAction.Restrict); |
||||
|
||||
migrationBuilder.AddForeignKey( |
||||
name: "FK_Tag_OtherVideoMetadata_OtherVideoMetadataId", |
||||
table: "Tag", |
||||
column: "OtherVideoMetadataId", |
||||
principalTable: "OtherVideoMetadata", |
||||
principalColumn: "Id", |
||||
onDelete: ReferentialAction.Cascade); |
||||
} |
||||
|
||||
protected override void Down(MigrationBuilder migrationBuilder) |
||||
{ |
||||
migrationBuilder.DropForeignKey( |
||||
name: "FK_Actor_OtherVideoMetadata_OtherVideoMetadataId", |
||||
table: "Actor"); |
||||
|
||||
migrationBuilder.DropForeignKey( |
||||
name: "FK_Artwork_OtherVideoMetadata_OtherVideoMetadataId", |
||||
table: "Artwork"); |
||||
|
||||
migrationBuilder.DropForeignKey( |
||||
name: "FK_Genre_OtherVideoMetadata_OtherVideoMetadataId", |
||||
table: "Genre"); |
||||
|
||||
migrationBuilder.DropForeignKey( |
||||
name: "FK_MediaVersion_OtherVideo_OtherVideoId", |
||||
table: "MediaVersion"); |
||||
|
||||
migrationBuilder.DropForeignKey( |
||||
name: "FK_MetadataGuid_OtherVideoMetadata_OtherVideoMetadataId", |
||||
table: "MetadataGuid"); |
||||
|
||||
migrationBuilder.DropForeignKey( |
||||
name: "FK_Studio_OtherVideoMetadata_OtherVideoMetadataId", |
||||
table: "Studio"); |
||||
|
||||
migrationBuilder.DropForeignKey( |
||||
name: "FK_Tag_OtherVideoMetadata_OtherVideoMetadataId", |
||||
table: "Tag"); |
||||
|
||||
migrationBuilder.DropTable( |
||||
name: "OtherVideoMetadata"); |
||||
|
||||
migrationBuilder.DropTable( |
||||
name: "OtherVideo"); |
||||
|
||||
migrationBuilder.DropIndex( |
||||
name: "IX_Tag_OtherVideoMetadataId", |
||||
table: "Tag"); |
||||
|
||||
migrationBuilder.DropIndex( |
||||
name: "IX_Studio_OtherVideoMetadataId", |
||||
table: "Studio"); |
||||
|
||||
migrationBuilder.DropIndex( |
||||
name: "IX_MetadataGuid_OtherVideoMetadataId", |
||||
table: "MetadataGuid"); |
||||
|
||||
migrationBuilder.DropIndex( |
||||
name: "IX_MediaVersion_OtherVideoId", |
||||
table: "MediaVersion"); |
||||
|
||||
migrationBuilder.DropIndex( |
||||
name: "IX_Genre_OtherVideoMetadataId", |
||||
table: "Genre"); |
||||
|
||||
migrationBuilder.DropIndex( |
||||
name: "IX_Artwork_OtherVideoMetadataId", |
||||
table: "Artwork"); |
||||
|
||||
migrationBuilder.DropIndex( |
||||
name: "IX_Actor_OtherVideoMetadataId", |
||||
table: "Actor"); |
||||
|
||||
migrationBuilder.DropColumn( |
||||
name: "OtherVideoMetadataId", |
||||
table: "Tag"); |
||||
|
||||
migrationBuilder.DropColumn( |
||||
name: "OtherVideoMetadataId", |
||||
table: "Studio"); |
||||
|
||||
migrationBuilder.DropColumn( |
||||
name: "OtherVideoMetadataId", |
||||
table: "MetadataGuid"); |
||||
|
||||
migrationBuilder.DropColumn( |
||||
name: "OtherVideoId", |
||||
table: "MediaVersion"); |
||||
|
||||
migrationBuilder.DropColumn( |
||||
name: "OtherVideoMetadataId", |
||||
table: "Genre"); |
||||
|
||||
migrationBuilder.DropColumn( |
||||
name: "OtherVideoMetadataId", |
||||
table: "Artwork"); |
||||
|
||||
migrationBuilder.DropColumn( |
||||
name: "OtherVideoMetadataId", |
||||
table: "Actor"); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,158 @@
@@ -0,0 +1,158 @@
|
||||
@page "/media/other/videos" |
||||
@page "/media/other/videos/page/{PageNumber:int}" |
||||
@using LanguageExt.UnsafeValueAccess |
||||
@using ErsatzTV.Application.MediaCards |
||||
@using ErsatzTV.Application.MediaCollections |
||||
@using ErsatzTV.Application.MediaCollections.Commands |
||||
@using ErsatzTV.Application.Search.Queries |
||||
@using ErsatzTV.Extensions |
||||
@using Unit = LanguageExt.Unit |
||||
@inherits MultiSelectBase<OtherVideoList> |
||||
@inject NavigationManager _navigationManager |
||||
@inject ChannelWriter<IBackgroundServiceRequest> _channel |
||||
|
||||
<MudPaper Square="true" Style="display: flex; height: 64px; left: 240px; padding: 0; position: fixed; right: 0; z-index: 100;"> |
||||
<div style="display: flex; flex-direction: row; margin-bottom: auto; margin-top: auto; width: 100%" class="ml-6 mr-6"> |
||||
@if (IsSelectMode()) |
||||
{ |
||||
<MudText Typo="Typo.h6" Color="Color.Primary">@SelectionLabel()</MudText> |
||||
<div style="margin-left: auto"> |
||||
<MudButton Variant="Variant.Filled" |
||||
Color="Color.Primary" |
||||
StartIcon="@Icons.Material.Filled.Add" |
||||
OnClick="@(_ => AddSelectionToCollection())"> |
||||
Add To Collection |
||||
</MudButton> |
||||
<MudButton Class="ml-3" |
||||
Variant="Variant.Filled" |
||||
Color="Color.Secondary" |
||||
StartIcon="@Icons.Material.Filled.Check" |
||||
OnClick="@(_ => ClearSelection())"> |
||||
Clear Selection |
||||
</MudButton> |
||||
</div> |
||||
} |
||||
else |
||||
{ |
||||
<MudText Style="margin-bottom: auto; margin-top: auto; width: 33%">@_query</MudText> |
||||
<div style="max-width: 300px; width: 33%;"> |
||||
<MudPaper Style="align-items: center; display: flex; justify-content: center;"> |
||||
<MudIconButton Icon="@Icons.Material.Outlined.ChevronLeft" |
||||
OnClick="@PrevPage" |
||||
Disabled="@(PageNumber <= 1)"> |
||||
</MudIconButton> |
||||
<MudText Style="flex-grow: 1" |
||||
Align="Align.Center"> |
||||
@Math.Min((PageNumber - 1) * PageSize + 1, _data.Count)-@Math.Min(_data.Count, PageNumber * PageSize) of @_data.Count |
||||
</MudText> |
||||
<MudIconButton Icon="@Icons.Material.Outlined.ChevronRight" |
||||
OnClick="@NextPage" Disabled="@(PageNumber * PageSize >= _data.Count)"> |
||||
</MudIconButton> |
||||
</MudPaper> |
||||
</div> |
||||
} |
||||
</div> |
||||
</MudPaper> |
||||
<MudContainer MaxWidth="MaxWidth.ExtraLarge" Class="pt-8" Style="margin-top: 64px"> |
||||
<MudContainer MaxWidth="MaxWidth.False" Class="media-card-grid"> |
||||
<FragmentLetterAnchor TCard="OtherVideoCardViewModel" Cards="@_data.Cards"> |
||||
<MediaCard Data="@context" |
||||
Link="" |
||||
ArtworkKind="ArtworkKind.Thumbnail" |
||||
AddToCollectionClicked="@AddToCollection" |
||||
SelectClicked="@(e => SelectClicked(context, e))" |
||||
IsSelected="@IsSelected(context)" |
||||
IsSelectMode="@IsSelectMode()"/> |
||||
</FragmentLetterAnchor> |
||||
</MudContainer> |
||||
</MudContainer> |
||||
@if (_data.PageMap.IsSome) |
||||
{ |
||||
<LetterBar PageMap="@_data.PageMap.ValueUnsafe()" |
||||
BaseUri="/media/other/videos" |
||||
Query="@_query"/> |
||||
} |
||||
|
||||
@code { |
||||
private static int PageSize => 100; |
||||
|
||||
[Parameter] |
||||
public int PageNumber { get; set; } |
||||
|
||||
private OtherVideoCardResultsViewModel _data; |
||||
private string _query; |
||||
|
||||
protected override Task OnParametersSetAsync() |
||||
{ |
||||
if (PageNumber == 0) |
||||
{ |
||||
PageNumber = 1; |
||||
} |
||||
|
||||
_query = _navigationManager.Uri.GetSearchQuery(); |
||||
return RefreshData(); |
||||
} |
||||
|
||||
protected override async Task RefreshData() |
||||
{ |
||||
string searchQuery = string.IsNullOrWhiteSpace(_query) ? "type:other_video" : $"type:other_video AND ({_query})"; |
||||
_data = await Mediator.Send(new QuerySearchIndexOtherVideos(searchQuery, PageNumber, PageSize)); |
||||
} |
||||
|
||||
private void PrevPage() |
||||
{ |
||||
var uri = $"/media/other/videos/page/{PageNumber - 1}"; |
||||
if (!string.IsNullOrWhiteSpace(_query)) |
||||
{ |
||||
(string key, string value) = _query.EncodeQuery(); |
||||
uri = $"{uri}?{key}={value}"; |
||||
} |
||||
_navigationManager.NavigateTo(uri); |
||||
} |
||||
|
||||
private void NextPage() |
||||
{ |
||||
var uri = $"/media/other/videos/page/{PageNumber + 1}"; |
||||
if (!string.IsNullOrWhiteSpace(_query)) |
||||
{ |
||||
(string key, string value) = _query.EncodeQuery(); |
||||
uri = $"{uri}?{key}={value}"; |
||||
} |
||||
_navigationManager.NavigateTo(uri); |
||||
} |
||||
|
||||
private void SelectClicked(MediaCardViewModel card, MouseEventArgs e) |
||||
{ |
||||
List<MediaCardViewModel> GetSortedItems() |
||||
{ |
||||
return _data.Cards.OrderBy(m => m.SortTitle).ToList<MediaCardViewModel>(); |
||||
} |
||||
|
||||
SelectClicked(GetSortedItems, card, e); |
||||
} |
||||
|
||||
private async Task AddToCollection(MediaCardViewModel card) |
||||
{ |
||||
if (card is OtherVideoCardViewModel otherVideo) |
||||
{ |
||||
var parameters = new DialogParameters { { "EntityType", "other video" }, { "EntityName", otherVideo.Title } }; |
||||
var options = new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.ExtraSmall }; |
||||
|
||||
IDialogReference dialog = Dialog.Show<AddToCollectionDialog>("Add To Collection", parameters, options); |
||||
DialogResult result = await dialog.Result; |
||||
if (!result.Cancelled && result.Data is MediaCollectionViewModel collection) |
||||
{ |
||||
var request = new AddOtherVideoToCollection(collection.Id, otherVideo.OtherVideoId); |
||||
Either<BaseError, Unit> addResult = await Mediator.Send(request); |
||||
addResult.Match( |
||||
Left: error => |
||||
{ |
||||
Snackbar.Add($"Unexpected error adding other video to collection: {error.Value}"); |
||||
Logger.LogError("Unexpected error adding other video to collection: {Error}", error.Value); |
||||
}, |
||||
Right: _ => Snackbar.Add($"Added {otherVideo.Title} to collection {collection.Name}", Severity.Success)); |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue