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