PhotoResolver.cs 3.2 KB

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