PersonXmlSaver.cs 2.9 KB

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