XmlSaverHelpers.cs 21 KB

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