PhotoResolver.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. namespace MediaBrowser.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))
  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. if (args.DirectoryService.GetFiles(Path.GetDirectoryName(args.Path)).Any(i => IsOwnedByMedia(args.GetLibraryOptions(), i, filename)))
  40. {
  41. return null;
  42. }
  43. return new Photo
  44. {
  45. Path = args.Path
  46. };
  47. }
  48. }
  49. }
  50. return null;
  51. }
  52. private bool IsOwnedByMedia(LibraryOptions libraryOptions, FileSystemMetadata file, string imageFilename)
  53. {
  54. if (_libraryManager.IsVideoFile(file.FullName, libraryOptions) && imageFilename.StartsWith(Path.GetFileNameWithoutExtension(file.Name), StringComparison.OrdinalIgnoreCase))
  55. {
  56. return true;
  57. }
  58. return false;
  59. }
  60. private static readonly string[] IgnoreFiles =
  61. {
  62. "folder",
  63. "thumb",
  64. "landscape",
  65. "fanart",
  66. "backdrop",
  67. "poster",
  68. "cover"
  69. };
  70. internal static bool IsImageFile(string path, IImageProcessor imageProcessor)
  71. {
  72. var filename = Path.GetFileNameWithoutExtension(path) ?? string.Empty;
  73. if (IgnoreFiles.Contains(filename, StringComparer.OrdinalIgnoreCase))
  74. {
  75. return false;
  76. }
  77. if (IgnoreFiles.Any(i => filename.IndexOf("-" + i, StringComparison.OrdinalIgnoreCase) != -1))
  78. {
  79. return false;
  80. }
  81. return imageProcessor.SupportedInputFormats.Contains((Path.GetExtension(path) ?? string.Empty).TrimStart('.'), StringComparer.OrdinalIgnoreCase);
  82. }
  83. }
  84. }