PhotoResolver.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.Common.IO;
  9. using MediaBrowser.Model.IO;
  10. using MediaBrowser.Controller.Configuration;
  11. using MediaBrowser.Controller.IO;
  12. using MediaBrowser.Model.Configuration;
  13. namespace Emby.Server.Implementations.Library.Resolvers
  14. {
  15. public class PhotoResolver : ItemResolver<Photo>
  16. {
  17. private readonly IImageProcessor _imageProcessor;
  18. private readonly ILibraryManager _libraryManager;
  19. public PhotoResolver(IImageProcessor imageProcessor, ILibraryManager libraryManager)
  20. {
  21. _imageProcessor = imageProcessor;
  22. _libraryManager = libraryManager;
  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. if (args.DirectoryService.GetFiles(Path.GetDirectoryName(args.Path)).Any(i => IsOwnedByMedia(args.GetLibraryOptions(), i, filename)))
  43. {
  44. return null;
  45. }
  46. return new Photo
  47. {
  48. Path = args.Path
  49. };
  50. }
  51. }
  52. }
  53. return null;
  54. }
  55. private bool IsOwnedByMedia(LibraryOptions libraryOptions, FileSystemMetadata file, string imageFilename)
  56. {
  57. if (_libraryManager.IsVideoFile(file.FullName, libraryOptions) && imageFilename.StartsWith(Path.GetFileNameWithoutExtension(file.Name), StringComparison.OrdinalIgnoreCase))
  58. {
  59. return true;
  60. }
  61. return false;
  62. }
  63. private static readonly string[] IgnoreFiles =
  64. {
  65. "folder",
  66. "thumb",
  67. "landscape",
  68. "fanart",
  69. "backdrop",
  70. "poster",
  71. "cover"
  72. };
  73. internal static bool IsImageFile(string path, IImageProcessor imageProcessor)
  74. {
  75. var filename = Path.GetFileNameWithoutExtension(path) ?? string.Empty;
  76. if (IgnoreFiles.Contains(filename, StringComparer.OrdinalIgnoreCase))
  77. {
  78. return false;
  79. }
  80. if (IgnoreFiles.Any(i => filename.IndexOf(i, StringComparison.OrdinalIgnoreCase) != -1))
  81. {
  82. return false;
  83. }
  84. return imageProcessor.SupportedInputFormats.Contains((Path.GetExtension(path) ?? string.Empty).TrimStart('.'), StringComparer.OrdinalIgnoreCase);
  85. }
  86. }
  87. }