XmlSaverHelpers.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. if (item.CriticRating.HasValue)
  185. {
  186. builder.Append("<CriticRating>" + SecurityElement.Escape(item.CriticRating.Value.ToString(UsCulture)) + "</CriticRating>");
  187. }
  188. if (!string.IsNullOrEmpty(item.CriticRatingSummary))
  189. {
  190. builder.Append("<CriticRatingSummary><![CDATA[" + item.CriticRatingSummary + "]]></CriticRatingSummary>");
  191. }
  192. if (!string.IsNullOrEmpty(item.Overview))
  193. {
  194. builder.Append("<Overview><![CDATA[" + item.Overview + "]]></Overview>");
  195. }
  196. if (!string.IsNullOrEmpty(item.CustomRating))
  197. {
  198. builder.Append("<CustomRating>" + SecurityElement.Escape(item.CustomRating) + "</CustomRating>");
  199. }
  200. if (!string.IsNullOrEmpty(item.Name) && !(item is Episode))
  201. {
  202. builder.Append("<LocalTitle>" + SecurityElement.Escape(item.Name) + "</LocalTitle>");
  203. }
  204. if (!string.IsNullOrEmpty(item.ForcedSortName))
  205. {
  206. builder.Append("<SortTitle>" + SecurityElement.Escape(item.ForcedSortName) + "</SortTitle>");
  207. }
  208. if (item.PremiereDate.HasValue)
  209. {
  210. if (item is Person)
  211. {
  212. builder.Append("<BirthDate>" + SecurityElement.Escape(item.PremiereDate.Value.ToString("yyyy-MM-dd")) + "</BirthDate>");
  213. }
  214. else if (!(item is Episode))
  215. {
  216. builder.Append("<PremiereDate>" + SecurityElement.Escape(item.PremiereDate.Value.ToString("yyyy-MM-dd")) + "</PremiereDate>");
  217. }
  218. }
  219. if (item.EndDate.HasValue)
  220. {
  221. if (item is Person)
  222. {
  223. builder.Append("<DeathDate>" + SecurityElement.Escape(item.EndDate.Value.ToString("yyyy-MM-dd")) + "</DeathDate>");
  224. }
  225. else if (!(item is Episode))
  226. {
  227. builder.Append("<EndDate>" + SecurityElement.Escape(item.EndDate.Value.ToString("yyyy-MM-dd")) + "</EndDate>");
  228. }
  229. }
  230. if (item.RemoteTrailers.Count > 0)
  231. {
  232. builder.Append("<Trailer>" + SecurityElement.Escape(item.RemoteTrailers[0].Url) + "</Trailer>");
  233. }
  234. if (item.Budget.HasValue)
  235. {
  236. builder.Append("<Budget>" + SecurityElement.Escape(item.Budget.Value.ToString(UsCulture)) + "</Budget>");
  237. }
  238. if (item.Revenue.HasValue)
  239. {
  240. builder.Append("<Revenue>" + SecurityElement.Escape(item.Revenue.Value.ToString(UsCulture)) + "</Revenue>");
  241. }
  242. if (item.CommunityRating.HasValue)
  243. {
  244. builder.Append("<Rating>" + SecurityElement.Escape(item.CommunityRating.Value.ToString(UsCulture)) + "</Rating>");
  245. }
  246. if (item.VoteCount.HasValue)
  247. {
  248. builder.Append("<VoteCount>" + SecurityElement.Escape(item.VoteCount.Value.ToString(UsCulture)) + "</VoteCount>");
  249. }
  250. if (item.ProductionYear.HasValue && !(item is Person))
  251. {
  252. builder.Append("<ProductionYear>" + SecurityElement.Escape(item.ProductionYear.Value.ToString(UsCulture)) + "</ProductionYear>");
  253. }
  254. if (!string.IsNullOrEmpty(item.HomePageUrl))
  255. {
  256. builder.Append("<Website>" + SecurityElement.Escape(item.HomePageUrl) + "</Website>");
  257. }
  258. if (!string.IsNullOrEmpty(item.AspectRatio))
  259. {
  260. builder.Append("<AspectRatio>" + SecurityElement.Escape(item.AspectRatio) + "</AspectRatio>");
  261. }
  262. if (!string.IsNullOrEmpty(item.Language))
  263. {
  264. builder.Append("<Language>" + SecurityElement.Escape(item.Language) + "</Language>");
  265. }
  266. // Use original runtime here, actual file runtime later in MediaInfo
  267. var runTimeTicks = item.OriginalRunTimeTicks ?? item.RunTimeTicks;
  268. if (runTimeTicks.HasValue)
  269. {
  270. var timespan = TimeSpan.FromTicks(runTimeTicks.Value);
  271. builder.Append("<RunningTime>" + Convert.ToInt32(timespan.TotalMinutes).ToString(UsCulture) + "</RunningTime>");
  272. builder.Append("<Runtime>" + Convert.ToInt32(timespan.TotalMinutes).ToString(UsCulture) + "</Runtime>");
  273. }
  274. var imdb = item.GetProviderId(MetadataProviders.Imdb);
  275. if (!string.IsNullOrEmpty(imdb))
  276. {
  277. builder.Append("<IMDB_ID>" + SecurityElement.Escape(imdb) + "</IMDB_ID>");
  278. builder.Append("<IMDB>" + SecurityElement.Escape(imdb) + "</IMDB>");
  279. builder.Append("<IMDbId>" + SecurityElement.Escape(imdb) + "</IMDbId>");
  280. }
  281. var tmdb = item.GetProviderId(MetadataProviders.Tmdb);
  282. if (!string.IsNullOrEmpty(tmdb))
  283. {
  284. builder.Append("<TMDbId>" + SecurityElement.Escape(tmdb) + "</TMDbId>");
  285. }
  286. if (!(item is Series))
  287. {
  288. var tvdb = item.GetProviderId(MetadataProviders.Tvdb);
  289. if (!string.IsNullOrEmpty(tvdb))
  290. {
  291. builder.Append("<TvDbId>" + SecurityElement.Escape(tvdb) + "</TvDbId>");
  292. }
  293. }
  294. var tvcom = item.GetProviderId(MetadataProviders.Tvcom);
  295. if (!string.IsNullOrEmpty(tvcom))
  296. {
  297. builder.Append("<TVcomId>" + SecurityElement.Escape(tvcom) + "</TVcomId>");
  298. }
  299. var rt = item.GetProviderId(MetadataProviders.RottenTomatoes);
  300. if (!string.IsNullOrEmpty(rt))
  301. {
  302. builder.Append("<RottenTomatoesId>" + SecurityElement.Escape(rt) + "</RottenTomatoesId>");
  303. }
  304. var zap2It = item.GetProviderId(MetadataProviders.Zap2It);
  305. if (!string.IsNullOrEmpty(zap2It))
  306. {
  307. builder.Append("<Zap2ItId>" + SecurityElement.Escape(zap2It) + "</Zap2ItId>");
  308. }
  309. var mbz = item.GetProviderId(MetadataProviders.Musicbrainz);
  310. if (!string.IsNullOrEmpty(mbz))
  311. {
  312. builder.Append("<MusicbrainzId>" + SecurityElement.Escape(mbz) + "</MusicbrainzId>");
  313. }
  314. mbz = item.GetProviderId(MetadataProviders.MusicBrainzReleaseGroup);
  315. if (!string.IsNullOrEmpty(mbz))
  316. {
  317. builder.Append("<MusicBrainzReleaseGroupId>" + SecurityElement.Escape(mbz) + "</MusicBrainzReleaseGroupId>");
  318. }
  319. var gamesdb = item.GetProviderId(MetadataProviders.Gamesdb);
  320. if (!string.IsNullOrEmpty(gamesdb))
  321. {
  322. builder.Append("<GamesDbId>" + SecurityElement.Escape(gamesdb) + "</GamesDbId>");
  323. }
  324. var tmdbCollection = item.GetProviderId(MetadataProviders.TmdbCollection);
  325. if (!string.IsNullOrEmpty(tmdbCollection))
  326. {
  327. builder.Append("<TMDbCollectionId>" + SecurityElement.Escape(tmdbCollection) + "</TMDbCollectionId>");
  328. }
  329. if (item.Taglines.Count > 0)
  330. {
  331. builder.Append("<TagLine>" + SecurityElement.Escape(item.Taglines[0]) + "</TagLine>");
  332. builder.Append("<Taglines>");
  333. foreach (var tagline in item.Taglines)
  334. {
  335. builder.Append("<Tagline>" + SecurityElement.Escape(tagline) + "</Tagline>");
  336. }
  337. builder.Append("</Taglines>");
  338. }
  339. if (item.Genres.Count > 0)
  340. {
  341. builder.Append("<Genres>");
  342. foreach (var genre in item.Genres)
  343. {
  344. builder.Append("<Genre>" + SecurityElement.Escape(genre) + "</Genre>");
  345. }
  346. builder.Append("</Genres>");
  347. builder.Append("<Genre>" + SecurityElement.Escape(string.Join("|", item.Genres.ToArray())) + "</Genre>");
  348. }
  349. if (item.Studios.Count > 0)
  350. {
  351. builder.Append("<Studios>");
  352. foreach (var studio in item.Studios)
  353. {
  354. builder.Append("<Studio>" + SecurityElement.Escape(studio) + "</Studio>");
  355. }
  356. builder.Append("</Studios>");
  357. }
  358. if (item.Tags.Count > 0)
  359. {
  360. builder.Append("<Tags>");
  361. foreach (var tag in item.Tags)
  362. {
  363. builder.Append("<Tag>" + SecurityElement.Escape(tag) + "</Tag>");
  364. }
  365. builder.Append("</Tags>");
  366. }
  367. if (item.People.Count > 0)
  368. {
  369. builder.Append("<Persons>");
  370. foreach (var person in item.People)
  371. {
  372. builder.Append("<Person>");
  373. builder.Append("<Name>" + SecurityElement.Escape(person.Name) + "</Name>");
  374. builder.Append("<Type>" + SecurityElement.Escape(person.Type) + "</Type>");
  375. builder.Append("<Role>" + SecurityElement.Escape(person.Role) + "</Role>");
  376. builder.Append("</Person>");
  377. }
  378. builder.Append("</Persons>");
  379. }
  380. }
  381. public static void AddChapters(Video item, StringBuilder builder, IItemRepository repository)
  382. {
  383. var chapters = repository.GetChapters(item.Id);
  384. builder.Append("<Chapters>");
  385. foreach (var chapter in chapters)
  386. {
  387. builder.Append("<Chapter>");
  388. builder.Append("<Name>" + SecurityElement.Escape(chapter.Name) + "</Name>");
  389. var time = TimeSpan.FromTicks(chapter.StartPositionTicks);
  390. var ms = Convert.ToInt64(time.TotalMilliseconds);
  391. builder.Append("<StartPositionMs>" + SecurityElement.Escape(ms.ToString(UsCulture)) + "</StartPositionMs>");
  392. builder.Append("</Chapter>");
  393. }
  394. builder.Append("</Chapters>");
  395. }
  396. /// <summary>
  397. /// Appends the media info.
  398. /// </summary>
  399. /// <typeparam name="T"></typeparam>
  400. /// <param name="item">The item.</param>
  401. /// <param name="builder">The builder.</param>
  402. public static void AddMediaInfo<T>(T item, StringBuilder builder, IItemRepository itemRepository)
  403. where T : BaseItem, IHasMediaStreams
  404. {
  405. var video = item as Video;
  406. builder.Append("<MediaInfo>");
  407. foreach (var stream in item.MediaStreams)
  408. {
  409. builder.Append("<" + stream.Type + ">");
  410. if (!string.IsNullOrEmpty(stream.Codec))
  411. {
  412. builder.Append("<Codec>" + SecurityElement.Escape(stream.Codec) + "</Codec>");
  413. builder.Append("<FFCodec>" + SecurityElement.Escape(stream.Codec) + "</FFCodec>");
  414. }
  415. if (stream.BitRate.HasValue)
  416. {
  417. builder.Append("<BitRate>" + stream.BitRate.Value.ToString(UsCulture) + "</BitRate>");
  418. }
  419. if (stream.Width.HasValue)
  420. {
  421. builder.Append("<Width>" + stream.Width.Value.ToString(UsCulture) + "</Width>");
  422. }
  423. if (stream.Height.HasValue)
  424. {
  425. builder.Append("<Height>" + stream.Height.Value.ToString(UsCulture) + "</Height>");
  426. }
  427. if (!string.IsNullOrEmpty(stream.AspectRatio))
  428. {
  429. builder.Append("<AspectRatio>" + SecurityElement.Escape(stream.AspectRatio) + "</AspectRatio>");
  430. }
  431. var framerate = stream.AverageFrameRate ?? stream.RealFrameRate;
  432. if (framerate.HasValue)
  433. {
  434. builder.Append("<FrameRate>" + framerate.Value.ToString(UsCulture) + "</FrameRate>");
  435. }
  436. if (!string.IsNullOrEmpty(stream.Language))
  437. {
  438. builder.Append("<Language>" + SecurityElement.Escape(stream.Language) + "</Language>");
  439. }
  440. if (!string.IsNullOrEmpty(stream.ScanType))
  441. {
  442. builder.Append("<ScanType>" + SecurityElement.Escape(stream.ScanType) + "</ScanType>");
  443. }
  444. if (stream.Channels.HasValue)
  445. {
  446. builder.Append("<Channels>" + stream.Channels.Value.ToString(UsCulture) + "</Channels>");
  447. }
  448. if (stream.SampleRate.HasValue)
  449. {
  450. builder.Append("<SamplingRate>" + stream.SampleRate.Value.ToString(UsCulture) + "</SamplingRate>");
  451. }
  452. builder.Append("<Default>" + SecurityElement.Escape(stream.IsDefault.ToString()) + "</Default>");
  453. builder.Append("<Forced>" + SecurityElement.Escape(stream.IsForced.ToString()) + "</Forced>");
  454. if (stream.Type == MediaStreamType.Video)
  455. {
  456. if (item.RunTimeTicks.HasValue)
  457. {
  458. var timespan = TimeSpan.FromTicks(item.RunTimeTicks.Value);
  459. builder.Append("<Duration>" + Convert.ToInt64(timespan.TotalMinutes).ToString(UsCulture) + "</Duration>");
  460. builder.Append("<DurationSeconds>" + Convert.ToInt64(timespan.TotalSeconds).ToString(UsCulture) + "</DurationSeconds>");
  461. }
  462. if (video != null && video.Video3DFormat.HasValue)
  463. {
  464. switch (video.Video3DFormat.Value)
  465. {
  466. case Video3DFormat.FullSideBySide:
  467. builder.Append("<Format3D>FSBS</Format3D>");
  468. break;
  469. case Video3DFormat.FullTopAndBottom:
  470. builder.Append("<Format3D>FTAB</Format3D>");
  471. break;
  472. case Video3DFormat.HalfSideBySide:
  473. builder.Append("<Format3D>HSBS</Format3D>");
  474. break;
  475. case Video3DFormat.HalfTopAndBottom:
  476. builder.Append("<Format3D>HTAB</Format3D>");
  477. break;
  478. }
  479. }
  480. }
  481. builder.Append("</" + stream.Type + ">");
  482. }
  483. builder.Append("</MediaInfo>");
  484. if (video != null)
  485. {
  486. //AddChapters(video, builder, itemRepository);
  487. }
  488. }
  489. }
  490. }