PhotoResolver.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. var files = args.DirectoryService.GetFiles(_fileSystem.GetDirectoryName(args.Path));
  42. var libraryOptions = args.GetLibraryOptions();
  43. foreach (var file in files)
  44. {
  45. if (IsOwnedByMedia(_libraryManager, libraryOptions, file.FullName, filename))
  46. {
  47. return null;
  48. }
  49. }
  50. return new Photo
  51. {
  52. Path = args.Path
  53. };
  54. }
  55. }
  56. }
  57. return null;
  58. }
  59. internal static bool IsOwnedByMedia(ILibraryManager libraryManager, LibraryOptions libraryOptions, string file, string imageFilename)
  60. {
  61. if (libraryManager.IsVideoFile(file, libraryOptions))
  62. {
  63. return IsOwnedByResolvedMedia(libraryManager, libraryOptions, file, imageFilename);
  64. }
  65. return false;
  66. }
  67. internal static bool IsOwnedByResolvedMedia(ILibraryManager libraryManager, LibraryOptions libraryOptions, string file, string imageFilename)
  68. {
  69. if (imageFilename.StartsWith(Path.GetFileNameWithoutExtension(file), StringComparison.OrdinalIgnoreCase))
  70. {
  71. return true;
  72. }
  73. return false;
  74. }
  75. private static readonly string[] IgnoreFiles =
  76. {
  77. "folder",
  78. "thumb",
  79. "landscape",
  80. "fanart",
  81. "backdrop",
  82. "poster",
  83. "cover",
  84. "logo",
  85. "default"
  86. };
  87. internal static bool IsImageFile(string path, IImageProcessor imageProcessor)
  88. {
  89. var filename = Path.GetFileNameWithoutExtension(path) ?? string.Empty;
  90. if (IgnoreFiles.Contains(filename, StringComparer.OrdinalIgnoreCase))
  91. {
  92. return false;
  93. }
  94. if (IgnoreFiles.Any(i => filename.IndexOf(i, StringComparison.OrdinalIgnoreCase) != -1))
  95. {
  96. return false;
  97. }
  98. return imageProcessor.SupportedInputFormats.Contains((Path.GetExtension(path) ?? string.Empty).TrimStart('.'), StringComparer.OrdinalIgnoreCase);
  99. }
  100. }
  101. }