2021-05-20 15:28:18 -04:00
|
|
|
#nullable disable
|
|
|
|
|
2019-11-01 13:38:54 -04:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 14:54:44 -05:00
|
|
|
using System;
|
2019-02-06 15:31:41 -05:00
|
|
|
using System.Collections.Generic;
|
2014-08-29 00:06:30 -04:00
|
|
|
using System.IO;
|
2014-02-13 00:11:54 -05:00
|
|
|
using System.Linq;
|
2021-11-15 09:56:02 -05:00
|
|
|
using Emby.Naming.Common;
|
|
|
|
using Emby.Naming.Video;
|
2021-12-20 07:31:07 -05:00
|
|
|
using Jellyfin.Extensions;
|
2019-01-13 14:21:32 -05:00
|
|
|
using MediaBrowser.Controller.Drawing;
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Model.Entities;
|
2014-02-13 00:11:54 -05:00
|
|
|
|
2016-11-03 02:37:52 -04:00
|
|
|
namespace Emby.Server.Implementations.Library.Resolvers
|
2014-02-13 00:11:54 -05:00
|
|
|
{
|
|
|
|
public class PhotoResolver : ItemResolver<Photo>
|
|
|
|
{
|
2015-04-08 10:38:02 -04:00
|
|
|
private readonly IImageProcessor _imageProcessor;
|
2021-11-15 09:56:02 -05:00
|
|
|
private readonly NamingOptions _namingOptions;
|
|
|
|
|
2019-05-11 05:55:41 -04:00
|
|
|
private static readonly HashSet<string> _ignoreFiles = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
|
|
|
|
{
|
|
|
|
"folder",
|
|
|
|
"thumb",
|
|
|
|
"landscape",
|
|
|
|
"fanart",
|
|
|
|
"backdrop",
|
|
|
|
"poster",
|
|
|
|
"cover",
|
|
|
|
"logo",
|
|
|
|
"default"
|
|
|
|
};
|
2016-07-11 00:57:22 -04:00
|
|
|
|
2021-11-15 09:56:02 -05:00
|
|
|
public PhotoResolver(IImageProcessor imageProcessor, NamingOptions namingOptions)
|
2015-04-08 10:38:02 -04:00
|
|
|
{
|
|
|
|
_imageProcessor = imageProcessor;
|
2021-11-15 09:56:02 -05:00
|
|
|
_namingOptions = namingOptions;
|
2015-04-08 10:38:02 -04:00
|
|
|
}
|
|
|
|
|
2014-02-13 00:11:54 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Resolves the specified args.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="args">The args.</param>
|
|
|
|
/// <returns>Trailer.</returns>
|
|
|
|
protected override Photo Resolve(ItemResolveArgs args)
|
|
|
|
{
|
2016-07-11 00:57:22 -04:00
|
|
|
if (!args.IsDirectory)
|
2014-02-13 00:11:54 -05:00
|
|
|
{
|
2016-07-11 00:57:22 -04:00
|
|
|
// Must be an image file within a photo collection
|
2019-05-11 05:55:41 -04:00
|
|
|
var collectionType = args.CollectionType;
|
2016-07-11 00:57:22 -04:00
|
|
|
|
2019-05-11 05:55:41 -04:00
|
|
|
if (string.Equals(collectionType, CollectionType.Photos, StringComparison.OrdinalIgnoreCase)
|
2021-05-05 09:30:32 -04:00
|
|
|
|| (string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase) && args.LibraryOptions.EnablePhotos))
|
2014-02-13 00:11:54 -05:00
|
|
|
{
|
2016-07-11 00:57:22 -04:00
|
|
|
if (IsImageFile(args.Path, _imageProcessor))
|
|
|
|
{
|
|
|
|
var filename = Path.GetFileNameWithoutExtension(args.Path);
|
|
|
|
|
|
|
|
// Make sure the image doesn't belong to a video file
|
2019-01-26 15:47:11 -05:00
|
|
|
var files = args.DirectoryService.GetFiles(Path.GetDirectoryName(args.Path));
|
2017-10-21 12:39:52 -04:00
|
|
|
|
|
|
|
foreach (var file in files)
|
2016-07-11 00:57:22 -04:00
|
|
|
{
|
2021-11-15 09:56:02 -05:00
|
|
|
if (IsOwnedByMedia(_namingOptions, file.FullName, filename))
|
2017-10-21 12:39:52 -04:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2016-07-11 00:57:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return new Photo
|
|
|
|
{
|
|
|
|
Path = args.Path
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2014-02-13 00:11:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-11-15 09:56:02 -05:00
|
|
|
internal static bool IsOwnedByMedia(NamingOptions namingOptions, string file, string imageFilename)
|
2016-07-11 00:57:22 -04:00
|
|
|
{
|
2021-11-15 09:56:02 -05:00
|
|
|
return VideoResolver.IsVideoFile(file, namingOptions) && IsOwnedByResolvedMedia(file, imageFilename);
|
2017-10-21 12:39:52 -04:00
|
|
|
}
|
|
|
|
|
2021-11-15 09:56:02 -05:00
|
|
|
internal static bool IsOwnedByResolvedMedia(string file, string imageFilename)
|
2019-05-11 05:55:41 -04:00
|
|
|
=> imageFilename.StartsWith(Path.GetFileNameWithoutExtension(file), StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
internal static bool IsImageFile(string path, IImageProcessor imageProcessor)
|
2017-10-21 12:39:52 -04:00
|
|
|
{
|
2022-10-06 14:21:23 -04:00
|
|
|
ArgumentNullException.ThrowIfNull(path);
|
2016-07-11 00:57:22 -04:00
|
|
|
|
2019-05-11 05:55:41 -04:00
|
|
|
var filename = Path.GetFileNameWithoutExtension(path);
|
2014-08-29 00:06:30 -04:00
|
|
|
|
2019-05-11 05:55:41 -04:00
|
|
|
if (_ignoreFiles.Contains(filename))
|
2016-01-27 12:34:23 -05:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-11 05:55:41 -04:00
|
|
|
if (_ignoreFiles.Any(i => filename.IndexOf(i, StringComparison.OrdinalIgnoreCase) != -1))
|
2016-01-27 12:34:23 -05:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-11 05:55:41 -04:00
|
|
|
string extension = Path.GetExtension(path).TrimStart('.');
|
2021-12-20 07:31:07 -05:00
|
|
|
return imageProcessor.SupportedInputFormats.Contains(extension, StringComparison.OrdinalIgnoreCase);
|
2014-02-13 00:11:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|