PhotoResolver.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using MediaBrowser.Controller.Drawing;
  7. using MediaBrowser.Controller.Entities;
  8. using MediaBrowser.Controller.Library;
  9. using MediaBrowser.Model.Configuration;
  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.GetLibraryOptions().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. var libraryOptions = args.GetLibraryOptions();
  54. foreach (var file in files)
  55. {
  56. if (IsOwnedByMedia(_libraryManager, libraryOptions, file.FullName, filename))
  57. {
  58. return null;
  59. }
  60. }
  61. return new Photo
  62. {
  63. Path = args.Path
  64. };
  65. }
  66. }
  67. }
  68. return null;
  69. }
  70. internal static bool IsOwnedByMedia(ILibraryManager libraryManager, LibraryOptions libraryOptions, string file, string imageFilename)
  71. {
  72. if (libraryManager.IsVideoFile(file, libraryOptions))
  73. {
  74. return IsOwnedByResolvedMedia(libraryManager, libraryOptions, file, imageFilename);
  75. }
  76. return false;
  77. }
  78. internal static bool IsOwnedByResolvedMedia(ILibraryManager libraryManager, LibraryOptions libraryOptions, string file, string imageFilename)
  79. => imageFilename.StartsWith(Path.GetFileNameWithoutExtension(file), StringComparison.OrdinalIgnoreCase);
  80. internal static bool IsImageFile(string path, IImageProcessor imageProcessor)
  81. {
  82. if (path == null)
  83. {
  84. throw new ArgumentNullException(nameof(path));
  85. }
  86. var filename = Path.GetFileNameWithoutExtension(path);
  87. if (_ignoreFiles.Contains(filename))
  88. {
  89. return false;
  90. }
  91. if (_ignoreFiles.Any(i => filename.IndexOf(i, StringComparison.OrdinalIgnoreCase) != -1))
  92. {
  93. return false;
  94. }
  95. string extension = Path.GetExtension(path).TrimStart('.');
  96. return imageProcessor.SupportedInputFormats.Contains(extension, StringComparer.OrdinalIgnoreCase);
  97. }
  98. }
  99. }