XmlSaverHelpers.cs 19 KB

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