PhotoResolver.cs 3.5 KB

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