XmlSaverHelpers.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Entities.TV;
  3. using MediaBrowser.Model.Entities;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Globalization;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Security;
  10. using System.Text;
  11. using System.Xml;
  12. namespace MediaBrowser.Providers.Savers
  13. {
  14. /// <summary>
  15. /// Class XmlHelpers
  16. /// </summary>
  17. public static class XmlSaverHelpers
  18. {
  19. /// <summary>
  20. /// The us culture
  21. /// </summary>
  22. private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  23. /// <summary>
  24. /// Saves the specified XML.
  25. /// </summary>
  26. /// <param name="xml">The XML.</param>
  27. /// <param name="path">The path.</param>
  28. /// <param name="xmlTagsUsed">The XML tags used.</param>
  29. public static void Save(StringBuilder xml, string path, IEnumerable<string> xmlTagsUsed)
  30. {
  31. if (File.Exists(path))
  32. {
  33. var tags = xmlTagsUsed.ToList();
  34. tags.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. "RottenTomatoesId",
  65. "MusicbrainzId",
  66. "CollectionNumber",
  67. "Genres",
  68. "Genre",
  69. "Studios",
  70. "Tags",
  71. "Added",
  72. "LockData",
  73. "Trailer",
  74. "CriticRating",
  75. "CriticRatingSummary",
  76. "GamesDbId",
  77. "BirthDate",
  78. "DeathDate",
  79. "LockedFields"
  80. });
  81. var position = xml.ToString().LastIndexOf("</", StringComparison.OrdinalIgnoreCase);
  82. xml.Insert(position, GetCustomTags(path, tags));
  83. }
  84. var xmlDocument = new XmlDocument();
  85. xmlDocument.LoadXml(xml.ToString());
  86. //Add the new node to the document.
  87. xmlDocument.InsertBefore(xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", "yes"), xmlDocument.DocumentElement);
  88. var parentPath = Path.GetDirectoryName(path);
  89. if (!Directory.Exists(parentPath))
  90. {
  91. Directory.CreateDirectory(parentPath);
  92. }
  93. var wasHidden = false;
  94. var file = new FileInfo(path);
  95. // This will fail if the file is hidden
  96. if (file.Exists)
  97. {
  98. if ((file.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
  99. {
  100. file.Attributes &= ~FileAttributes.Hidden;
  101. wasHidden = true;
  102. }
  103. }
  104. using (var filestream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read))
  105. {
  106. using (var streamWriter = new StreamWriter(filestream, Encoding.UTF8))
  107. {
  108. xmlDocument.Save(streamWriter);
  109. }
  110. }
  111. if (wasHidden)
  112. {
  113. file.Refresh();
  114. // Add back the attribute
  115. file.Attributes |= FileAttributes.Hidden;
  116. }
  117. }
  118. /// <summary>
  119. /// Gets the custom tags.
  120. /// </summary>
  121. /// <param name="path">The path.</param>
  122. /// <param name="xmlTagsUsed">The XML tags used.</param>
  123. /// <returns>System.String.</returns>
  124. private static string GetCustomTags(string path, ICollection<string> xmlTagsUsed)
  125. {
  126. var doc = new XmlDocument();
  127. doc.Load(path);
  128. var nodes = doc.DocumentElement.ChildNodes.Cast<XmlNode>()
  129. .Where(i => !xmlTagsUsed.Contains(i.Name))
  130. .Select(i => i.OuterXml)
  131. .ToArray();
  132. return string.Join(Environment.NewLine, nodes);
  133. }
  134. /// <summary>
  135. /// Adds the common nodes.
  136. /// </summary>
  137. /// <param name="item">The item.</param>
  138. /// <param name="builder">The builder.</param>
  139. public static void AddCommonNodes(BaseItem item, StringBuilder builder)
  140. {
  141. if (!string.IsNullOrEmpty(item.OfficialRating))
  142. {
  143. builder.Append("<ContentRating>" + SecurityElement.Escape(item.OfficialRating) + "</ContentRating>");
  144. builder.Append("<MPAARating>" + SecurityElement.Escape(item.OfficialRating) + "</MPAARating>");
  145. builder.Append("<certification>" + SecurityElement.Escape(item.OfficialRating) + "</certification>");
  146. }
  147. builder.Append("<Added>" + SecurityElement.Escape(item.DateCreated.ToLocalTime().ToString("G")) + "</Added>");
  148. builder.Append("<LockData>" + item.DontFetchMeta.ToString().ToLower() + "</LockData>");
  149. if (item.LockedFields.Count > 0)
  150. {
  151. builder.Append("<LockedFields>" + string.Join("|", item.LockedFields.Select(i => i.ToString()).ToArray()) + "</LockedFields>");
  152. }
  153. if (!string.IsNullOrEmpty(item.DisplayMediaType))
  154. {
  155. builder.Append("<Type>" + SecurityElement.Escape(item.DisplayMediaType) + "</Type>");
  156. }
  157. if (item.CriticRating.HasValue)
  158. {
  159. builder.Append("<CriticRating>" + SecurityElement.Escape(item.CriticRating.Value.ToString(UsCulture)) + "</CriticRating>");
  160. }
  161. if (!string.IsNullOrEmpty(item.CriticRatingSummary))
  162. {
  163. builder.Append("<CriticRatingSummary><![CDATA[" + item.CriticRatingSummary + "]]></CriticRatingSummary>");
  164. }
  165. if (!string.IsNullOrEmpty(item.Overview))
  166. {
  167. builder.Append("<Overview><![CDATA[" + item.Overview + "]]></Overview>");
  168. }
  169. if (!string.IsNullOrEmpty(item.CustomRating))
  170. {
  171. builder.Append("<CustomRating>" + SecurityElement.Escape(item.CustomRating) + "</CustomRating>");
  172. }
  173. if (!string.IsNullOrEmpty(item.Name) && !(item is Episode))
  174. {
  175. builder.Append("<LocalTitle>" + SecurityElement.Escape(item.Name) + "</LocalTitle>");
  176. }
  177. if (!string.IsNullOrEmpty(item.ForcedSortName))
  178. {
  179. builder.Append("<SortTitle>" + SecurityElement.Escape(item.ForcedSortName) + "</SortTitle>");
  180. }
  181. if (item.PremiereDate.HasValue)
  182. {
  183. if (item is Person)
  184. {
  185. builder.Append("<BirthDate>" + SecurityElement.Escape(item.PremiereDate.Value.ToString("yyyy-MM-dd")) + "</BirthDate>");
  186. }
  187. else if (!(item is Episode))
  188. {
  189. builder.Append("<PremiereDate>" + SecurityElement.Escape(item.PremiereDate.Value.ToString("yyyy-MM-dd")) + "</PremiereDate>");
  190. }
  191. }
  192. if (item.EndDate.HasValue)
  193. {
  194. if (item is Person)
  195. {
  196. builder.Append("<DeathDate>" + SecurityElement.Escape(item.EndDate.Value.ToString("yyyy-MM-dd")) + "</DeathDate>");
  197. }
  198. else if (!(item is Episode))
  199. {
  200. builder.Append("<EndDate>" + SecurityElement.Escape(item.EndDate.Value.ToString("yyyy-MM-dd")) + "</EndDate>");
  201. }
  202. }
  203. if (item.RemoteTrailers.Count > 0)
  204. {
  205. builder.Append("<Trailer>" + SecurityElement.Escape(item.RemoteTrailers[0].Url) + "</Trailer>");
  206. }
  207. if (item.Budget.HasValue)
  208. {
  209. builder.Append("<Budget>" + SecurityElement.Escape(item.Budget.Value.ToString(UsCulture)) + "</Budget>");
  210. }
  211. if (item.Revenue.HasValue)
  212. {
  213. builder.Append("<Revenue>" + SecurityElement.Escape(item.Revenue.Value.ToString(UsCulture)) + "</Revenue>");
  214. }
  215. if (item.CommunityRating.HasValue)
  216. {
  217. builder.Append("<Rating>" + SecurityElement.Escape(item.CommunityRating.Value.ToString(UsCulture)) + "</Rating>");
  218. }
  219. if (item.ProductionYear.HasValue && !(item is Person))
  220. {
  221. builder.Append("<ProductionYear>" + SecurityElement.Escape(item.ProductionYear.Value.ToString(UsCulture)) + "</ProductionYear>");
  222. }
  223. if (!string.IsNullOrEmpty(item.HomePageUrl))
  224. {
  225. builder.Append("<Website>" + SecurityElement.Escape(item.HomePageUrl) + "</Website>");
  226. }
  227. if (!string.IsNullOrEmpty(item.AspectRatio))
  228. {
  229. builder.Append("<AspectRatio>" + SecurityElement.Escape(item.AspectRatio) + "</AspectRatio>");
  230. }
  231. if (!string.IsNullOrEmpty(item.Language))
  232. {
  233. builder.Append("<Language>" + SecurityElement.Escape(item.Language) + "</Language>");
  234. }
  235. // Use original runtime here, actual file runtime later in MediaInfo
  236. var runTimeTicks = item.OriginalRunTimeTicks ?? item.RunTimeTicks;
  237. if (runTimeTicks.HasValue)
  238. {
  239. var timespan = TimeSpan.FromTicks(runTimeTicks.Value);
  240. builder.Append("<RunningTime>" + Convert.ToInt32(timespan.TotalMinutes).ToString(UsCulture) + "</RunningTime>");
  241. builder.Append("<Runtime>" + Convert.ToInt32(timespan.TotalMinutes).ToString(UsCulture) + "</Runtime>");
  242. }
  243. var imdb = item.GetProviderId(MetadataProviders.Imdb);
  244. if (!string.IsNullOrEmpty(imdb))
  245. {
  246. builder.Append("<IMDB_ID>" + SecurityElement.Escape(imdb) + "</IMDB_ID>");
  247. builder.Append("<IMDB>" + SecurityElement.Escape(imdb) + "</IMDB>");
  248. builder.Append("<IMDbId>" + SecurityElement.Escape(imdb) + "</IMDbId>");
  249. }
  250. var tmdb = item.GetProviderId(MetadataProviders.Tmdb);
  251. if (!string.IsNullOrEmpty(tmdb))
  252. {
  253. builder.Append("<TMDbId>" + SecurityElement.Escape(tmdb) + "</TMDbId>");
  254. }
  255. if (!(item is Series))
  256. {
  257. var tvdb = item.GetProviderId(MetadataProviders.Tvdb);
  258. if (!string.IsNullOrEmpty(tvdb))
  259. {
  260. builder.Append("<TvDbId>" + SecurityElement.Escape(tvdb) + "</TvDbId>");
  261. }
  262. }
  263. var tvcom = item.GetProviderId(MetadataProviders.Tvcom);
  264. if (!string.IsNullOrEmpty(tvcom))
  265. {
  266. builder.Append("<TVcomId>" + SecurityElement.Escape(tvcom) + "</TVcomId>");
  267. }
  268. var rt = item.GetProviderId(MetadataProviders.RottenTomatoes);
  269. if (!string.IsNullOrEmpty(rt))
  270. {
  271. builder.Append("<RottenTomatoesId>" + SecurityElement.Escape(rt) + "</RottenTomatoesId>");
  272. }
  273. var mbz = item.GetProviderId(MetadataProviders.Musicbrainz);
  274. if (!string.IsNullOrEmpty(mbz))
  275. {
  276. builder.Append("<MusicbrainzId>" + SecurityElement.Escape(mbz) + "</MusicbrainzId>");
  277. }
  278. var gamesdb = item.GetProviderId(MetadataProviders.Gamesdb);
  279. if (!string.IsNullOrEmpty(gamesdb))
  280. {
  281. builder.Append("<GamesDbId>" + SecurityElement.Escape(gamesdb) + "</GamesDbId>");
  282. }
  283. var tmdbCollection = item.GetProviderId(MetadataProviders.TmdbCollection);
  284. if (!string.IsNullOrEmpty(tmdbCollection))
  285. {
  286. builder.Append("<CollectionNumber>" + SecurityElement.Escape(tmdbCollection) + "</CollectionNumber>");
  287. }
  288. if (item.Taglines.Count > 0)
  289. {
  290. builder.Append("<TagLine>" + SecurityElement.Escape(item.Taglines[0]) + "</TagLine>");
  291. builder.Append("<Taglines>");
  292. foreach (var tagline in item.Taglines)
  293. {
  294. builder.Append("<Tagline>" + SecurityElement.Escape(tagline) + "</Tagline>");
  295. }
  296. builder.Append("</Taglines>");
  297. }
  298. if (item.Genres.Count > 0)
  299. {
  300. builder.Append("<Genres>");
  301. foreach (var genre in item.Genres)
  302. {
  303. builder.Append("<Genre>" + SecurityElement.Escape(genre) + "</Genre>");
  304. }
  305. builder.Append("</Genres>");
  306. builder.Append("<Genre>" + SecurityElement.Escape(string.Join("|", item.Genres.ToArray())) + "</Genre>");
  307. }
  308. if (item.Studios.Count > 0)
  309. {
  310. builder.Append("<Studios>");
  311. foreach (var studio in item.Studios)
  312. {
  313. builder.Append("<Studio>" + SecurityElement.Escape(studio) + "</Studio>");
  314. }
  315. builder.Append("</Studios>");
  316. }
  317. if (item.Tags.Count > 0)
  318. {
  319. builder.Append("<Tags>");
  320. foreach (var tag in item.Tags)
  321. {
  322. builder.Append("<Tag>" + SecurityElement.Escape(tag) + "</Tag>");
  323. }
  324. builder.Append("</Tags>");
  325. }
  326. if (item.People.Count > 0)
  327. {
  328. builder.Append("<Persons>");
  329. foreach (var person in item.People)
  330. {
  331. builder.Append("<Person>");
  332. builder.Append("<Name>" + SecurityElement.Escape(person.Name) + "</Name>");
  333. builder.Append("<Type>" + SecurityElement.Escape(person.Type) + "</Type>");
  334. builder.Append("<Role>" + SecurityElement.Escape(person.Role) + "</Role>");
  335. builder.Append("</Person>");
  336. }
  337. builder.Append("</Persons>");
  338. }
  339. }
  340. /// <summary>
  341. /// Appends the media info.
  342. /// </summary>
  343. /// <typeparam name="T"></typeparam>
  344. /// <param name="item">The item.</param>
  345. /// <param name="builder">The builder.</param>
  346. public static void AddMediaInfo<T>(T item, StringBuilder builder)
  347. where T : BaseItem, IHasMediaStreams
  348. {
  349. builder.Append("<MediaInfo>");
  350. foreach (var stream in item.MediaStreams)
  351. {
  352. builder.Append("<" + stream.Type + ">");
  353. if (!string.IsNullOrEmpty(stream.Codec))
  354. {
  355. builder.Append("<Codec>" + SecurityElement.Escape(stream.Codec) + "</Codec>");
  356. builder.Append("<FFCodec>" + SecurityElement.Escape(stream.Codec) + "</FFCodec>");
  357. }
  358. if (stream.BitRate.HasValue)
  359. {
  360. builder.Append("<BitRate>" + stream.BitRate.Value.ToString(UsCulture) + "</BitRate>");
  361. }
  362. if (stream.Width.HasValue)
  363. {
  364. builder.Append("<Width>" + stream.Width.Value.ToString(UsCulture) + "</Width>");
  365. }
  366. if (stream.Height.HasValue)
  367. {
  368. builder.Append("<Height>" + stream.Height.Value.ToString(UsCulture) + "</Height>");
  369. }
  370. if (!string.IsNullOrEmpty(stream.AspectRatio))
  371. {
  372. builder.Append("<AspectRatio>" + SecurityElement.Escape(stream.AspectRatio) + "</AspectRatio>");
  373. }
  374. var framerate = stream.AverageFrameRate ?? stream.RealFrameRate;
  375. if (framerate.HasValue)
  376. {
  377. builder.Append("<FrameRate>" + framerate.Value.ToString(UsCulture) + "</FrameRate>");
  378. }
  379. if (!string.IsNullOrEmpty(stream.Language))
  380. {
  381. builder.Append("<Language>" + SecurityElement.Escape(stream.Language) + "</Language>");
  382. }
  383. if (!string.IsNullOrEmpty(stream.ScanType))
  384. {
  385. builder.Append("<ScanType>" + SecurityElement.Escape(stream.ScanType) + "</ScanType>");
  386. }
  387. if (stream.Channels.HasValue)
  388. {
  389. builder.Append("<Channels>" + stream.Channels.Value.ToString(UsCulture) + "</Channels>");
  390. }
  391. if (stream.SampleRate.HasValue)
  392. {
  393. builder.Append("<SamplingRate>" + stream.SampleRate.Value.ToString(UsCulture) + "</SamplingRate>");
  394. }
  395. builder.Append("<Default>" + SecurityElement.Escape(stream.IsDefault.ToString()) + "</Default>");
  396. builder.Append("<Forced>" + SecurityElement.Escape(stream.IsForced.ToString()) + "</Forced>");
  397. if (stream.Type == MediaStreamType.Video)
  398. {
  399. if (item.RunTimeTicks.HasValue)
  400. {
  401. var timespan = TimeSpan.FromTicks(item.RunTimeTicks.Value);
  402. builder.Append("<Duration>" + Convert.ToInt32(timespan.TotalMinutes).ToString(UsCulture) + "</Duration>");
  403. builder.Append("<DurationSeconds>" + Convert.ToInt32(timespan.TotalSeconds).ToString(UsCulture) + "</DurationSeconds>");
  404. }
  405. var video = item as Video;
  406. if (video != null && video.Video3DFormat.HasValue)
  407. {
  408. switch (video.Video3DFormat.Value)
  409. {
  410. case Video3DFormat.FullSideBySide:
  411. builder.Append("<Format3D>FSBS</Format3D>");
  412. break;
  413. case Video3DFormat.FullTopAndBottom:
  414. builder.Append("<Format3D>FTAB</Format3D>");
  415. break;
  416. case Video3DFormat.HalfSideBySide:
  417. builder.Append("<Format3D>HSBS</Format3D>");
  418. break;
  419. case Video3DFormat.HalfTopAndBottom:
  420. builder.Append("<Format3D>HTAB</Format3D>");
  421. break;
  422. }
  423. }
  424. }
  425. builder.Append("</" + stream.Type + ">");
  426. }
  427. builder.Append("</MediaInfo>");
  428. }
  429. }
  430. }