BaseItemResolver.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. /// <summary>
  17. /// Sets initial values on the newly resolved item
  18. /// </summary>
  19. protected virtual void SetItemValues(T item, ItemResolveEventArgs args)
  20. {
  21. // If the subclass didn't specify this
  22. if (string.IsNullOrEmpty(item.Path))
  23. {
  24. item.Path = args.Path;
  25. }
  26. // If the subclass didn't specify this
  27. if (args.Parent != null)
  28. {
  29. item.Parent = args.Parent;
  30. }
  31. item.Id = Kernel.GetMD5(item.Path);
  32. PopulateImages(item, args);
  33. PopulateLocalTrailers(item, args);
  34. }
  35. public BaseItem ResolvePath(ItemResolveEventArgs args)
  36. {
  37. T item = Resolve(args);
  38. if (item != null)
  39. {
  40. // Set initial values on the newly resolved item
  41. SetItemValues(item, args);
  42. // Make sure the item has a name
  43. EnsureName(item);
  44. // Make sure DateCreated and DateModified have values
  45. EnsureDates(item);
  46. }
  47. return item;
  48. }
  49. private void EnsureName(T item)
  50. {
  51. // If the subclass didn't supply a name, add it here
  52. if (string.IsNullOrEmpty(item.Name))
  53. {
  54. item.Name = Path.GetFileNameWithoutExtension(item.Path);
  55. }
  56. }
  57. /// <summary>
  58. /// Ensures DateCreated and DateModified have values
  59. /// </summary>
  60. private void EnsureDates(T item)
  61. {
  62. // If the subclass didn't supply dates, add them here
  63. if (item.DateCreated == DateTime.MinValue)
  64. {
  65. item.DateCreated = Path.IsPathRooted(item.Path) ? File.GetCreationTime(item.Path) : DateTime.Now;
  66. }
  67. if (item.DateModified == DateTime.MinValue)
  68. {
  69. item.DateModified = Path.IsPathRooted(item.Path) ? File.GetLastWriteTime(item.Path) : DateTime.Now;
  70. }
  71. }
  72. /// <summary>
  73. /// Fills in image paths based on files win the folder
  74. /// </summary>
  75. protected virtual void PopulateImages(T item, ItemResolveEventArgs args)
  76. {
  77. List<string> backdropFiles = new List<string>();
  78. foreach (KeyValuePair<string,FileAttributes> file in args.FileSystemChildren)
  79. {
  80. if (file.Value.HasFlag(FileAttributes.Directory))
  81. {
  82. continue;
  83. }
  84. string filePath = file.Key;
  85. string ext = Path.GetExtension(filePath);
  86. // Only support png and jpg files
  87. if (!ext.EndsWith("png", StringComparison.OrdinalIgnoreCase) && !ext.EndsWith("jpg", StringComparison.OrdinalIgnoreCase))
  88. {
  89. continue;
  90. }
  91. string name = Path.GetFileNameWithoutExtension(filePath);
  92. if (name.Equals("folder", StringComparison.OrdinalIgnoreCase))
  93. {
  94. item.PrimaryImagePath = filePath;
  95. }
  96. else if (name.StartsWith("backdrop", StringComparison.OrdinalIgnoreCase))
  97. {
  98. backdropFiles.Add(filePath);
  99. }
  100. if (name.Equals("logo", StringComparison.OrdinalIgnoreCase))
  101. {
  102. item.LogoImagePath = filePath;
  103. }
  104. if (name.Equals("banner", StringComparison.OrdinalIgnoreCase))
  105. {
  106. item.BannerImagePath = filePath;
  107. }
  108. if (name.Equals("art", StringComparison.OrdinalIgnoreCase))
  109. {
  110. item.ArtImagePath = filePath;
  111. }
  112. if (name.Equals("thumb", StringComparison.OrdinalIgnoreCase))
  113. {
  114. item.ThumbnailImagePath = filePath;
  115. }
  116. }
  117. item.BackdropImagePaths = backdropFiles;
  118. }
  119. protected virtual void PopulateLocalTrailers(T item, ItemResolveEventArgs args)
  120. {
  121. var trailerPath = args.GetFolderByName("trailers");
  122. if (trailerPath.HasValue)
  123. {
  124. string[] allFiles = Directory.GetFileSystemEntries(trailerPath.Value.Key, "*", SearchOption.TopDirectoryOnly);
  125. item.LocalTrailers = allFiles.Select(f => Kernel.Instance.ItemController.GetItem(f)).OfType<Video>();
  126. }
  127. }
  128. }
  129. /// <summary>
  130. /// Weed this to keep a list of resolvers, since Resolvers are built with generics
  131. /// </summary>
  132. public interface IBaseItemResolver
  133. {
  134. BaseItem ResolvePath(ItemResolveEventArgs args);
  135. }
  136. }