PersonXmlSaver.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using MediaBrowser.Controller.Configuration;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Library;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Security;
  7. using System.Text;
  8. using System.Threading;
  9. using MediaBrowser.Common.IO;
  10. using MediaBrowser.Controller.IO;
  11. using MediaBrowser.Model.IO;
  12. namespace MediaBrowser.LocalMetadata.Savers
  13. {
  14. /// <summary>
  15. /// Class PersonXmlSaver
  16. /// </summary>
  17. public class PersonXmlSaver : IMetadataFileSaver
  18. {
  19. public string Name
  20. {
  21. get
  22. {
  23. return XmlProviderUtils.Name;
  24. }
  25. }
  26. private readonly IServerConfigurationManager _config;
  27. private readonly ILibraryManager _libraryManager;
  28. private readonly IFileSystem _fileSystem;
  29. public PersonXmlSaver(IServerConfigurationManager config, ILibraryManager libraryManager, IFileSystem fileSystem)
  30. {
  31. _config = config;
  32. _libraryManager = libraryManager;
  33. _fileSystem = fileSystem;
  34. }
  35. /// <summary>
  36. /// Determines whether [is enabled for] [the specified item].
  37. /// </summary>
  38. /// <param name="item">The item.</param>
  39. /// <param name="updateType">Type of the update.</param>
  40. /// <returns><c>true</c> if [is enabled for] [the specified item]; otherwise, <c>false</c>.</returns>
  41. public bool IsEnabledFor(IHasMetadata item, ItemUpdateType updateType)
  42. {
  43. if (!item.SupportsLocalMetadata)
  44. {
  45. return false;
  46. }
  47. return item is Person && updateType >= ItemUpdateType.MetadataDownload;
  48. }
  49. /// <summary>
  50. /// Saves the specified item.
  51. /// </summary>
  52. /// <param name="item">The item.</param>
  53. /// <param name="cancellationToken">The cancellation token.</param>
  54. /// <returns>Task.</returns>
  55. public void Save(IHasMetadata item, CancellationToken cancellationToken)
  56. {
  57. var person = (Person)item;
  58. var builder = new StringBuilder();
  59. builder.Append("<Item>");
  60. XmlSaverHelpers.AddCommonNodes(person, _libraryManager, builder);
  61. if (person.ProductionLocations.Count > 0)
  62. {
  63. builder.Append("<PlaceOfBirth>" + SecurityElement.Escape(person.ProductionLocations[0]) + "</PlaceOfBirth>");
  64. }
  65. builder.Append("</Item>");
  66. var xmlFilePath = GetSavePath(item);
  67. XmlSaverHelpers.Save(builder, xmlFilePath, new List<string>
  68. {
  69. "PlaceOfBirth"
  70. }, _config, _fileSystem);
  71. }
  72. /// <summary>
  73. /// Gets the save path.
  74. /// </summary>
  75. /// <param name="item">The item.</param>
  76. /// <returns>System.String.</returns>
  77. public string GetSavePath(IHasMetadata item)
  78. {
  79. return Path.Combine(item.Path, "person.xml");
  80. }
  81. }
  82. }