ISupportsSpecialFeatures.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Common.Logging;
  3. using MediaBrowser.Common.Win32;
  4. using MediaBrowser.Controller.Library;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. namespace MediaBrowser.Controller.Entities
  10. {
  11. /// <summary>
  12. /// Allows some code sharing between entities that support special features
  13. /// </summary>
  14. public interface ISupportsSpecialFeatures
  15. {
  16. /// <summary>
  17. /// Gets the path.
  18. /// </summary>
  19. /// <value>The path.</value>
  20. string Path { get; }
  21. /// <summary>
  22. /// Gets the name.
  23. /// </summary>
  24. /// <value>The name.</value>
  25. string Name { get; }
  26. /// <summary>
  27. /// Gets the resolve args.
  28. /// </summary>
  29. /// <value>The resolve args.</value>
  30. ItemResolveArgs ResolveArgs { get; }
  31. /// <summary>
  32. /// Gets the special features.
  33. /// </summary>
  34. /// <value>The special features.</value>
  35. List<Video> SpecialFeatures { get; }
  36. }
  37. /// <summary>
  38. /// Class SpecialFeatures
  39. /// </summary>
  40. public static class SpecialFeatures
  41. {
  42. /// <summary>
  43. /// Loads special features from the file system
  44. /// </summary>
  45. /// <param name="entity">The entity.</param>
  46. /// <returns>List{Video}.</returns>
  47. /// <exception cref="System.ArgumentNullException"></exception>
  48. public static IEnumerable<Video> LoadSpecialFeatures(ISupportsSpecialFeatures entity)
  49. {
  50. if (entity == null)
  51. {
  52. throw new ArgumentNullException();
  53. }
  54. WIN32_FIND_DATA? folder;
  55. try
  56. {
  57. folder = entity.ResolveArgs.GetFileSystemEntryByName("specials");
  58. }
  59. catch (IOException ex)
  60. {
  61. Logger.LogException("Error getting ResolveArgs for {0}", ex, entity.Path);
  62. return new List<Video> { };
  63. }
  64. // Path doesn't exist. No biggie
  65. if (folder == null)
  66. {
  67. return new List<Video> {};
  68. }
  69. IEnumerable<WIN32_FIND_DATA> files;
  70. try
  71. {
  72. files = FileSystem.GetFiles(folder.Value.Path);
  73. }
  74. catch (IOException ex)
  75. {
  76. Logger.LogException("Error loading trailers for {0}", ex, entity.Name);
  77. return new List<Video> { };
  78. }
  79. return Kernel.Instance.LibraryManager.GetItems<Video>(files, null).Select(video =>
  80. {
  81. // Try to retrieve it from the db. If we don't find it, use the resolved version
  82. var dbItem = Kernel.Instance.ItemRepository.RetrieveItem(video.Id) as Video;
  83. if (dbItem != null)
  84. {
  85. dbItem.ResolveArgs = video.ResolveArgs;
  86. video = dbItem;
  87. }
  88. return video;
  89. });
  90. }
  91. }
  92. }