PhotoResolver.cs 3.8 KB

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