mirror of https://github.com/ErsatzTV/ErsatzTV.git
Compare commits
55 Commits
505e135482
...
474e647d6d
| Author | SHA1 | Date |
|---|---|---|
|
|
474e647d6d | 18 hours ago |
|
|
daff1c6533 | 1 day ago |
|
|
14d2dd0c3a | 1 day ago |
|
|
c606319030 | 1 day ago |
|
|
1b72b8491c | 1 day ago |
|
|
9fea25a77d | 2 days ago |
|
|
74b049b6e3 | 2 days ago |
|
|
b2caf8ee8d | 2 days ago |
|
|
b582b4cbf7 | 1 week ago |
|
|
0af81ad839 | 2 weeks ago |
|
|
2f0cd1eb6c | 2 weeks ago |
|
|
e4f1a93db0 | 2 weeks ago |
|
|
6562d616fb | 3 weeks ago |
|
|
d8122edad6 | 3 weeks ago |
|
|
99b8c56a31 | 3 weeks ago |
|
|
09858df654 | 3 weeks ago |
|
|
038286c92b | 3 weeks ago |
|
|
8575ab5c32 | 3 weeks ago |
|
|
8b768a2990 | 3 weeks ago |
|
|
f9e4c4d386 | 3 weeks ago |
|
|
a1f9b86fc1 | 3 weeks ago |
|
|
5dc20ebd1b | 4 weeks ago |
|
|
d30e8b4102 | 4 weeks ago |
|
|
c14f373f23 | 4 weeks ago |
|
|
a90fe26d50 | 4 weeks ago |
|
|
7a263ddaed | 4 weeks ago |
|
|
3e0a9aae1e | 4 weeks ago |
|
|
72dc401829 | 4 weeks ago |
|
|
85e25ca6ea | 4 weeks ago |
|
|
9c23b03758 | 4 weeks ago |
|
|
e12888ebee | 4 weeks ago |
|
|
468ff087d4 | 1 month ago |
|
|
54606c76f9 | 1 month ago |
|
|
6bd49ffcec | 1 month ago |
|
|
c524bc0d7d | 1 month ago |
|
|
42bcadf936 | 1 month ago |
|
|
1f31beab5b | 1 month ago |
|
|
b45c22092d | 1 month ago |
|
|
7bd8cefe2e | 1 month ago |
|
|
f101d0b366 | 1 month ago |
|
|
73aabdabda | 1 month ago |
|
|
bcea96d53a | 1 month ago |
|
|
d7952e4cfa | 1 month ago |
|
|
758399e339 | 1 month ago |
|
|
6c635a4be9 | 1 month ago |
|
|
9d637cdd54 | 1 month ago |
|
|
cc287ffc6e | 1 month ago |
|
|
371659c5c5 | 1 month ago |
|
|
7afb1866ad | 1 month ago |
|
|
7bc1dd63fe | 1 month ago |
|
|
076b8a7188 | 1 month ago |
|
|
ec0d8ea6ac | 1 month ago |
|
|
e40d192aea | 1 month ago |
|
|
bd7fd8984c | 1 month ago |
|
|
2682912f5a | 1 month ago |
334 changed files with 121792 additions and 2383 deletions
@ -0,0 +1,15 @@ |
|||||||
|
<Project> |
||||||
|
<PropertyGroup> |
||||||
|
<EnableThreadingAnalyzers Condition="'$(EnableThreadingAnalyzers)' == ''">false</EnableThreadingAnalyzers> |
||||||
|
</PropertyGroup> |
||||||
|
|
||||||
|
<ItemGroup> |
||||||
|
<PackageReference |
||||||
|
Include="Microsoft.VisualStudio.Threading.Analyzers" |
||||||
|
Version="17.14.15" |
||||||
|
Condition="'$(EnableThreadingAnalyzers)' == 'true'"> |
||||||
|
<PrivateAssets>all</PrivateAssets> |
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> |
||||||
|
</PackageReference> |
||||||
|
</ItemGroup> |
||||||
|
</Project> |
||||||
|
Before Width: | Height: | Size: 4.2 KiB After Width: | Height: | Size: 15 KiB |
@ -1,3 +1,5 @@ |
|||||||
namespace ErsatzTV.Application.Channels; |
using ErsatzTV.FFmpeg; |
||||||
|
|
||||||
public record GetChannelFramerate(string ChannelNumber) : IRequest<Option<int>>; |
namespace ErsatzTV.Application.Channels; |
||||||
|
|
||||||
|
public record GetChannelFramerate(string ChannelNumber) : IRequest<Option<FrameRate>>; |
||||||
|
|||||||
@ -0,0 +1,3 @@ |
|||||||
|
namespace ErsatzTV.Application.Troubleshooting; |
||||||
|
|
||||||
|
public record ArchiveMediaSample(int MediaItemId) : IRequest<Option<string>>; |
||||||
@ -0,0 +1,169 @@ |
|||||||
|
using System.IO.Abstractions; |
||||||
|
using System.IO.Compression; |
||||||
|
using CliWrap; |
||||||
|
using CliWrap.Buffered; |
||||||
|
using ErsatzTV.Core; |
||||||
|
using ErsatzTV.Core.Domain; |
||||||
|
using ErsatzTV.Core.Interfaces.Emby; |
||||||
|
using ErsatzTV.Core.Interfaces.Jellyfin; |
||||||
|
using ErsatzTV.Core.Interfaces.Plex; |
||||||
|
using ErsatzTV.Infrastructure.Data; |
||||||
|
using Microsoft.EntityFrameworkCore; |
||||||
|
using Microsoft.Extensions.Logging; |
||||||
|
|
||||||
|
namespace ErsatzTV.Application.Troubleshooting; |
||||||
|
|
||||||
|
public class ArchiveMediaSampleHandler( |
||||||
|
IDbContextFactory<TvContext> dbContextFactory, |
||||||
|
IPlexPathReplacementService plexPathReplacementService, |
||||||
|
IJellyfinPathReplacementService jellyfinPathReplacementService, |
||||||
|
IEmbyPathReplacementService embyPathReplacementService, |
||||||
|
IFileSystem fileSystem, |
||||||
|
ILogger<ArchiveMediaSampleHandler> logger) |
||||||
|
: TroubleshootingHandlerBase( |
||||||
|
plexPathReplacementService, |
||||||
|
jellyfinPathReplacementService, |
||||||
|
embyPathReplacementService, |
||||||
|
fileSystem), IRequestHandler<ArchiveMediaSample, Option<string>> |
||||||
|
{ |
||||||
|
private readonly IFileSystem _fileSystem = fileSystem; |
||||||
|
|
||||||
|
public async Task<Option<string>> Handle(ArchiveMediaSample request, CancellationToken cancellationToken) |
||||||
|
{ |
||||||
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken); |
||||||
|
Validation<BaseError, Tuple<MediaItem, string>> validation = await Validate( |
||||||
|
dbContext, |
||||||
|
request, |
||||||
|
cancellationToken); |
||||||
|
|
||||||
|
foreach ((MediaItem mediaItem, string ffmpegPath) in validation.SuccessToSeq()) |
||||||
|
{ |
||||||
|
Option<string> maybeMediaSample = await GetMediaSample( |
||||||
|
request, |
||||||
|
dbContext, |
||||||
|
mediaItem, |
||||||
|
ffmpegPath, |
||||||
|
cancellationToken); |
||||||
|
|
||||||
|
foreach (string mediaSample in maybeMediaSample) |
||||||
|
{ |
||||||
|
return await GetArchive(request, mediaSample, cancellationToken); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return Option<string>.None; |
||||||
|
} |
||||||
|
|
||||||
|
private async Task<Option<string>> GetArchive( |
||||||
|
ArchiveMediaSample request, |
||||||
|
string mediaSample, |
||||||
|
CancellationToken cancellationToken) |
||||||
|
{ |
||||||
|
string tempFile = Path.GetTempFileName(); |
||||||
|
|
||||||
|
try |
||||||
|
{ |
||||||
|
await using ZipArchive zipArchive = await ZipFile.OpenAsync( |
||||||
|
tempFile, |
||||||
|
ZipArchiveMode.Update, |
||||||
|
cancellationToken); |
||||||
|
|
||||||
|
string fileName = Path.GetFileName(mediaSample); |
||||||
|
await zipArchive.CreateEntryFromFileAsync(mediaSample, fileName, cancellationToken); |
||||||
|
|
||||||
|
return tempFile; |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
logger.LogWarning(ex, "Failed to archive media sample for media item {MediaItemId}", request.MediaItemId); |
||||||
|
_fileSystem.File.Delete(tempFile); |
||||||
|
} |
||||||
|
|
||||||
|
return Option<string>.None; |
||||||
|
} |
||||||
|
|
||||||
|
private async Task<Option<string>> GetMediaSample( |
||||||
|
ArchiveMediaSample request, |
||||||
|
TvContext dbContext, |
||||||
|
MediaItem mediaItem, |
||||||
|
string ffmpegPath, |
||||||
|
CancellationToken cancellationToken) |
||||||
|
{ |
||||||
|
try |
||||||
|
{ |
||||||
|
string mediaItemPath = await GetMediaItemPath(dbContext, mediaItem, cancellationToken); |
||||||
|
if (string.IsNullOrEmpty(mediaItemPath)) |
||||||
|
{ |
||||||
|
logger.LogWarning( |
||||||
|
"Media item {MediaItemId} does not exist on disk; cannot extract media sample.", |
||||||
|
mediaItem.Id); |
||||||
|
|
||||||
|
return Option<string>.None; |
||||||
|
} |
||||||
|
|
||||||
|
string extension = Path.GetExtension(mediaItemPath); |
||||||
|
if (string.IsNullOrWhiteSpace(extension)) |
||||||
|
{ |
||||||
|
// this can help with remote servers (e.g. mediaItemPath is http://localhost/whatever)
|
||||||
|
extension = Path.GetExtension(await GetLocalPath(mediaItem, cancellationToken)); |
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(extension)) |
||||||
|
{ |
||||||
|
// fall back to mkv when extension is otherwise unknown
|
||||||
|
extension = "mkv"; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
string tempPath = Path.GetTempPath(); |
||||||
|
string fileName = Path.ChangeExtension(Guid.NewGuid().ToString(), extension); |
||||||
|
string outputPath = Path.Combine(tempPath, fileName); |
||||||
|
|
||||||
|
List<string> arguments = |
||||||
|
[ |
||||||
|
"-nostdin", |
||||||
|
"-i", mediaItemPath, |
||||||
|
"-t", "30", |
||||||
|
"-map", "0", |
||||||
|
"-c", "copy", |
||||||
|
outputPath |
||||||
|
]; |
||||||
|
|
||||||
|
using var cts = new CancellationTokenSource(TimeSpan.FromMinutes(2)); |
||||||
|
using var linkedTokenSource = |
||||||
|
CancellationTokenSource.CreateLinkedTokenSource(cts.Token, cancellationToken); |
||||||
|
|
||||||
|
logger.LogDebug("media sample arguments {Arguments}", arguments); |
||||||
|
|
||||||
|
BufferedCommandResult result = await Cli.Wrap(ffmpegPath) |
||||||
|
.WithArguments(arguments) |
||||||
|
.WithWorkingDirectory(FileSystemLayout.FontsCacheFolder) |
||||||
|
.WithStandardErrorPipe(PipeTarget.Null) |
||||||
|
.WithValidation(CommandResultValidation.None) |
||||||
|
.ExecuteBufferedAsync(linkedTokenSource.Token); |
||||||
|
|
||||||
|
if (result.IsSuccess) |
||||||
|
{ |
||||||
|
return outputPath; |
||||||
|
} |
||||||
|
|
||||||
|
logger.LogWarning( |
||||||
|
"Failed to extract media sample for media item {MediaItemId} - exit code {ExitCode}", |
||||||
|
request.MediaItemId, |
||||||
|
result.ExitCode); |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
logger.LogWarning(ex, "Failed to extract media sample for media item {MediaItemId}", request.MediaItemId); |
||||||
|
} |
||||||
|
|
||||||
|
return Option<string>.None; |
||||||
|
} |
||||||
|
|
||||||
|
private static async Task<Validation<BaseError, Tuple<MediaItem, string>>> Validate( |
||||||
|
TvContext dbContext, |
||||||
|
ArchiveMediaSample request, |
||||||
|
CancellationToken cancellationToken) => |
||||||
|
(await MediaItemMustExist(dbContext, request.MediaItemId, cancellationToken), |
||||||
|
await FFmpegPathMustExist(dbContext, cancellationToken)) |
||||||
|
.Apply((mediaItem, ffmpegPath) => Tuple(mediaItem, ffmpegPath)); |
||||||
|
} |
||||||
@ -0,0 +1,167 @@ |
|||||||
|
using System.IO.Abstractions; |
||||||
|
using Dapper; |
||||||
|
using ErsatzTV.Core; |
||||||
|
using ErsatzTV.Core.Domain; |
||||||
|
using ErsatzTV.Core.Errors; |
||||||
|
using ErsatzTV.Core.Extensions; |
||||||
|
using ErsatzTV.Core.Interfaces.Emby; |
||||||
|
using ErsatzTV.Core.Interfaces.Jellyfin; |
||||||
|
using ErsatzTV.Core.Interfaces.Plex; |
||||||
|
using ErsatzTV.Infrastructure.Data; |
||||||
|
using ErsatzTV.Infrastructure.Extensions; |
||||||
|
using Microsoft.EntityFrameworkCore; |
||||||
|
|
||||||
|
namespace ErsatzTV.Application.Troubleshooting; |
||||||
|
|
||||||
|
public abstract class TroubleshootingHandlerBase( |
||||||
|
IPlexPathReplacementService plexPathReplacementService, |
||||||
|
IJellyfinPathReplacementService jellyfinPathReplacementService, |
||||||
|
IEmbyPathReplacementService embyPathReplacementService, |
||||||
|
IFileSystem fileSystem) |
||||||
|
{ |
||||||
|
protected static async Task<Validation<BaseError, MediaItem>> MediaItemMustExist( |
||||||
|
TvContext dbContext, |
||||||
|
int mediaItemId, |
||||||
|
CancellationToken cancellationToken) => |
||||||
|
await dbContext.MediaItems |
||||||
|
.AsNoTracking() |
||||||
|
.Include(mi => (mi as Episode).EpisodeMetadata) |
||||||
|
.ThenInclude(em => em.Subtitles) |
||||||
|
.Include(mi => (mi as Episode).MediaVersions) |
||||||
|
.ThenInclude(mv => mv.MediaFiles) |
||||||
|
.Include(mi => (mi as Episode).MediaVersions) |
||||||
|
.ThenInclude(mv => mv.Streams) |
||||||
|
.Include(mi => (mi as Episode).Season) |
||||||
|
.ThenInclude(s => s.Show) |
||||||
|
.ThenInclude(s => s.ShowMetadata) |
||||||
|
.Include(mi => (mi as Movie).MovieMetadata) |
||||||
|
.ThenInclude(mm => mm.Subtitles) |
||||||
|
.Include(mi => (mi as Movie).MediaVersions) |
||||||
|
.ThenInclude(mv => mv.MediaFiles) |
||||||
|
.Include(mi => (mi as Movie).MediaVersions) |
||||||
|
.ThenInclude(mv => mv.Streams) |
||||||
|
.Include(mi => (mi as MusicVideo).MusicVideoMetadata) |
||||||
|
.ThenInclude(mvm => mvm.Subtitles) |
||||||
|
.Include(mi => (mi as MusicVideo).MusicVideoMetadata) |
||||||
|
.ThenInclude(mvm => mvm.Artists) |
||||||
|
.Include(mi => (mi as MusicVideo).MusicVideoMetadata) |
||||||
|
.ThenInclude(mvm => mvm.Studios) |
||||||
|
.Include(mi => (mi as MusicVideo).MusicVideoMetadata) |
||||||
|
.ThenInclude(mvm => mvm.Directors) |
||||||
|
.Include(mi => (mi as MusicVideo).MediaVersions) |
||||||
|
.ThenInclude(mv => mv.MediaFiles) |
||||||
|
.Include(mi => (mi as MusicVideo).MediaVersions) |
||||||
|
.ThenInclude(mv => mv.Streams) |
||||||
|
.Include(mi => (mi as MusicVideo).Artist) |
||||||
|
.ThenInclude(mv => mv.ArtistMetadata) |
||||||
|
.Include(mi => (mi as OtherVideo).OtherVideoMetadata) |
||||||
|
.ThenInclude(ovm => ovm.Subtitles) |
||||||
|
.Include(mi => (mi as OtherVideo).MediaVersions) |
||||||
|
.ThenInclude(ov => ov.MediaFiles) |
||||||
|
.Include(mi => (mi as OtherVideo).MediaVersions) |
||||||
|
.ThenInclude(ov => ov.Streams) |
||||||
|
.Include(mi => (mi as Song).MediaVersions) |
||||||
|
.ThenInclude(mv => mv.MediaFiles) |
||||||
|
.Include(mi => (mi as Song).MediaVersions) |
||||||
|
.ThenInclude(mv => mv.Streams) |
||||||
|
.Include(mi => (mi as Song).SongMetadata) |
||||||
|
.ThenInclude(sm => sm.Artwork) |
||||||
|
.Include(mi => (mi as Image).MediaVersions) |
||||||
|
.ThenInclude(mv => mv.MediaFiles) |
||||||
|
.Include(mi => (mi as Image).MediaVersions) |
||||||
|
.ThenInclude(mv => mv.Streams) |
||||||
|
.Include(mi => (mi as Image).ImageMetadata) |
||||||
|
.Include(mi => (mi as RemoteStream).MediaVersions) |
||||||
|
.ThenInclude(mv => mv.MediaFiles) |
||||||
|
.Include(mi => (mi as RemoteStream).MediaVersions) |
||||||
|
.ThenInclude(mv => mv.Streams) |
||||||
|
.Include(mi => (mi as RemoteStream).RemoteStreamMetadata) |
||||||
|
.AsSplitQuery() |
||||||
|
.SingleOrDefaultAsync(mi => mi.Id == mediaItemId, cancellationToken) |
||||||
|
.Map(Optional) |
||||||
|
.Map(o => o.ToValidation<BaseError>(new UnableToLocatePlayoutItem())); |
||||||
|
|
||||||
|
protected static Task<Validation<BaseError, string>> FFmpegPathMustExist( |
||||||
|
TvContext dbContext, |
||||||
|
CancellationToken cancellationToken) => |
||||||
|
dbContext.ConfigElements.GetValue<string>(ConfigElementKey.FFmpegPath, cancellationToken) |
||||||
|
.FilterT(File.Exists) |
||||||
|
.Map(maybePath => maybePath.ToValidation<BaseError>("FFmpeg path does not exist on filesystem")); |
||||||
|
|
||||||
|
protected Task<string> GetLocalPath(MediaItem mediaItem, CancellationToken cancellationToken) => |
||||||
|
mediaItem.GetLocalPath( |
||||||
|
plexPathReplacementService, |
||||||
|
jellyfinPathReplacementService, |
||||||
|
embyPathReplacementService, |
||||||
|
cancellationToken); |
||||||
|
|
||||||
|
protected async Task<string> GetMediaItemPath( |
||||||
|
TvContext dbContext, |
||||||
|
MediaItem mediaItem, |
||||||
|
CancellationToken cancellationToken) |
||||||
|
{ |
||||||
|
string path = await GetLocalPath(mediaItem, cancellationToken); |
||||||
|
|
||||||
|
// check filesystem first
|
||||||
|
if (fileSystem.File.Exists(path)) |
||||||
|
{ |
||||||
|
if (mediaItem is RemoteStream remoteStream) |
||||||
|
{ |
||||||
|
path = !string.IsNullOrWhiteSpace(remoteStream.Url) |
||||||
|
? remoteStream.Url |
||||||
|
: $"http://localhost:{Settings.StreamingPort}/ffmpeg/remote-stream/{remoteStream.Id}"; |
||||||
|
} |
||||||
|
|
||||||
|
return path; |
||||||
|
} |
||||||
|
|
||||||
|
// attempt to remotely stream plex
|
||||||
|
MediaFile file = mediaItem.GetHeadVersion().MediaFiles.Head(); |
||||||
|
switch (file) |
||||||
|
{ |
||||||
|
case PlexMediaFile pmf: |
||||||
|
Option<int> maybeId = await dbContext.Connection.QuerySingleOrDefaultAsync<int>( |
||||||
|
@"SELECT PMS.Id FROM PlexMediaSource PMS
|
||||||
|
INNER JOIN Library L on PMS.Id = L.MediaSourceId |
||||||
|
INNER JOIN LibraryPath LP on L.Id = LP.LibraryId |
||||||
|
WHERE LP.Id = @LibraryPathId",
|
||||||
|
new { mediaItem.LibraryPathId }) |
||||||
|
.Map(Optional); |
||||||
|
|
||||||
|
foreach (int plexMediaSourceId in maybeId) |
||||||
|
{ |
||||||
|
return $"http://localhost:{Settings.StreamingPort}/media/plex/{plexMediaSourceId}/{pmf.Key}"; |
||||||
|
} |
||||||
|
|
||||||
|
break; |
||||||
|
} |
||||||
|
|
||||||
|
// attempt to remotely stream jellyfin
|
||||||
|
Option<string> jellyfinItemId = mediaItem switch |
||||||
|
{ |
||||||
|
JellyfinEpisode e => e.ItemId, |
||||||
|
JellyfinMovie m => m.ItemId, |
||||||
|
_ => None |
||||||
|
}; |
||||||
|
|
||||||
|
foreach (string itemId in jellyfinItemId) |
||||||
|
{ |
||||||
|
return $"http://localhost:{Settings.StreamingPort}/media/jellyfin/{itemId}"; |
||||||
|
} |
||||||
|
|
||||||
|
// attempt to remotely stream emby
|
||||||
|
Option<string> embyItemId = mediaItem switch |
||||||
|
{ |
||||||
|
EmbyEpisode e => e.ItemId, |
||||||
|
EmbyMovie m => m.ItemId, |
||||||
|
_ => None |
||||||
|
}; |
||||||
|
|
||||||
|
foreach (string itemId in embyItemId) |
||||||
|
{ |
||||||
|
return $"http://localhost:{Settings.StreamingPort}/media/emby/{itemId}"; |
||||||
|
} |
||||||
|
|
||||||
|
return null; |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,15 @@ |
|||||||
|
using System.ComponentModel; |
||||||
|
|
||||||
|
namespace ErsatzTV.Core.Api.ScriptedPlayout; |
||||||
|
|
||||||
|
public record ContentSearch |
||||||
|
{ |
||||||
|
[Description("Unique name used to reference this content throughout the scripted schedule")] |
||||||
|
public required string Key { get; set; } |
||||||
|
|
||||||
|
[Description("The search query")] |
||||||
|
public required string Query { get; set; } |
||||||
|
|
||||||
|
[Description("The playback order; only chronological and shuffle are currently supported")] |
||||||
|
public string Order { get; set; } = "shuffle"; |
||||||
|
} |
||||||
@ -0,0 +1,14 @@ |
|||||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||||
|
|
||||||
|
<PropertyGroup> |
||||||
|
<TargetFramework>net10.0</TargetFramework> |
||||||
|
<ImplicitUsings>enable</ImplicitUsings> |
||||||
|
<Nullable>enable</Nullable> |
||||||
|
<RootNamespace>ErsatzTV.Core</RootNamespace> |
||||||
|
</PropertyGroup> |
||||||
|
|
||||||
|
<ItemGroup> |
||||||
|
<Folder Include="Api\" /> |
||||||
|
</ItemGroup> |
||||||
|
|
||||||
|
</Project> |
||||||
@ -1,8 +0,0 @@ |
|||||||
namespace ErsatzTV.Core.Tests.Fakes; |
|
||||||
|
|
||||||
public record FakeFileEntry(string Path) |
|
||||||
{ |
|
||||||
public DateTime LastWriteTime { get; set; } = SystemTime.MinValueUtc; |
|
||||||
|
|
||||||
public string Contents { get; set; } |
|
||||||
} |
|
||||||
@ -1,3 +0,0 @@ |
|||||||
namespace ErsatzTV.Core.Tests.Fakes; |
|
||||||
|
|
||||||
public record FakeFolderEntry(string Path); |
|
||||||
@ -1,99 +0,0 @@ |
|||||||
using ErsatzTV.Core.Domain; |
|
||||||
using ErsatzTV.Core.Interfaces.Metadata; |
|
||||||
|
|
||||||
namespace ErsatzTV.Core.Tests.Fakes; |
|
||||||
|
|
||||||
public class FakeLocalFileSystem : ILocalFileSystem |
|
||||||
{ |
|
||||||
private readonly List<FakeFileEntry> _files; |
|
||||||
private readonly List<FakeFolderEntry> _folders; |
|
||||||
|
|
||||||
public FakeLocalFileSystem(List<FakeFileEntry> files) : this(files, new List<FakeFolderEntry>()) |
|
||||||
{ |
|
||||||
} |
|
||||||
|
|
||||||
public FakeLocalFileSystem(List<FakeFileEntry> files, List<FakeFolderEntry> folders) |
|
||||||
{ |
|
||||||
_files = files; |
|
||||||
|
|
||||||
var allFolders = new List<string>(folders.Map(f => f.Path)); |
|
||||||
foreach (FakeFileEntry file in _files) |
|
||||||
{ |
|
||||||
List<DirectoryInfo> moreFolders = |
|
||||||
Split(new DirectoryInfo(Path.GetDirectoryName(file.Path) ?? string.Empty)); |
|
||||||
allFolders.AddRange(moreFolders.Map(i => i.FullName)); |
|
||||||
} |
|
||||||
|
|
||||||
_folders = allFolders.Distinct().Map(f => new FakeFolderEntry(f)).ToList(); |
|
||||||
} |
|
||||||
|
|
||||||
public Unit EnsureFolderExists(string folder) => Unit.Default; |
|
||||||
|
|
||||||
public DateTime GetLastWriteTime(string path) => |
|
||||||
Optional(_files.SingleOrDefault(f => f.Path == path)) |
|
||||||
.Map(f => f.LastWriteTime) |
|
||||||
.IfNone(SystemTime.MinValueUtc); |
|
||||||
|
|
||||||
public bool IsLibraryPathAccessible(LibraryPath libraryPath) => |
|
||||||
_folders.Any(f => f.Path == libraryPath.Path); |
|
||||||
|
|
||||||
public IEnumerable<string> ListSubdirectories(string folder) => |
|
||||||
_folders.Map(f => f.Path).Filter(f => f.StartsWith(folder) && Directory.GetParent(f)?.FullName == folder); |
|
||||||
|
|
||||||
public IEnumerable<string> ListFiles(string folder) => |
|
||||||
_files.Map(f => f.Path).Filter(f => Path.GetDirectoryName(f) == folder); |
|
||||||
|
|
||||||
// TODO: this isn't accurate, need to use search pattern
|
|
||||||
public IEnumerable<string> ListFiles(string folder, string searchPattern) => |
|
||||||
_files.Map(f => f.Path).Filter(f => Path.GetDirectoryName(f) == folder); |
|
||||||
|
|
||||||
public IEnumerable<string> ListFiles(string folder, params string[] searchPatterns) => |
|
||||||
_files.Map(f => f.Path).Filter(f => Path.GetDirectoryName(f) == folder); |
|
||||||
|
|
||||||
public bool FileExists(string path) => _files.Any(f => f.Path == path); |
|
||||||
public bool FolderExists(string folder) => false; |
|
||||||
|
|
||||||
public Task<Either<BaseError, Unit>> CopyFile(string source, string destination) => |
|
||||||
Task.FromResult(Right<BaseError, Unit>(Unit.Default)); |
|
||||||
|
|
||||||
public Unit EmptyFolder(string folder) => Unit.Default; |
|
||||||
|
|
||||||
public async Task<string> ReadAllText(string path) => await _files |
|
||||||
.Filter(f => f.Path == path) |
|
||||||
.HeadOrNone() |
|
||||||
.Select(f => f.Contents) |
|
||||||
.IfNoneAsync(string.Empty); |
|
||||||
|
|
||||||
public async Task<string[]> ReadAllLines(string path) => await _files |
|
||||||
.Filter(f => f.Path == path) |
|
||||||
.HeadOrNone() |
|
||||||
.Select(f => f.Contents) |
|
||||||
.IfNoneAsync(string.Empty) |
|
||||||
.Map(s => s.Split(Environment.NewLine)); |
|
||||||
|
|
||||||
public Task<byte[]> GetHash(string path) => throw new NotSupportedException(); |
|
||||||
|
|
||||||
public string GetCustomOrDefaultFile(string folder, string file) |
|
||||||
{ |
|
||||||
string path = Path.Combine(folder, file); |
|
||||||
return FileExists(path) ? path : Path.Combine(folder, $"_{file}"); |
|
||||||
} |
|
||||||
|
|
||||||
private static List<DirectoryInfo> Split(DirectoryInfo path) |
|
||||||
{ |
|
||||||
var result = new List<DirectoryInfo>(); |
|
||||||
if (path == null || string.IsNullOrWhiteSpace(path.FullName)) |
|
||||||
{ |
|
||||||
return result; |
|
||||||
} |
|
||||||
|
|
||||||
if (path.Parent != null) |
|
||||||
{ |
|
||||||
result.AddRange(Split(path.Parent)); |
|
||||||
} |
|
||||||
|
|
||||||
result.Add(path); |
|
||||||
|
|
||||||
return result; |
|
||||||
} |
|
||||||
} |
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue