using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Jellyfin.Extensions;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO;
namespace MediaBrowser.LocalMetadata.Images
{
///
/// Episode local image provider.
///
public class EpisodeLocalImageProvider : ILocalImageProvider, IHasOrder
{
///
public string Name => "Local Images";
///
public int Order => 0;
///
public bool Supports(BaseItem item)
{
return item is Episode && item.SupportsLocalMetadata;
}
///
public IEnumerable GetImages(BaseItem item, IDirectoryService directoryService)
{
var parentPath = Path.GetDirectoryName(item.Path);
if (parentPath == null)
{
return Enumerable.Empty();
}
var parentPathFiles = directoryService.GetFiles(parentPath);
var nameWithoutExtension = Path.GetFileNameWithoutExtension(item.Path.AsSpan());
return GetFilesFromParentFolder(nameWithoutExtension, parentPathFiles);
}
private List GetFilesFromParentFolder(ReadOnlySpan filenameWithoutExtension, List parentPathFiles)
{
var thumbName = string.Concat(filenameWithoutExtension, "-thumb");
var list = new List(1);
foreach (var i in parentPathFiles)
{
if (i.IsDirectory)
{
continue;
}
if (BaseItem.SupportedImageExtensions.Contains(i.Extension.AsSpan(), StringComparison.OrdinalIgnoreCase))
{
var currentNameWithoutExtension = Path.GetFileNameWithoutExtension(i.FullName.AsSpan());
if (filenameWithoutExtension.Equals(currentNameWithoutExtension, StringComparison.OrdinalIgnoreCase))
{
list.Add(new LocalImageInfo { FileInfo = i, Type = ImageType.Primary });
}
else if (currentNameWithoutExtension.Equals(thumbName, StringComparison.OrdinalIgnoreCase))
{
list.Add(new LocalImageInfo { FileInfo = i, Type = ImageType.Primary });
}
}
}
return list;
}
}
}