XmlSaverHelpers.cs 22 KB

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