PhotoResolver.cs 4.0 KB

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