FolderProviderFromXml.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using MediaBrowser.Controller.Configuration;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Model.Entities;
  4. using System;
  5. using System.IO;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using MediaBrowser.Model.Logging;
  9. namespace MediaBrowser.Controller.Providers
  10. {
  11. /// <summary>
  12. /// Provides metadata for Folders and all subclasses by parsing folder.xml
  13. /// </summary>
  14. public class FolderProviderFromXml : BaseMetadataProvider
  15. {
  16. public FolderProviderFromXml(ILogManager logManager, IServerConfigurationManager configurationManager) : base(logManager, configurationManager)
  17. {
  18. }
  19. /// <summary>
  20. /// Supportses the specified item.
  21. /// </summary>
  22. /// <param name="item">The item.</param>
  23. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  24. public override bool Supports(BaseItem item)
  25. {
  26. return item is Folder && item.LocationType == LocationType.FileSystem;
  27. }
  28. /// <summary>
  29. /// Gets the priority.
  30. /// </summary>
  31. /// <value>The priority.</value>
  32. public override MetadataProviderPriority Priority
  33. {
  34. get { return MetadataProviderPriority.First; }
  35. }
  36. /// <summary>
  37. /// Override this to return the date that should be compared to the last refresh date
  38. /// to determine if this provider should be re-fetched.
  39. /// </summary>
  40. /// <param name="item">The item.</param>
  41. /// <returns>DateTime.</returns>
  42. protected override DateTime CompareDate(BaseItem item)
  43. {
  44. var entry = item.MetaLocation != null ? item.ResolveArgs.GetMetaFileByPath(Path.Combine(item.MetaLocation, "folder.xml")) : null;
  45. return entry != null ? entry.LastWriteTimeUtc : DateTime.MinValue;
  46. }
  47. /// <summary>
  48. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  49. /// </summary>
  50. /// <param name="item">The item.</param>
  51. /// <param name="force">if set to <c>true</c> [force].</param>
  52. /// <param name="cancellationToken">The cancellation token.</param>
  53. /// <returns>Task{System.Boolean}.</returns>
  54. public override Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
  55. {
  56. return Fetch(item, cancellationToken);
  57. }
  58. /// <summary>
  59. /// Fetches the specified item.
  60. /// </summary>
  61. /// <param name="item">The item.</param>
  62. /// <param name="cancellationToken">The cancellation token.</param>
  63. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  64. private async Task<bool> Fetch(BaseItem item, CancellationToken cancellationToken)
  65. {
  66. cancellationToken.ThrowIfCancellationRequested();
  67. var metadataFile = item.ResolveArgs.GetMetaFileByPath(Path.Combine(item.MetaLocation, "folder.xml"));
  68. if (metadataFile != null)
  69. {
  70. var path = metadataFile.FullName;
  71. await XmlParsingResourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
  72. try
  73. {
  74. new BaseItemXmlParser<Folder>(Logger).Fetch((Folder)item, path, cancellationToken);
  75. }
  76. finally
  77. {
  78. XmlParsingResourcePool.Release();
  79. }
  80. SetLastRefreshed(item, DateTime.UtcNow);
  81. return true;
  82. }
  83. return false;
  84. }
  85. }
  86. }