PhotoResolver.cs 4.4 KB

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