PhotoResolver.cs 4.1 KB

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