PhotoResolver.cs 3.4 KB

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