PhotoResolver.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using Emby.Naming.Common;
  8. using Emby.Naming.Video;
  9. using MediaBrowser.Controller.Drawing;
  10. using MediaBrowser.Controller.Entities;
  11. using MediaBrowser.Controller.Library;
  12. using MediaBrowser.Model.Entities;
  13. namespace Emby.Server.Implementations.Library.Resolvers
  14. {
  15. public class PhotoResolver : ItemResolver<Photo>
  16. {
  17. private readonly IImageProcessor _imageProcessor;
  18. private readonly NamingOptions _namingOptions;
  19. private static readonly HashSet<string> _ignoreFiles = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
  20. {
  21. "folder",
  22. "thumb",
  23. "landscape",
  24. "fanart",
  25. "backdrop",
  26. "poster",
  27. "cover",
  28. "logo",
  29. "default"
  30. };
  31. public PhotoResolver(IImageProcessor imageProcessor, NamingOptions namingOptions)
  32. {
  33. _imageProcessor = imageProcessor;
  34. _namingOptions = namingOptions;
  35. }
  36. /// <summary>
  37. /// Resolves the specified args.
  38. /// </summary>
  39. /// <param name="args">The args.</param>
  40. /// <returns>Trailer.</returns>
  41. protected override Photo Resolve(ItemResolveArgs args)
  42. {
  43. if (!args.IsDirectory)
  44. {
  45. // Must be an image file within a photo collection
  46. var collectionType = args.CollectionType;
  47. if (string.Equals(collectionType, CollectionType.Photos, StringComparison.OrdinalIgnoreCase)
  48. || (string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase) && args.LibraryOptions.EnablePhotos))
  49. {
  50. if (IsImageFile(args.Path, _imageProcessor))
  51. {
  52. var filename = Path.GetFileNameWithoutExtension(args.Path);
  53. // Make sure the image doesn't belong to a video file
  54. var files = args.DirectoryService.GetFiles(Path.GetDirectoryName(args.Path));
  55. foreach (var file in files)
  56. {
  57. if (IsOwnedByMedia(_namingOptions, file.FullName, filename))
  58. {
  59. return null;
  60. }
  61. }
  62. return new Photo
  63. {
  64. Path = args.Path
  65. };
  66. }
  67. }
  68. }
  69. return null;
  70. }
  71. internal static bool IsOwnedByMedia(NamingOptions namingOptions, string file, string imageFilename)
  72. {
  73. return VideoResolver.IsVideoFile(file, namingOptions) && IsOwnedByResolvedMedia(file, imageFilename);
  74. }
  75. internal static bool IsOwnedByResolvedMedia(string file, string imageFilename)
  76. => imageFilename.StartsWith(Path.GetFileNameWithoutExtension(file), StringComparison.OrdinalIgnoreCase);
  77. internal static bool IsImageFile(string path, IImageProcessor imageProcessor)
  78. {
  79. if (path == null)
  80. {
  81. throw new ArgumentNullException(nameof(path));
  82. }
  83. var filename = Path.GetFileNameWithoutExtension(path);
  84. if (_ignoreFiles.Contains(filename))
  85. {
  86. return false;
  87. }
  88. if (_ignoreFiles.Any(i => filename.IndexOf(i, StringComparison.OrdinalIgnoreCase) != -1))
  89. {
  90. return false;
  91. }
  92. string extension = Path.GetExtension(path).TrimStart('.');
  93. return imageProcessor.SupportedInputFormats.Contains(extension, StringComparer.OrdinalIgnoreCase);
  94. }
  95. }
  96. }