PhotoResolver.cs 3.4 KB

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