jellyfin/MediaBrowser.LocalMetadata/Images/EpisodeLocalImageProvider.cs

79 lines
2.6 KiB
C#
Raw Normal View History

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;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Model.IO;
namespace MediaBrowser.LocalMetadata.Images
{
/// <summary>
/// Episode local image provider.
/// </summary>
public class EpisodeLocalImageProvider : ILocalImageProvider, IHasOrder
{
/// <inheritdoc />
public string Name => "Local Images";
/// <inheritdoc />
public int Order => 0;
2015-04-11 01:42:37 +00:00
/// <inheritdoc />
2018-09-12 17:26:21 +00:00
public bool Supports(BaseItem item)
{
2014-02-10 20:11:46 +00:00
return item is Episode && item.SupportsLocalMetadata;
}
/// <inheritdoc />
2021-03-09 04:57:38 +00:00
public IEnumerable<LocalImageInfo> GetImages(BaseItem item, IDirectoryService directoryService)
2014-02-08 20:02:35 +00:00
{
var parentPath = Path.GetDirectoryName(item.Path);
2022-12-05 14:00:20 +00:00
if (parentPath is null)
{
return Enumerable.Empty<LocalImageInfo>();
}
2014-02-08 22:38:02 +00:00
2017-08-24 19:52:19 +00:00
var parentPathFiles = directoryService.GetFiles(parentPath);
2014-02-08 22:38:02 +00:00
2021-05-23 22:30:41 +00:00
var nameWithoutExtension = Path.GetFileNameWithoutExtension(item.Path.AsSpan());
2014-02-08 22:38:02 +00:00
2017-06-04 20:24:45 +00:00
return GetFilesFromParentFolder(nameWithoutExtension, parentPathFiles);
2014-02-08 20:02:35 +00:00
}
2021-05-23 22:30:41 +00:00
private List<LocalImageInfo> GetFilesFromParentFolder(ReadOnlySpan<char> filenameWithoutExtension, List<FileSystemMetadata> parentPathFiles)
{
2021-05-23 22:30:41 +00:00
var thumbName = string.Concat(filenameWithoutExtension, "-thumb");
2017-08-24 19:52:19 +00:00
var list = new List<LocalImageInfo>(1);
foreach (var i in parentPathFiles)
{
if (i.IsDirectory)
{
continue;
}
2021-05-23 22:30:41 +00:00
if (BaseItem.SupportedImageExtensions.Contains(i.Extension.AsSpan(), StringComparison.OrdinalIgnoreCase))
2017-08-24 19:52:19 +00:00
{
2021-05-23 22:30:41 +00:00
var currentNameWithoutExtension = Path.GetFileNameWithoutExtension(i.FullName.AsSpan());
2017-08-24 19:52:19 +00:00
2021-05-23 22:30:41 +00:00
if (filenameWithoutExtension.Equals(currentNameWithoutExtension, StringComparison.OrdinalIgnoreCase))
2017-08-24 19:52:19 +00:00
{
list.Add(new LocalImageInfo { FileInfo = i, Type = ImageType.Primary });
2017-08-24 19:52:19 +00:00
}
2021-05-23 22:30:41 +00:00
else if (currentNameWithoutExtension.Equals(thumbName, StringComparison.OrdinalIgnoreCase))
2017-08-24 19:52:19 +00:00
{
list.Add(new LocalImageInfo { FileInfo = i, Type = ImageType.Primary });
2017-08-24 19:52:19 +00:00
}
}
}
2017-08-24 19:52:19 +00:00
return list;
2014-02-08 22:38:02 +00:00
}
}
}