XmlSaverHelpers.cs 22 KB

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