BaseItemResolver.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using MediaBrowser.Controller.Events;
  6. using MediaBrowser.Model.Entities;
  7. namespace MediaBrowser.Controller.Resolvers
  8. {
  9. public abstract class BaseItemResolver<T> : IBaseItemResolver
  10. where T : BaseItem, new ()
  11. {
  12. protected virtual T Resolve(ItemResolveEventArgs args)
  13. {
  14. return null;
  15. }
  16. protected virtual void SetItemValues(T item, ItemResolveEventArgs args)
  17. {
  18. // If the subclass didn't specify this
  19. if (string.IsNullOrEmpty(item.Path))
  20. {
  21. item.Path = args.Path;
  22. }
  23. if (args.Parent != null)
  24. {
  25. item.Parent = args.Parent;
  26. }
  27. item.Id = Kernel.GetMD5(item.Path);
  28. PopulateImages(item, args);
  29. PopulateLocalTrailers(item, args);
  30. }
  31. public BaseItem ResolvePath(ItemResolveEventArgs args)
  32. {
  33. T item = Resolve(args);
  34. if (item != null)
  35. {
  36. SetItemValues(item, args);
  37. EnsureName(item);
  38. EnsureDates(item);
  39. }
  40. return item;
  41. }
  42. private void EnsureName(T item)
  43. {
  44. // If the subclass didn't supply a name, add it here
  45. if (string.IsNullOrEmpty(item.Name))
  46. {
  47. item.Name = Path.GetFileNameWithoutExtension(item.Path);
  48. }
  49. }
  50. private void EnsureDates(T item)
  51. {
  52. // If the subclass didn't supply dates, add them here
  53. if (item.DateCreated == DateTime.MinValue)
  54. {
  55. item.DateCreated = Path.IsPathRooted(item.Path) ? File.GetCreationTime(item.Path) : DateTime.Now;
  56. }
  57. if (item.DateModified == DateTime.MinValue)
  58. {
  59. item.DateModified = Path.IsPathRooted(item.Path) ? File.GetLastWriteTime(item.Path) : DateTime.Now;
  60. }
  61. }
  62. protected virtual void PopulateImages(T item, ItemResolveEventArgs args)
  63. {
  64. List<string> backdropFiles = new List<string>();
  65. foreach (KeyValuePair<string,FileAttributes> file in args.FileSystemChildren)
  66. {
  67. if (file.Value.HasFlag(FileAttributes.Directory))
  68. {
  69. continue;
  70. }
  71. string filePath = file.Key;
  72. string ext = Path.GetExtension(filePath);
  73. if (!ext.EndsWith("png", StringComparison.OrdinalIgnoreCase) && !ext.EndsWith("jpg", StringComparison.OrdinalIgnoreCase))
  74. {
  75. continue;
  76. }
  77. string name = Path.GetFileNameWithoutExtension(filePath);
  78. if (name.Equals("folder", StringComparison.OrdinalIgnoreCase))
  79. {
  80. item.PrimaryImagePath = filePath;
  81. }
  82. else if (name.StartsWith("backdrop", StringComparison.OrdinalIgnoreCase))
  83. {
  84. backdropFiles.Add(filePath);
  85. }
  86. if (name.Equals("logo", StringComparison.OrdinalIgnoreCase))
  87. {
  88. item.LogoImagePath = filePath;
  89. }
  90. if (name.Equals("banner", StringComparison.OrdinalIgnoreCase))
  91. {
  92. item.BannerImagePath = filePath;
  93. }
  94. if (name.Equals("art", StringComparison.OrdinalIgnoreCase))
  95. {
  96. item.ArtImagePath = filePath;
  97. }
  98. if (name.Equals("thumb", StringComparison.OrdinalIgnoreCase))
  99. {
  100. item.ThumbnailImagePath = filePath;
  101. }
  102. }
  103. item.BackdropImagePaths = backdropFiles;
  104. }
  105. protected virtual void PopulateLocalTrailers(T item, ItemResolveEventArgs args)
  106. {
  107. var trailerPath = args.GetFolderByName("trailers");
  108. if (trailerPath.HasValue)
  109. {
  110. string[] allFiles = Directory.GetFileSystemEntries(trailerPath.Value.Key, "*", SearchOption.TopDirectoryOnly);
  111. item.LocalTrailers = allFiles.Select(f => Kernel.Instance.ItemController.GetItem(f)).OfType<Video>();
  112. }
  113. }
  114. }
  115. public interface IBaseItemResolver
  116. {
  117. BaseItem ResolvePath(ItemResolveEventArgs args);
  118. }
  119. }