EpisodeLocalImageProvider.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Entities.TV;
  4. using MediaBrowser.Controller.Providers;
  5. using MediaBrowser.Model.Entities;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. namespace MediaBrowser.LocalMetadata.Images
  11. {
  12. public class EpisodeLocalLocalImageProvider : ILocalImageFileProvider
  13. {
  14. private readonly IFileSystem _fileSystem;
  15. public EpisodeLocalLocalImageProvider(IFileSystem fileSystem)
  16. {
  17. _fileSystem = fileSystem;
  18. }
  19. public string Name
  20. {
  21. get { return "Local Images"; }
  22. }
  23. public bool Supports(IHasImages item)
  24. {
  25. return item is Episode && item.SupportsLocalMetadata;
  26. }
  27. public List<LocalImageInfo> GetImages(IHasImages item, IDirectoryService directoryService)
  28. {
  29. var parentPath = Path.GetDirectoryName(item.Path);
  30. var parentPathFiles = directoryService.GetFileSystemEntries(parentPath);
  31. var nameWithoutExtension = _fileSystem.GetFileNameWithoutExtension(item.Path);
  32. var files = GetFilesFromParentFolder(nameWithoutExtension, parentPathFiles);
  33. if (files.Count > 0)
  34. {
  35. return files;
  36. }
  37. var metadataPath = Path.Combine(parentPath, "metadata");
  38. if (parentPathFiles.Any(i => string.Equals(i.FullName, metadataPath, StringComparison.OrdinalIgnoreCase)))
  39. {
  40. return GetFilesFromParentFolder(nameWithoutExtension, directoryService.GetFiles(metadataPath));
  41. }
  42. return new List<LocalImageInfo>();
  43. }
  44. private List<LocalImageInfo> GetFilesFromParentFolder(string filenameWithoutExtension, IEnumerable<FileSystemInfo> parentPathFiles)
  45. {
  46. var thumbName = filenameWithoutExtension + "-thumb";
  47. return parentPathFiles
  48. .Where(i =>
  49. {
  50. if ((i.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
  51. {
  52. return false;
  53. }
  54. if (BaseItem.SupportedImageExtensions.Contains(i.Extension, StringComparer.OrdinalIgnoreCase))
  55. {
  56. var currentNameWithoutExtension = _fileSystem.GetFileNameWithoutExtension(i);
  57. if (string.Equals(filenameWithoutExtension, currentNameWithoutExtension, StringComparison.OrdinalIgnoreCase))
  58. {
  59. return true;
  60. }
  61. if (string.Equals(thumbName, currentNameWithoutExtension, StringComparison.OrdinalIgnoreCase))
  62. {
  63. return true;
  64. }
  65. }
  66. return false;
  67. })
  68. .Select(i => new LocalImageInfo
  69. {
  70. FileInfo = (FileInfo)i,
  71. Type = ImageType.Primary
  72. })
  73. .ToList();
  74. }
  75. }
  76. }