PhotoResolver.cs 3.9 KB

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