jellyfin/MediaBrowser.LocalMetadata/Images/EpisodeLocalImageProvider.cs

89 lines
3 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
2016-10-25 21:02:04 +02:00
using MediaBrowser.Model.IO;
namespace MediaBrowser.LocalMetadata.Images
{
/// <summary>
/// Episode local image provider.
/// </summary>
public class EpisodeLocalImageProvider : ILocalImageProvider, IHasOrder
{
2014-07-26 19:30:15 +02:00
private readonly IFileSystem _fileSystem;
/// <summary>
/// Initializes a new instance of the <see cref="EpisodeLocalImageProvider"/> class.
/// </summary>
/// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
public EpisodeLocalImageProvider(IFileSystem fileSystem)
2014-07-26 19:30:15 +02:00
{
_fileSystem = fileSystem;
}
/// <inheritdoc />
public string Name => "Local Images";
/// <inheritdoc />
public int Order => 0;
2015-04-11 03:42:37 +02:00
/// <inheritdoc />
2018-09-12 19:26:21 +02:00
public bool Supports(BaseItem item)
{
2014-02-10 21:11:46 +01:00
return item is Episode && item.SupportsLocalMetadata;
}
/// <inheritdoc />
2021-03-09 05:57:38 +01:00
public IEnumerable<LocalImageInfo> GetImages(BaseItem item, IDirectoryService directoryService)
2014-02-08 21:02:35 +01:00
{
var parentPath = Path.GetDirectoryName(item.Path);
if (parentPath == null)
{
return Enumerable.Empty<LocalImageInfo>();
}
2014-02-08 23:38:02 +01:00
2017-08-24 21:52:19 +02:00
var parentPathFiles = directoryService.GetFiles(parentPath);
2014-02-08 23:38:02 +01:00
var nameWithoutExtension = Path.GetFileNameWithoutExtension(item.Path);
2014-02-08 23:38:02 +01:00
2017-06-04 22:24:45 +02:00
return GetFilesFromParentFolder(nameWithoutExtension, parentPathFiles);
2014-02-08 21:02:35 +01:00
}
2017-08-24 21:52:19 +02:00
private List<LocalImageInfo> GetFilesFromParentFolder(string filenameWithoutExtension, List<FileSystemMetadata> parentPathFiles)
{
2014-02-08 23:38:02 +01:00
var thumbName = filenameWithoutExtension + "-thumb";
2017-08-24 21:52:19 +02:00
var list = new List<LocalImageInfo>(1);
foreach (var i in parentPathFiles)
{
if (i.IsDirectory)
{
continue;
}
if (BaseItem.SupportedImageExtensions.Contains(i.Extension, StringComparer.OrdinalIgnoreCase))
{
var currentNameWithoutExtension = _fileSystem.GetFileNameWithoutExtension(i);
if (string.Equals(filenameWithoutExtension, currentNameWithoutExtension, StringComparison.OrdinalIgnoreCase))
{
list.Add(new LocalImageInfo { FileInfo = i, Type = ImageType.Primary });
2017-08-24 21:52:19 +02:00
}
else if (string.Equals(thumbName, currentNameWithoutExtension, StringComparison.OrdinalIgnoreCase))
{
list.Add(new LocalImageInfo { FileInfo = i, Type = ImageType.Primary });
2017-08-24 21:52:19 +02:00
}
}
}
2017-08-24 21:52:19 +02:00
return list;
2014-02-08 23:38:02 +01:00
}
}
}