PhotoResolver.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using Emby.Naming.Common;
  5. using Emby.Naming.Video;
  6. using Jellyfin.Data.Enums;
  7. using Jellyfin.Extensions;
  8. using MediaBrowser.Controller.Drawing;
  9. using MediaBrowser.Controller.Entities;
  10. using MediaBrowser.Controller.Library;
  11. using MediaBrowser.Controller.Providers;
  12. using MediaBrowser.Controller.Resolvers;
  13. using MediaBrowser.Model.Entities;
  14. namespace Emby.Server.Implementations.Library.Resolvers
  15. {
  16. /// <summary>
  17. /// Class PhotoResolver.
  18. /// </summary>
  19. public class PhotoResolver : ItemResolver<Photo>
  20. {
  21. private readonly IImageProcessor _imageProcessor;
  22. private readonly NamingOptions _namingOptions;
  23. private readonly IDirectoryService _directoryService;
  24. private static readonly string[] _ignoreFiles = new[]
  25. {
  26. "folder",
  27. "thumb",
  28. "landscape",
  29. "fanart",
  30. "backdrop",
  31. "poster",
  32. "cover",
  33. "logo",
  34. "default"
  35. };
  36. /// <summary>
  37. /// Initializes a new instance of the <see cref="PhotoResolver"/> class.
  38. /// </summary>
  39. /// <param name="imageProcessor">The image processor.</param>
  40. /// <param name="namingOptions">The naming options.</param>
  41. /// <param name="directoryService">The directory service.</param>
  42. public PhotoResolver(IImageProcessor imageProcessor, NamingOptions namingOptions, IDirectoryService directoryService)
  43. {
  44. _imageProcessor = imageProcessor;
  45. _namingOptions = namingOptions;
  46. _directoryService = directoryService;
  47. }
  48. /// <summary>
  49. /// Resolves the specified args.
  50. /// </summary>
  51. /// <param name="args">The args.</param>
  52. /// <returns>Trailer.</returns>
  53. protected override Photo? Resolve(ItemResolveArgs args)
  54. {
  55. if (!args.IsDirectory)
  56. {
  57. // Must be an image file within a photo collection
  58. var collectionType = args.CollectionType;
  59. if (collectionType == CollectionType.photos
  60. || (collectionType == CollectionType.homevideos && args.LibraryOptions.EnablePhotos))
  61. {
  62. if (IsImageFile(args.Path, _imageProcessor))
  63. {
  64. var filename = Path.GetFileNameWithoutExtension(args.Path.AsSpan());
  65. // Make sure the image doesn't belong to a video file
  66. var files = _directoryService.GetFiles(Path.GetDirectoryName(args.Path)
  67. ?? throw new InvalidOperationException("Path can't be a root directory."));
  68. foreach (var file in files)
  69. {
  70. if (IsOwnedByMedia(_namingOptions, file.FullName, filename))
  71. {
  72. return null;
  73. }
  74. }
  75. return new Photo
  76. {
  77. Path = args.Path
  78. };
  79. }
  80. }
  81. }
  82. return null;
  83. }
  84. internal static bool IsOwnedByMedia(NamingOptions namingOptions, string file, ReadOnlySpan<char> imageFilename)
  85. {
  86. return VideoResolver.IsVideoFile(file, namingOptions) && IsOwnedByResolvedMedia(file, imageFilename);
  87. }
  88. internal static bool IsOwnedByResolvedMedia(ReadOnlySpan<char> file, ReadOnlySpan<char> imageFilename)
  89. => imageFilename.StartsWith(Path.GetFileNameWithoutExtension(file), StringComparison.OrdinalIgnoreCase);
  90. internal static bool IsImageFile(string path, IImageProcessor imageProcessor)
  91. {
  92. ArgumentNullException.ThrowIfNull(path);
  93. var extension = Path.GetExtension(path.AsSpan()).TrimStart('.');
  94. if (!imageProcessor.SupportedInputFormats.Contains(extension, StringComparison.OrdinalIgnoreCase))
  95. {
  96. return false;
  97. }
  98. var filename = Path.GetFileNameWithoutExtension(path);
  99. if (_ignoreFiles.Any(i => filename.StartsWith(i, StringComparison.OrdinalIgnoreCase)))
  100. {
  101. return false;
  102. }
  103. return true;
  104. }
  105. }
  106. }