XmlSaverHelpers.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Entities.TV;
  3. using MediaBrowser.Controller.Persistence;
  4. using MediaBrowser.Model.Entities;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Globalization;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Security;
  11. using System.Text;
  12. using System.Xml;
  13. namespace MediaBrowser.Providers.Savers
  14. {
  15. /// <summary>
  16. /// Class XmlHelpers
  17. /// </summary>
  18. public static class XmlSaverHelpers
  19. {
  20. private static readonly Dictionary<string, string> CommonTags = new[] {
  21. "Added",
  22. "AspectRatio",
  23. "BirthDate",
  24. "Budget",
  25. "certification",
  26. "Chapters",
  27. "ContentRating",
  28. "CustomRating",
  29. "CriticRating",
  30. "CriticRatingSummary",
  31. "DeathDate",
  32. "EndDate",
  33. "Genres",
  34. "Genre",
  35. "GamesDbId",
  36. "IMDB_ID",
  37. "IMDB",
  38. "IMDbId",
  39. "Language",
  40. "LocalTitle",
  41. "LockData",
  42. "LockedFields",
  43. "Format3D",
  44. "MPAARating",
  45. "MusicbrainzId",
  46. "MusicBrainzReleaseGroupId",
  47. "Overview",
  48. "Persons",
  49. "PremiereDate",
  50. "ProductionYear",
  51. "Rating",
  52. "Revenue",
  53. "RottenTomatoesId",
  54. "RunningTime",
  55. "Runtime",
  56. "SortTitle",
  57. "Studios",
  58. "Tags",
  59. "TagLine",
  60. "Taglines",
  61. "TMDbCollectionId",
  62. "TMDbId",
  63. "Trailer",
  64. "Trailers",
  65. "TVcomId",
  66. "TvDbId",
  67. "Type",
  68. "VoteCount",
  69. "Website",
  70. "Zap2ItId"
  71. }.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
  72. /// <summary>
  73. /// The us culture
  74. /// </summary>
  75. private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  76. /// <summary>
  77. /// Saves the specified XML.
  78. /// </summary>
  79. /// <param name="xml">The XML.</param>
  80. /// <param name="path">The path.</param>
  81. /// <param name="xmlTagsUsed">The XML tags used.</param>
  82. public static void Save(StringBuilder xml, string path, List<string> xmlTagsUsed)
  83. {
  84. if (File.Exists(path))
  85. {
  86. var position = xml.ToString().LastIndexOf("</", StringComparison.OrdinalIgnoreCase);
  87. xml.Insert(position, GetCustomTags(path, xmlTagsUsed));
  88. }
  89. var xmlDocument = new XmlDocument();
  90. xmlDocument.LoadXml(xml.ToString());
  91. //Add the new node to the document.
  92. xmlDocument.InsertBefore(xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", "yes"), xmlDocument.DocumentElement);
  93. var parentPath = Path.GetDirectoryName(path);
  94. Directory.CreateDirectory(parentPath);
  95. var wasHidden = false;
  96. var file = new FileInfo(path);
  97. // This will fail if the file is hidden
  98. if (file.Exists)
  99. {
  100. if ((file.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
  101. {
  102. file.Attributes &= ~FileAttributes.Hidden;
  103. wasHidden = true;
  104. }
  105. }
  106. using (var filestream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read))
  107. {
  108. using (var streamWriter = new StreamWriter(filestream, Encoding.UTF8))
  109. {
  110. xmlDocument.Save(streamWriter);
  111. }
  112. }
  113. if (wasHidden)
  114. {
  115. file.Refresh();
  116. // Add back the attribute
  117. file.Attributes |= FileAttributes.Hidden;
  118. }
  119. }
  120. /// <summary>
  121. /// Gets the custom tags.
  122. /// </summary>
  123. /// <param name="path">The path.</param>
  124. /// <param name="xmlTagsUsed">The XML tags used.</param>
  125. /// <returns>System.String.</returns>
  126. private static string GetCustomTags(string path, List<string> xmlTagsUsed)
  127. {
  128. var settings = new XmlReaderSettings
  129. {
  130. CheckCharacters = false,
  131. IgnoreProcessingInstructions = true,
  132. IgnoreComments = true,
  133. ValidationType = ValidationType.None
  134. };
  135. var builder = new StringBuilder();
  136. using (var streamReader = new StreamReader(path, Encoding.UTF8))
  137. {
  138. // Use XmlReader for best performance
  139. using (var reader = XmlReader.Create(streamReader, settings))
  140. {
  141. reader.MoveToContent();
  142. // Loop through each element
  143. while (reader.Read())
  144. {
  145. if (reader.NodeType == XmlNodeType.Element)
  146. {
  147. var name = reader.Name;
  148. if (!CommonTags.ContainsKey(name) && !xmlTagsUsed.Contains(name, StringComparer.OrdinalIgnoreCase))
  149. {
  150. builder.AppendLine(reader.ReadOuterXml());
  151. }
  152. else
  153. {
  154. reader.Skip();
  155. }
  156. }
  157. }
  158. }
  159. }
  160. return builder.ToString();
  161. }
  162. /// <summary>
  163. /// Adds the common nodes.
  164. /// </summary>
  165. /// <param name="item">The item.</param>
  166. /// <param name="builder">The builder.</param>
  167. public static void AddCommonNodes(BaseItem item, StringBuilder builder)
  168. {
  169. if (!string.IsNullOrEmpty(item.OfficialRating))
  170. {
  171. builder.Append("<ContentRating>" + SecurityElement.Escape(item.OfficialRating) + "</ContentRating>");
  172. builder.Append("<MPAARating>" + SecurityElement.Escape(item.OfficialRating) + "</MPAARating>");
  173. builder.Append("<certification>" + SecurityElement.Escape(item.OfficialRating) + "</certification>");
  174. }
  175. builder.Append("<Added>" + SecurityElement.Escape(item.DateCreated.ToLocalTime().ToString("G")) + "</Added>");
  176. builder.Append("<LockData>" + item.DontFetchMeta.ToString().ToLower() + "</LockData>");
  177. if (item.LockedFields.Count > 0)
  178. {
  179. builder.Append("<LockedFields>" + string.Join("|", item.LockedFields.Select(i => i.ToString()).ToArray()) + "</LockedFields>");
  180. }
  181. if (!string.IsNullOrEmpty(item.DisplayMediaType))
  182. {
  183. builder.Append("<Type>" + SecurityElement.Escape(item.DisplayMediaType) + "</Type>");
  184. }
  185. var hasCriticRating = item as IHasCriticRating;
  186. if (hasCriticRating != null)
  187. {
  188. if (hasCriticRating.CriticRating.HasValue)
  189. {
  190. builder.Append("<CriticRating>" + SecurityElement.Escape(hasCriticRating.CriticRating.Value.ToString(UsCulture)) + "</CriticRating>");
  191. }
  192. if (!string.IsNullOrEmpty(hasCriticRating.CriticRatingSummary))
  193. {
  194. builder.Append("<CriticRatingSummary><![CDATA[" + hasCriticRating.CriticRatingSummary + "]]></CriticRatingSummary>");
  195. }
  196. }
  197. if (!string.IsNullOrEmpty(item.Overview))
  198. {
  199. builder.Append("<Overview><![CDATA[" + item.Overview + "]]></Overview>");
  200. }
  201. if (!string.IsNullOrEmpty(item.CustomRating))
  202. {
  203. builder.Append("<CustomRating>" + SecurityElement.Escape(item.CustomRating) + "</CustomRating>");
  204. }
  205. if (!string.IsNullOrEmpty(item.Name) && !(item is Episode))
  206. {
  207. builder.Append("<LocalTitle>" + SecurityElement.Escape(item.Name) + "</LocalTitle>");
  208. }
  209. if (!string.IsNullOrEmpty(item.ForcedSortName))
  210. {
  211. builder.Append("<SortTitle>" + SecurityElement.Escape(item.ForcedSortName) + "</SortTitle>");
  212. }
  213. if (item.PremiereDate.HasValue)
  214. {
  215. if (item is Person)
  216. {
  217. builder.Append("<BirthDate>" + SecurityElement.Escape(item.PremiereDate.Value.ToString("yyyy-MM-dd")) + "</BirthDate>");
  218. }
  219. else if (!(item is Episode))
  220. {
  221. builder.Append("<PremiereDate>" + SecurityElement.Escape(item.PremiereDate.Value.ToString("yyyy-MM-dd")) + "</PremiereDate>");
  222. }
  223. }
  224. if (item.EndDate.HasValue)
  225. {
  226. if (item is Person)
  227. {
  228. builder.Append("<DeathDate>" + SecurityElement.Escape(item.EndDate.Value.ToString("yyyy-MM-dd")) + "</DeathDate>");
  229. }
  230. else if (!(item is Episode))
  231. {
  232. builder.Append("<EndDate>" + SecurityElement.Escape(item.EndDate.Value.ToString("yyyy-MM-dd")) + "</EndDate>");
  233. }
  234. }
  235. var hasTrailers = item as IHasTrailers;
  236. if (hasTrailers != null)
  237. {
  238. if (hasTrailers.RemoteTrailers.Count > 0)
  239. {
  240. builder.Append("<Trailers>");
  241. foreach (var trailer in hasTrailers.RemoteTrailers)
  242. {
  243. builder.Append("<Trailer>" + SecurityElement.Escape(trailer.Url) + "</Trailer>");
  244. }
  245. builder.Append("</Trailers>");
  246. }
  247. }
  248. var hasBudget = item as IHasBudget;
  249. if (hasBudget != null)
  250. {
  251. if (hasBudget.Budget.HasValue)
  252. {
  253. builder.Append("<Budget>" + SecurityElement.Escape(hasBudget.Budget.Value.ToString(UsCulture)) + "</Budget>");
  254. }
  255. if (hasBudget.Revenue.HasValue)
  256. {
  257. builder.Append("<Revenue>" + SecurityElement.Escape(hasBudget.Revenue.Value.ToString(UsCulture)) + "</Revenue>");
  258. }
  259. }
  260. if (item.CommunityRating.HasValue)
  261. {
  262. builder.Append("<Rating>" + SecurityElement.Escape(item.CommunityRating.Value.ToString(UsCulture)) + "</Rating>");
  263. }
  264. if (item.VoteCount.HasValue)
  265. {
  266. builder.Append("<VoteCount>" + SecurityElement.Escape(item.VoteCount.Value.ToString(UsCulture)) + "</VoteCount>");
  267. }
  268. if (item.ProductionYear.HasValue && !(item is Person))
  269. {
  270. builder.Append("<ProductionYear>" + SecurityElement.Escape(item.ProductionYear.Value.ToString(UsCulture)) + "</ProductionYear>");
  271. }
  272. if (!string.IsNullOrEmpty(item.HomePageUrl))
  273. {
  274. builder.Append("<Website>" + SecurityElement.Escape(item.HomePageUrl) + "</Website>");
  275. }
  276. var hasAspectRatio = item as IHasAspectRatio;
  277. if (hasAspectRatio != null)
  278. {
  279. if (!string.IsNullOrEmpty(hasAspectRatio.AspectRatio))
  280. {
  281. builder.Append("<AspectRatio>" + SecurityElement.Escape(hasAspectRatio.AspectRatio) + "</AspectRatio>");
  282. }
  283. }
  284. var hasLanguage = item as IHasPreferredMetadataLanguage;
  285. if (hasLanguage != null)
  286. {
  287. if (!string.IsNullOrEmpty(hasLanguage.PreferredMetadataLanguage))
  288. {
  289. builder.Append("<Language>" + SecurityElement.Escape(hasLanguage.PreferredMetadataLanguage) + "</Language>");
  290. }
  291. }
  292. // Use original runtime here, actual file runtime later in MediaInfo
  293. var runTimeTicks = item.OriginalRunTimeTicks ?? item.RunTimeTicks;
  294. if (runTimeTicks.HasValue)
  295. {
  296. var timespan = TimeSpan.FromTicks(runTimeTicks.Value);
  297. builder.Append("<RunningTime>" + Convert.ToInt32(timespan.TotalMinutes).ToString(UsCulture) + "</RunningTime>");
  298. builder.Append("<Runtime>" + Convert.ToInt32(timespan.TotalMinutes).ToString(UsCulture) + "</Runtime>");
  299. }
  300. var imdb = item.GetProviderId(MetadataProviders.Imdb);
  301. if (!string.IsNullOrEmpty(imdb))
  302. {
  303. builder.Append("<IMDB_ID>" + SecurityElement.Escape(imdb) + "</IMDB_ID>");
  304. builder.Append("<IMDB>" + SecurityElement.Escape(imdb) + "</IMDB>");
  305. builder.Append("<IMDbId>" + SecurityElement.Escape(imdb) + "</IMDbId>");
  306. }
  307. var tmdb = item.GetProviderId(MetadataProviders.Tmdb);
  308. if (!string.IsNullOrEmpty(tmdb))
  309. {
  310. builder.Append("<TMDbId>" + SecurityElement.Escape(tmdb) + "</TMDbId>");
  311. }
  312. if (!(item is Series))
  313. {
  314. var tvdb = item.GetProviderId(MetadataProviders.Tvdb);
  315. if (!string.IsNullOrEmpty(tvdb))
  316. {
  317. builder.Append("<TvDbId>" + SecurityElement.Escape(tvdb) + "</TvDbId>");
  318. }
  319. }
  320. var tvcom = item.GetProviderId(MetadataProviders.Tvcom);
  321. if (!string.IsNullOrEmpty(tvcom))
  322. {
  323. builder.Append("<TVcomId>" + SecurityElement.Escape(tvcom) + "</TVcomId>");
  324. }
  325. var rt = item.GetProviderId(MetadataProviders.RottenTomatoes);
  326. if (!string.IsNullOrEmpty(rt))
  327. {
  328. builder.Append("<RottenTomatoesId>" + SecurityElement.Escape(rt) + "</RottenTomatoesId>");
  329. }
  330. var zap2It = item.GetProviderId(MetadataProviders.Zap2It);
  331. if (!string.IsNullOrEmpty(zap2It))
  332. {
  333. builder.Append("<Zap2ItId>" + SecurityElement.Escape(zap2It) + "</Zap2ItId>");
  334. }
  335. var mbz = item.GetProviderId(MetadataProviders.Musicbrainz);
  336. if (!string.IsNullOrEmpty(mbz))
  337. {
  338. builder.Append("<MusicbrainzId>" + SecurityElement.Escape(mbz) + "</MusicbrainzId>");
  339. }
  340. mbz = item.GetProviderId(MetadataProviders.MusicBrainzReleaseGroup);
  341. if (!string.IsNullOrEmpty(mbz))
  342. {
  343. builder.Append("<MusicBrainzReleaseGroupId>" + SecurityElement.Escape(mbz) + "</MusicBrainzReleaseGroupId>");
  344. }
  345. var gamesdb = item.GetProviderId(MetadataProviders.Gamesdb);
  346. if (!string.IsNullOrEmpty(gamesdb))
  347. {
  348. builder.Append("<GamesDbId>" + SecurityElement.Escape(gamesdb) + "</GamesDbId>");
  349. }
  350. var tmdbCollection = item.GetProviderId(MetadataProviders.TmdbCollection);
  351. if (!string.IsNullOrEmpty(tmdbCollection))
  352. {
  353. builder.Append("<TMDbCollectionId>" + SecurityElement.Escape(tmdbCollection) + "</TMDbCollectionId>");
  354. }
  355. var hasTagline = item as IHasTaglines;
  356. if (hasTagline != null)
  357. {
  358. if (hasTagline.Taglines.Count > 0)
  359. {
  360. builder.Append("<Taglines>");
  361. foreach (var tagline in hasTagline.Taglines)
  362. {
  363. builder.Append("<Tagline>" + SecurityElement.Escape(tagline) + "</Tagline>");
  364. }
  365. builder.Append("</Taglines>");
  366. }
  367. }
  368. if (item.Genres.Count > 0)
  369. {
  370. builder.Append("<Genres>");
  371. foreach (var genre in item.Genres)
  372. {
  373. builder.Append("<Genre>" + SecurityElement.Escape(genre) + "</Genre>");
  374. }
  375. builder.Append("</Genres>");
  376. }
  377. if (item.Studios.Count > 0)
  378. {
  379. builder.Append("<Studios>");
  380. foreach (var studio in item.Studios)
  381. {
  382. builder.Append("<Studio>" + SecurityElement.Escape(studio) + "</Studio>");
  383. }
  384. builder.Append("</Studios>");
  385. }
  386. var hasTags = item as IHasTags;
  387. if (hasTags != null)
  388. {
  389. if (hasTags.Tags.Count > 0)
  390. {
  391. builder.Append("<Tags>");
  392. foreach (var tag in hasTags.Tags)
  393. {
  394. builder.Append("<Tag>" + SecurityElement.Escape(tag) + "</Tag>");
  395. }
  396. builder.Append("</Tags>");
  397. }
  398. }
  399. if (item.People.Count > 0)
  400. {
  401. builder.Append("<Persons>");
  402. foreach (var person in item.People)
  403. {
  404. builder.Append("<Person>");
  405. builder.Append("<Name>" + SecurityElement.Escape(person.Name) + "</Name>");
  406. builder.Append("<Type>" + SecurityElement.Escape(person.Type) + "</Type>");
  407. builder.Append("<Role>" + SecurityElement.Escape(person.Role) + "</Role>");
  408. if (person.SortOrder.HasValue)
  409. {
  410. builder.Append("<SortOrder>" + SecurityElement.Escape(person.SortOrder.Value.ToString(UsCulture)) + "</SortOrder>");
  411. }
  412. builder.Append("</Person>");
  413. }
  414. builder.Append("</Persons>");
  415. }
  416. }
  417. public static void AddChapters(Video item, StringBuilder builder, IItemRepository repository)
  418. {
  419. var chapters = repository.GetChapters(item.Id);
  420. builder.Append("<Chapters>");
  421. foreach (var chapter in chapters)
  422. {
  423. builder.Append("<Chapter>");
  424. builder.Append("<Name>" + SecurityElement.Escape(chapter.Name) + "</Name>");
  425. var time = TimeSpan.FromTicks(chapter.StartPositionTicks);
  426. var ms = Convert.ToInt64(time.TotalMilliseconds);
  427. builder.Append("<StartPositionMs>" + SecurityElement.Escape(ms.ToString(UsCulture)) + "</StartPositionMs>");
  428. builder.Append("</Chapter>");
  429. }
  430. builder.Append("</Chapters>");
  431. }
  432. /// <summary>
  433. /// Appends the media info.
  434. /// </summary>
  435. /// <typeparam name="T"></typeparam>
  436. public static void AddMediaInfo<T>(T item, StringBuilder builder, IItemRepository itemRepository)
  437. where T : BaseItem, IHasMediaStreams
  438. {
  439. var video = item as Video;
  440. if (video != null && video.Video3DFormat.HasValue)
  441. {
  442. switch (video.Video3DFormat.Value)
  443. {
  444. case Video3DFormat.FullSideBySide:
  445. builder.Append("<Format3D>FSBS</Format3D>");
  446. break;
  447. case Video3DFormat.FullTopAndBottom:
  448. builder.Append("<Format3D>FTAB</Format3D>");
  449. break;
  450. case Video3DFormat.HalfSideBySide:
  451. builder.Append("<Format3D>HSBS</Format3D>");
  452. break;
  453. case Video3DFormat.HalfTopAndBottom:
  454. builder.Append("<Format3D>HTAB</Format3D>");
  455. break;
  456. }
  457. }
  458. }
  459. }
  460. }