BaseItemResolver.cs 4.6 KB

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