PhotoResolver.cs 4.1 KB

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