jellyfin/MediaBrowser.LocalMetadata/Images/LocalImageProvider.cs

494 lines
17 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
2021-12-20 04:15:20 -07:00
using Jellyfin.Extensions;
using MediaBrowser.Controller.Entities;
2014-01-31 12:55:21 -07:00
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
2016-10-25 12:02:04 -07:00
using MediaBrowser.Model.IO;
namespace MediaBrowser.LocalMetadata.Images
{
/// <summary>
/// Local image provider.
/// </summary>
2020-03-09 08:10:02 -07:00
public class LocalImageProvider : ILocalImageProvider, IHasOrder
{
private static readonly string[] _commonImageFileNames =
{
"poster",
"folder",
"cover",
"default"
};
private static readonly string[] _musicImageFileNames =
{
"folder",
"poster",
"cover",
"default"
};
private static readonly string[] _personImageFileNames =
{
"folder",
"poster"
};
private static readonly string[] _seriesImageFileNames =
{
"poster",
"folder",
"cover",
"default",
"show"
};
private static readonly string[] _videoImageFileNames =
{
"poster",
"folder",
"cover",
"default",
"movie"
};
2014-07-26 10:30:15 -07:00
private readonly IFileSystem _fileSystem;
/// <summary>
/// Initializes a new instance of the <see cref="LocalImageProvider"/> class.
/// </summary>
/// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
2014-07-26 10:30:15 -07:00
public LocalImageProvider(IFileSystem fileSystem)
{
_fileSystem = fileSystem;
}
2014-10-19 20:04:45 -07:00
/// <inheritdoc />
public string Name => "Local Images";
/// <inheritdoc />
public int Order => 0;
/// <inheritdoc />
2018-09-12 10:26:21 -07:00
public bool Supports(BaseItem item)
{
2014-02-10 13:11:46 -07:00
if (item.SupportsLocalMetadata)
{
// Episode has its own provider
2018-09-12 10:26:21 -07:00
if (item is Episode || item is Audio || item is Photo)
{
return false;
}
return true;
}
2014-01-31 12:55:21 -07:00
2014-02-10 13:11:46 -07:00
if (item.LocationType == LocationType.Virtual)
{
var season = item as Season;
var series = season?.Series;
2022-12-05 07:01:13 -07:00
if (series is not null && series.IsFileProtocol)
{
return true;
}
}
return false;
}
private static IEnumerable<FileSystemMetadata> GetFiles(BaseItem item, bool includeDirectories, IDirectoryService directoryService)
{
2018-09-12 10:26:21 -07:00
if (!item.IsFileProtocol)
{
2021-03-08 21:57:38 -07:00
return Enumerable.Empty<FileSystemMetadata>();
}
2014-02-05 22:17:00 -07:00
var path = item.ContainingFolderPath;
// Exit if the cache dir does not exist, alternative solution is to create it, but that's a lot of empty dirs...
if (!Directory.Exists(path))
{
2021-03-08 21:57:38 -07:00
return Enumerable.Empty<FileSystemMetadata>();
}
return directoryService.GetFileSystemEntries(path)
2021-12-12 16:26:54 -07:00
.Where(i =>
(includeDirectories && i.IsDirectory)
2021-12-20 04:15:20 -07:00
|| BaseItem.SupportedImageExtensions.Contains(i.Extension, StringComparison.OrdinalIgnoreCase))
2018-12-27 14:43:48 -07:00
.OrderBy(i => Array.IndexOf(BaseItem.SupportedImageExtensions, i.Extension ?? string.Empty));
}
/// <inheritdoc />
2021-03-08 21:57:38 -07:00
public IEnumerable<LocalImageInfo> GetImages(BaseItem item, IDirectoryService directoryService)
{
2014-02-08 15:38:02 -07:00
var files = GetFiles(item, true, directoryService).ToList();
var list = new List<LocalImageInfo>();
2014-02-08 15:38:02 -07:00
PopulateImages(item, list, files, true, directoryService);
2014-02-08 13:02:35 -07:00
return list;
}
/// <summary>
/// Get images for item.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="path">The images path.</param>
/// <param name="directoryService">Instance of the <see cref="IDirectoryService"/> interface.</param>
/// <returns>The local image info.</returns>
2021-03-08 21:57:38 -07:00
public IEnumerable<LocalImageInfo> GetImages(BaseItem item, string path, IDirectoryService directoryService)
2014-02-08 13:02:35 -07:00
{
return GetImages(item, new[] { path }, directoryService);
2014-02-08 13:02:35 -07:00
}
/// <summary>
/// Get images for item from multiple paths.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="paths">The image paths.</param>
/// <param name="directoryService">Instance of the <see cref="IDirectoryService"/> interface.</param>
/// <returns>The local image info.</returns>
2021-03-08 21:57:38 -07:00
public IEnumerable<LocalImageInfo> GetImages(BaseItem item, IEnumerable<string> paths, IDirectoryService directoryService)
2014-02-08 13:02:35 -07:00
{
IEnumerable<FileSystemMetadata> files = paths.SelectMany(i => _fileSystem.GetFiles(i, BaseItem.SupportedImageExtensions, true, false));
2017-03-28 23:26:48 -07:00
files = files
2018-12-27 14:43:48 -07:00
.OrderBy(i => Array.IndexOf(BaseItem.SupportedImageExtensions, i.Extension ?? string.Empty));
2014-02-08 13:02:35 -07:00
var list = new List<LocalImageInfo>();
2017-03-28 23:26:48 -07:00
PopulateImages(item, list, files.ToList(), false, directoryService);
return list;
}
2018-09-12 10:26:21 -07:00
private void PopulateImages(BaseItem item, List<LocalImageInfo> images, List<FileSystemMetadata> files, bool supportParentSeriesFiles, IDirectoryService directoryService)
{
2016-01-21 19:59:07 -07:00
if (supportParentSeriesFiles)
{
2021-03-08 21:57:38 -07:00
if (item is Season season)
2016-01-21 19:59:07 -07:00
{
PopulateSeasonImagesFromSeriesFolder(season, images, directoryService);
}
}
2017-03-28 23:26:48 -07:00
2014-12-08 21:57:18 -07:00
var imagePrefix = item.FileNameWithoutExtension + "-";
2017-07-30 22:16:22 -07:00
var isInMixedFolder = item.IsInMixedFolder;
2014-12-08 21:57:18 -07:00
PopulatePrimaryImages(item, images, files, imagePrefix, isInMixedFolder);
2017-11-15 14:33:04 -07:00
var added = false;
var isEpisode = item is Episode;
var isSong = item.GetType() == typeof(Audio);
var isPerson = item is Person;
// Logo
if (!isEpisode && !isSong && !isPerson)
{
added = AddImage(files, images, "logo", imagePrefix, isInMixedFolder, ImageType.Logo);
if (!added)
{
AddImage(files, images, "clearlogo", imagePrefix, isInMixedFolder, ImageType.Logo);
2017-11-15 14:33:04 -07:00
}
}
// Art
if (!isEpisode && !isSong && !isPerson)
{
AddImage(files, images, "clearart", imagePrefix, isInMixedFolder, ImageType.Art);
}
2016-01-21 19:29:00 -07:00
// For music albums, prefer cdart before disc
if (item is MusicAlbum)
{
2017-11-15 14:33:04 -07:00
added = AddImage(files, images, "cdart", imagePrefix, isInMixedFolder, ImageType.Disc);
if (!added)
{
AddImage(files, images, "disc", imagePrefix, isInMixedFolder, ImageType.Disc);
2017-11-15 14:33:04 -07:00
}
2016-01-21 19:29:00 -07:00
}
else if (item is Video || item is BoxSet)
2016-01-21 19:29:00 -07:00
{
2017-11-15 14:33:04 -07:00
added = AddImage(files, images, "disc", imagePrefix, isInMixedFolder, ImageType.Disc);
if (!added)
{
added = AddImage(files, images, "cdart", imagePrefix, isInMixedFolder, ImageType.Disc);
}
if (!added)
{
AddImage(files, images, "discart", imagePrefix, isInMixedFolder, ImageType.Disc);
2017-11-15 14:33:04 -07:00
}
}
// Banner
2017-11-15 14:33:04 -07:00
if (!isEpisode && !isSong && !isPerson)
{
AddImage(files, images, "banner", imagePrefix, isInMixedFolder, ImageType.Banner);
}
// Thumb
2017-11-15 14:33:04 -07:00
if (!isEpisode && !isSong && !isPerson)
{
added = AddImage(files, images, "landscape", imagePrefix, isInMixedFolder, ImageType.Thumb);
if (!added)
{
AddImage(files, images, "thumb", imagePrefix, isInMixedFolder, ImageType.Thumb);
2017-11-15 14:33:04 -07:00
}
}
if (!isEpisode && !isSong && !isPerson)
{
PopulateBackdrops(item, images, files, imagePrefix, isInMixedFolder);
2017-11-15 14:33:04 -07:00
}
}
2018-09-12 10:26:21 -07:00
private void PopulatePrimaryImages(BaseItem item, List<LocalImageInfo> images, List<FileSystemMetadata> files, string imagePrefix, bool isInMixedFolder)
{
2017-08-19 12:43:35 -07:00
string[] imageFileNames;
2017-08-19 12:43:35 -07:00
if (item is MusicAlbum || item is MusicArtist || item is PhotoAlbum)
2016-01-21 19:59:07 -07:00
{
// these prefer folder
imageFileNames = _musicImageFileNames;
2016-01-21 19:59:07 -07:00
}
2017-08-19 12:43:35 -07:00
else if (item is Person)
2016-01-21 19:59:07 -07:00
{
2017-08-19 12:43:35 -07:00
// these prefer folder
imageFileNames = _personImageFileNames;
2016-01-21 19:59:07 -07:00
}
2017-08-19 12:43:35 -07:00
else if (item is Series)
{
imageFileNames = _seriesImageFileNames;
}
2021-08-28 15:32:50 -07:00
else if (item is Video && item is not Episode)
2017-08-19 12:43:35 -07:00
{
imageFileNames = _videoImageFileNames;
2017-08-19 12:43:35 -07:00
}
else
{
imageFileNames = _commonImageFileNames;
}
2014-12-08 21:57:18 -07:00
var fileNameWithoutExtension = item.FileNameWithoutExtension;
if (!string.IsNullOrEmpty(fileNameWithoutExtension))
{
2017-11-15 14:33:04 -07:00
if (AddImage(files, images, fileNameWithoutExtension, ImageType.Primary))
{
return;
}
2014-12-08 21:57:18 -07:00
}
2017-08-19 12:43:35 -07:00
foreach (var name in imageFileNames)
2015-03-26 21:17:04 -07:00
{
2022-01-28 04:21:40 -07:00
if (AddImage(files, images, name, ImageType.Primary, imagePrefix))
2017-11-15 14:33:04 -07:00
{
return;
}
2015-03-26 21:17:04 -07:00
}
2014-12-08 21:57:18 -07:00
if (!isInMixedFolder)
{
2017-08-19 12:43:35 -07:00
foreach (var name in imageFileNames)
{
2017-11-15 14:33:04 -07:00
if (AddImage(files, images, name, ImageType.Primary))
{
return;
}
}
}
}
private void PopulateBackdrops(BaseItem item, List<LocalImageInfo> images, List<FileSystemMetadata> files, string imagePrefix, bool isInMixedFolder)
{
2014-02-27 09:25:04 -07:00
if (!string.IsNullOrEmpty(item.Path))
{
2014-12-08 21:57:18 -07:00
var name = item.FileNameWithoutExtension;
if (!string.IsNullOrEmpty(name))
{
2022-01-28 04:21:40 -07:00
AddImage(files, images, name + "-fanart", ImageType.Backdrop, imagePrefix);
2014-12-08 21:57:18 -07:00
// Support without the prefix if it's in it's own folder
if (!isInMixedFolder)
{
AddImage(files, images, name + "-fanart", ImageType.Backdrop);
}
}
}
2014-12-08 21:57:18 -07:00
PopulateBackdrops(images, files, imagePrefix, "fanart", "fanart-", isInMixedFolder, ImageType.Backdrop);
PopulateBackdrops(images, files, imagePrefix, "background", "background-", isInMixedFolder, ImageType.Backdrop);
PopulateBackdrops(images, files, imagePrefix, "art", "art-", isInMixedFolder, ImageType.Backdrop);
var extraFanartFolder = files
.FirstOrDefault(i => string.Equals(i.Name, "extrafanart", StringComparison.OrdinalIgnoreCase));
2022-12-05 07:01:13 -07:00
if (extraFanartFolder is not null)
{
PopulateBackdropsFromExtraFanart(extraFanartFolder.FullName, images);
}
2016-01-21 19:59:07 -07:00
PopulateBackdrops(images, files, imagePrefix, "backdrop", "backdrop", isInMixedFolder, ImageType.Backdrop);
}
private void PopulateBackdropsFromExtraFanart(string path, List<LocalImageInfo> images)
{
2017-03-28 23:26:48 -07:00
var imageFiles = _fileSystem.GetFiles(path, BaseItem.SupportedImageExtensions, false, false);
2017-03-29 12:16:18 -07:00
images.AddRange(imageFiles.Where(i => i.Length > 0).Select(i => new LocalImageInfo
{
FileInfo = i,
Type = ImageType.Backdrop
}));
}
2015-10-03 20:38:46 -07:00
private void PopulateBackdrops(List<LocalImageInfo> images, List<FileSystemMetadata> files, string imagePrefix, string firstFileName, string subsequentFileNamePrefix, bool isInMixedFolder, ImageType type)
{
AddImage(files, images, imagePrefix + firstFileName, type);
var unfound = 0;
for (var i = 1; i <= 20; i++)
{
// Screenshot Image
var found = AddImage(files, images, imagePrefix + subsequentFileNamePrefix + i, type);
if (!found)
{
unfound++;
if (unfound >= 3)
{
break;
}
}
}
2014-12-08 21:57:18 -07:00
// Support without the prefix
if (!isInMixedFolder)
{
AddImage(files, images, firstFileName, type);
unfound = 0;
for (var i = 1; i <= 20; i++)
{
// Screenshot Image
var found = AddImage(files, images, subsequentFileNamePrefix + i, type);
if (!found)
{
unfound++;
if (unfound >= 3)
{
break;
}
}
}
}
}
2014-02-10 11:39:41 -07:00
private void PopulateSeasonImagesFromSeriesFolder(Season season, List<LocalImageInfo> images, IDirectoryService directoryService)
{
var seasonNumber = season.IndexNumber;
var series = season.Series;
2018-09-12 10:26:21 -07:00
if (!seasonNumber.HasValue || !series.IsFileProtocol)
{
return;
}
2014-02-08 15:38:02 -07:00
var seriesFiles = GetFiles(series, false, directoryService).ToList();
// Try using the season name
var prefix = season.Name.Replace(" ", string.Empty, StringComparison.Ordinal).ToLowerInvariant();
var filenamePrefixes = new List<string> { prefix };
var seasonMarker = seasonNumber.Value == 0
? "-specials"
2021-09-26 07:14:36 -07:00
: seasonNumber.Value.ToString("00", CultureInfo.InvariantCulture);
// Get this one directly from the file system since we have to go up a level
if (!string.Equals(prefix, seasonMarker, StringComparison.OrdinalIgnoreCase))
{
filenamePrefixes.Add("season" + seasonMarker);
}
foreach (var filename in filenamePrefixes)
{
AddImage(seriesFiles, images, filename + "-poster", ImageType.Primary);
AddImage(seriesFiles, images, filename + "-fanart", ImageType.Backdrop);
AddImage(seriesFiles, images, filename + "-banner", ImageType.Banner);
AddImage(seriesFiles, images, filename + "-landscape", ImageType.Thumb);
}
}
2015-10-03 20:38:46 -07:00
private bool AddImage(List<FileSystemMetadata> files, List<LocalImageInfo> images, string name, string imagePrefix, bool isInMixedFolder, ImageType type)
2014-12-08 21:57:18 -07:00
{
2022-01-28 04:21:40 -07:00
var added = AddImage(files, images, name, type, imagePrefix);
2014-12-08 21:57:18 -07:00
if (!isInMixedFolder)
{
if (AddImage(files, images, name, type))
{
added = true;
}
}
return added;
}
2022-01-28 04:21:40 -07:00
private static bool AddImage(IReadOnlyList<FileSystemMetadata> files, List<LocalImageInfo> images, string name, ImageType type, string? prefix = null)
{
2022-01-28 04:21:40 -07:00
var image = GetImage(files, name, prefix);
2022-12-05 07:00:20 -07:00
if (image is null)
{
2022-01-28 04:21:40 -07:00
return false;
}
2022-01-28 04:21:40 -07:00
images.Add(new LocalImageInfo
{
FileInfo = image,
Type = type
});
return true;
}
2022-01-28 04:21:40 -07:00
private static FileSystemMetadata? GetImage(IReadOnlyList<FileSystemMetadata> files, string name, string? prefix = null)
{
2022-01-28 04:21:40 -07:00
var fileNameLength = name.Length + (prefix?.Length ?? 0);
2021-05-16 05:49:11 -07:00
for (var i = 0; i < files.Count; i++)
{
var file = files[i];
2022-01-28 04:21:40 -07:00
if (file.IsDirectory || file.Length <= 0)
{
continue;
}
var fileName = Path.GetFileNameWithoutExtension(file.FullName.AsSpan());
if (fileName.Length == fileNameLength
&& fileName.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)
&& fileName.EndsWith(name, StringComparison.OrdinalIgnoreCase))
2021-05-16 05:49:11 -07:00
{
return file;
}
}
return null;
}
}
}