DashboardService.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Controller.Configuration;
  4. using MediaBrowser.Controller.Localization;
  5. using MediaBrowser.Controller.Net;
  6. using MediaBrowser.Controller.Plugins;
  7. using MediaBrowser.Model.Extensions;
  8. using MediaBrowser.Model.Logging;
  9. using MediaBrowser.Model.Net;
  10. using MediaBrowser.Model.Serialization;
  11. using ServiceStack;
  12. using ServiceStack.Web;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.IO;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Threading.Tasks;
  19. using CommonIO;
  20. using WebMarkupMin.Core.Minifiers;
  21. namespace MediaBrowser.WebDashboard.Api
  22. {
  23. /// <summary>
  24. /// Class GetDashboardConfigurationPages
  25. /// </summary>
  26. [Route("/web/ConfigurationPages", "GET")]
  27. public class GetDashboardConfigurationPages : IReturn<List<ConfigurationPageInfo>>
  28. {
  29. /// <summary>
  30. /// Gets or sets the type of the page.
  31. /// </summary>
  32. /// <value>The type of the page.</value>
  33. public ConfigurationPageType? PageType { get; set; }
  34. }
  35. /// <summary>
  36. /// Class GetDashboardConfigurationPage
  37. /// </summary>
  38. [Route("/web/ConfigurationPage", "GET")]
  39. public class GetDashboardConfigurationPage
  40. {
  41. /// <summary>
  42. /// Gets or sets the name.
  43. /// </summary>
  44. /// <value>The name.</value>
  45. public string Name { get; set; }
  46. }
  47. [Route("/web/Package", "GET")]
  48. public class GetDashboardPackage
  49. {
  50. public string Mode { get; set; }
  51. }
  52. [Route("/robots.txt", "GET")]
  53. public class GetRobotsTxt
  54. {
  55. }
  56. /// <summary>
  57. /// Class GetDashboardResource
  58. /// </summary>
  59. [Route("/web/{ResourceName*}", "GET")]
  60. public class GetDashboardResource
  61. {
  62. /// <summary>
  63. /// Gets or sets the name.
  64. /// </summary>
  65. /// <value>The name.</value>
  66. public string ResourceName { get; set; }
  67. /// <summary>
  68. /// Gets or sets the V.
  69. /// </summary>
  70. /// <value>The V.</value>
  71. public string V { get; set; }
  72. }
  73. /// <summary>
  74. /// Class DashboardService
  75. /// </summary>
  76. public class DashboardService : IRestfulService, IHasResultFactory
  77. {
  78. /// <summary>
  79. /// Gets or sets the logger.
  80. /// </summary>
  81. /// <value>The logger.</value>
  82. public ILogger Logger { get; set; }
  83. /// <summary>
  84. /// Gets or sets the HTTP result factory.
  85. /// </summary>
  86. /// <value>The HTTP result factory.</value>
  87. public IHttpResultFactory ResultFactory { get; set; }
  88. /// <summary>
  89. /// Gets or sets the request context.
  90. /// </summary>
  91. /// <value>The request context.</value>
  92. public IRequest Request { get; set; }
  93. /// <summary>
  94. /// The _app host
  95. /// </summary>
  96. private readonly IServerApplicationHost _appHost;
  97. /// <summary>
  98. /// The _server configuration manager
  99. /// </summary>
  100. private readonly IServerConfigurationManager _serverConfigurationManager;
  101. private readonly IFileSystem _fileSystem;
  102. private readonly ILocalizationManager _localization;
  103. private readonly IJsonSerializer _jsonSerializer;
  104. /// <summary>
  105. /// Initializes a new instance of the <see cref="DashboardService" /> class.
  106. /// </summary>
  107. /// <param name="appHost">The app host.</param>
  108. /// <param name="serverConfigurationManager">The server configuration manager.</param>
  109. /// <param name="fileSystem">The file system.</param>
  110. public DashboardService(IServerApplicationHost appHost, IServerConfigurationManager serverConfigurationManager, IFileSystem fileSystem, ILocalizationManager localization, IJsonSerializer jsonSerializer)
  111. {
  112. _appHost = appHost;
  113. _serverConfigurationManager = serverConfigurationManager;
  114. _fileSystem = fileSystem;
  115. _localization = localization;
  116. _jsonSerializer = jsonSerializer;
  117. }
  118. /// <summary>
  119. /// Gets the specified request.
  120. /// </summary>
  121. /// <param name="request">The request.</param>
  122. /// <returns>System.Object.</returns>
  123. public Task<object> Get(GetDashboardConfigurationPage request)
  124. {
  125. var page = ServerEntryPoint.Instance.PluginConfigurationPages.First(p => p.Name.Equals(request.Name, StringComparison.OrdinalIgnoreCase));
  126. return ResultFactory.GetStaticResult(Request, page.Plugin.Version.ToString().GetMD5(), null, null, MimeTypes.GetMimeType("page.html"), () => GetPackageCreator().ModifyHtml("dummy.html", page.GetHtmlStream(), null, _appHost.ApplicationVersion.ToString(), null, false));
  127. }
  128. /// <summary>
  129. /// Gets the specified request.
  130. /// </summary>
  131. /// <param name="request">The request.</param>
  132. /// <returns>System.Object.</returns>
  133. public object Get(GetDashboardConfigurationPages request)
  134. {
  135. const string unavilableMessage = "The server is still loading. Please try again momentarily.";
  136. var instance = ServerEntryPoint.Instance;
  137. if (instance == null)
  138. {
  139. throw new InvalidOperationException(unavilableMessage);
  140. }
  141. var pages = instance.PluginConfigurationPages;
  142. if (pages == null)
  143. {
  144. throw new InvalidOperationException(unavilableMessage);
  145. }
  146. if (request.PageType.HasValue)
  147. {
  148. pages = pages.Where(p => p.ConfigurationPageType == request.PageType.Value);
  149. }
  150. // Don't allow a failing plugin to fail them all
  151. var configPages = pages.Select(p =>
  152. {
  153. try
  154. {
  155. return new ConfigurationPageInfo(p);
  156. }
  157. catch (Exception ex)
  158. {
  159. Logger.ErrorException("Error getting plugin information from {0}", ex, p.GetType().Name);
  160. return null;
  161. }
  162. })
  163. .Where(i => i != null)
  164. .ToList();
  165. return ResultFactory.GetOptimizedResult(Request, configPages);
  166. }
  167. public object Get(GetRobotsTxt request)
  168. {
  169. return Get(new GetDashboardResource
  170. {
  171. ResourceName = "robots.txt"
  172. });
  173. }
  174. /// <summary>
  175. /// Gets the specified request.
  176. /// </summary>
  177. /// <param name="request">The request.</param>
  178. /// <returns>System.Object.</returns>
  179. public async Task<object> Get(GetDashboardResource request)
  180. {
  181. var path = request.ResourceName;
  182. path = path.Replace("bower_components" + _appHost.ApplicationVersion, "bower_components", StringComparison.OrdinalIgnoreCase);
  183. var contentType = MimeTypes.GetMimeType(path);
  184. // Bounce them to the startup wizard if it hasn't been completed yet
  185. if (!_serverConfigurationManager.Configuration.IsStartupWizardCompleted && path.IndexOf("wizard", StringComparison.OrdinalIgnoreCase) == -1 && GetPackageCreator().IsCoreHtml(path))
  186. {
  187. // But don't redirect if an html import is being requested.
  188. if (path.IndexOf("bower_components", StringComparison.OrdinalIgnoreCase) == -1)
  189. {
  190. Request.Response.Redirect("wizardstart.html");
  191. return null;
  192. }
  193. }
  194. path = path.Replace("scripts/jquery.mobile-1.4.5.min.map", "thirdparty/jquerymobile-1.4.5/jquery.mobile-1.4.5.min.map", StringComparison.OrdinalIgnoreCase);
  195. var localizationCulture = GetLocalizationCulture();
  196. // Don't cache if not configured to do so
  197. // But always cache images to simulate production
  198. if (!_serverConfigurationManager.Configuration.EnableDashboardResponseCaching &&
  199. !contentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase) &&
  200. !contentType.StartsWith("font/", StringComparison.OrdinalIgnoreCase))
  201. {
  202. var stream = await GetResourceStream(path, localizationCulture).ConfigureAwait(false);
  203. return ResultFactory.GetResult(stream, contentType);
  204. }
  205. TimeSpan? cacheDuration = null;
  206. // Cache images unconditionally - updates to image files will require new filename
  207. // If there's a version number in the query string we can cache this unconditionally
  208. if (contentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase) || contentType.StartsWith("font/", StringComparison.OrdinalIgnoreCase) || !string.IsNullOrEmpty(request.V))
  209. {
  210. cacheDuration = TimeSpan.FromDays(365);
  211. }
  212. var assembly = GetType().Assembly.GetName();
  213. var cacheKey = (assembly.Version + (localizationCulture ?? string.Empty) + path).GetMD5();
  214. return await ResultFactory.GetStaticResult(Request, cacheKey, null, cacheDuration, contentType, () => GetResourceStream(path, localizationCulture)).ConfigureAwait(false);
  215. }
  216. private string GetLocalizationCulture()
  217. {
  218. return _serverConfigurationManager.Configuration.UICulture;
  219. }
  220. /// <summary>
  221. /// Gets the resource stream.
  222. /// </summary>
  223. /// <param name="path">The path.</param>
  224. /// <param name="localizationCulture">The localization culture.</param>
  225. /// <returns>Task{Stream}.</returns>
  226. private Task<Stream> GetResourceStream(string path, string localizationCulture)
  227. {
  228. var minify = _serverConfigurationManager.Configuration.EnableDashboardResourceMinification;
  229. return GetPackageCreator()
  230. .GetResource(path, null, localizationCulture, _appHost.ApplicationVersion.ToString(), minify);
  231. }
  232. private PackageCreator GetPackageCreator()
  233. {
  234. return new PackageCreator(_fileSystem, _localization, Logger, _serverConfigurationManager, _jsonSerializer);
  235. }
  236. public async Task<object> Get(GetDashboardPackage request)
  237. {
  238. var path = Path.Combine(_serverConfigurationManager.ApplicationPaths.ProgramDataPath,
  239. "webclient-dump");
  240. try
  241. {
  242. _fileSystem.DeleteDirectory(path, true);
  243. }
  244. catch (IOException)
  245. {
  246. }
  247. var creator = GetPackageCreator();
  248. CopyDirectory(creator.DashboardUIPath, path);
  249. string culture = null;
  250. var appVersion = _appHost.ApplicationVersion.ToString();
  251. var mode = request.Mode;
  252. if (string.Equals(mode, "cordova", StringComparison.OrdinalIgnoreCase))
  253. {
  254. _fileSystem.DeleteFile(Path.Combine(path, "scripts", "registrationservices.js"));
  255. }
  256. // Try to trim the output size a bit
  257. var bowerPath = Path.Combine(path, "bower_components");
  258. if (!string.Equals(mode, "cordova", StringComparison.OrdinalIgnoreCase))
  259. {
  260. //var versionedBowerPath = Path.Combine(Path.GetDirectoryName(bowerPath), "bower_components" + _appHost.ApplicationVersion);
  261. //Directory.Move(bowerPath, versionedBowerPath);
  262. //bowerPath = versionedBowerPath;
  263. }
  264. DeleteFilesByExtension(bowerPath, ".log");
  265. DeleteFilesByExtension(bowerPath, ".txt");
  266. DeleteFilesByExtension(bowerPath, ".map");
  267. DeleteFilesByExtension(bowerPath, ".md");
  268. DeleteFilesByExtension(bowerPath, ".json", "strings\\");
  269. DeleteFilesByExtension(bowerPath, ".gz");
  270. DeleteFilesByExtension(bowerPath, ".bat");
  271. DeleteFilesByExtension(bowerPath, ".sh");
  272. DeleteFilesByName(bowerPath, "copying", true);
  273. DeleteFilesByName(bowerPath, "license", true);
  274. DeleteFilesByName(bowerPath, "license-mit", true);
  275. DeleteFilesByName(bowerPath, "gitignore");
  276. DeleteFilesByName(bowerPath, "npmignore");
  277. DeleteFilesByName(bowerPath, "jshintrc");
  278. DeleteFilesByName(bowerPath, "gruntfile");
  279. DeleteFilesByName(bowerPath, "bowerrc");
  280. DeleteFilesByName(bowerPath, "jscsrc");
  281. DeleteFilesByName(bowerPath, "hero.svg");
  282. DeleteFilesByName(bowerPath, "travis.yml");
  283. DeleteFilesByName(bowerPath, "build.js");
  284. DeleteFilesByName(bowerPath, "editorconfig");
  285. DeleteFilesByName(bowerPath, "gitattributes");
  286. DeleteFoldersByName(bowerPath, "demo");
  287. DeleteFoldersByName(bowerPath, "test");
  288. DeleteFoldersByName(bowerPath, "guides");
  289. DeleteFoldersByName(bowerPath, "grunt");
  290. DeleteFoldersByName(bowerPath, "rollups");
  291. if (string.Equals(mode, "cordova", StringComparison.OrdinalIgnoreCase))
  292. {
  293. DeleteFoldersByName(Path.Combine(bowerPath, "emby-webcomponents", "fonts"), "montserrat");
  294. DeleteFoldersByName(Path.Combine(bowerPath, "emby-webcomponents", "fonts"), "opensans");
  295. DeleteFoldersByName(Path.Combine(bowerPath, "emby-webcomponents", "fonts"), "roboto");
  296. }
  297. _fileSystem.DeleteDirectory(Path.Combine(bowerPath, "jquery", "src"), true);
  298. _fileSystem.DeleteDirectory(Path.Combine(bowerPath, "fingerprintjs2", "flash"), true);
  299. _fileSystem.DeleteDirectory(Path.Combine(bowerPath, "fingerprintjs2", "specs"), true);
  300. DeleteCryptoFiles(Path.Combine(bowerPath, "cryptojslib", "components"));
  301. DeleteFoldersByName(Path.Combine(bowerPath, "jquery"), "src");
  302. DeleteFoldersByName(Path.Combine(bowerPath, "jstree"), "src");
  303. DeleteFoldersByName(Path.Combine(bowerPath, "Sortable"), "meteor");
  304. DeleteFoldersByName(Path.Combine(bowerPath, "Sortable"), "st");
  305. DeleteFoldersByName(Path.Combine(bowerPath, "Swiper"), "src");
  306. _fileSystem.DeleteDirectory(Path.Combine(bowerPath, "marked"), true);
  307. _fileSystem.DeleteDirectory(Path.Combine(bowerPath, "marked-element"), true);
  308. _fileSystem.DeleteDirectory(Path.Combine(bowerPath, "prism"), true);
  309. _fileSystem.DeleteDirectory(Path.Combine(bowerPath, "prism-element"), true);
  310. if (string.Equals(mode, "cordova", StringComparison.OrdinalIgnoreCase))
  311. {
  312. // Delete things that are unneeded in an attempt to keep the output as trim as possible
  313. _fileSystem.DeleteDirectory(Path.Combine(path, "css", "images", "tour"), true);
  314. _fileSystem.DeleteFile(Path.Combine(path, "thirdparty", "jquerymobile-1.4.5", "jquery.mobile-1.4.5.min.map"));
  315. }
  316. else
  317. {
  318. MinifyCssDirectory(path);
  319. MinifyJsDirectory(path);
  320. }
  321. await DumpHtml(creator.DashboardUIPath, path, mode, culture, appVersion);
  322. await DumpFile("css/all.css", Path.Combine(path, "css", "all.css"), mode, culture, appVersion).ConfigureAwait(false);
  323. return "";
  324. }
  325. private void DeleteCryptoFiles(string path)
  326. {
  327. var files = _fileSystem.GetFiles(path)
  328. .ToList();
  329. var keepFiles = new[] { "core-min.js", "md5-min.js", "sha1-min.js" };
  330. foreach (var file in files)
  331. {
  332. if (!keepFiles.Contains(file.Name, StringComparer.OrdinalIgnoreCase))
  333. {
  334. _fileSystem.DeleteFile(file.FullName);
  335. }
  336. }
  337. }
  338. private void DeleteFilesByExtension(string path, string extension, string exclude = null)
  339. {
  340. var files = _fileSystem.GetFiles(path, true)
  341. .Where(i => string.Equals(i.Extension, extension, StringComparison.OrdinalIgnoreCase))
  342. .ToList();
  343. foreach (var file in files)
  344. {
  345. if (!string.IsNullOrWhiteSpace(exclude))
  346. {
  347. if (file.FullName.IndexOf(exclude, StringComparison.OrdinalIgnoreCase) != -1)
  348. {
  349. continue;
  350. }
  351. }
  352. _fileSystem.DeleteFile(file.FullName);
  353. }
  354. }
  355. private void DeleteFilesByName(string path, string name, bool exact = false)
  356. {
  357. var files = _fileSystem.GetFiles(path, true)
  358. .Where(i => string.Equals(i.Name, name, StringComparison.OrdinalIgnoreCase) || (!exact && i.Name.IndexOf(name, StringComparison.OrdinalIgnoreCase) != -1))
  359. .ToList();
  360. foreach (var file in files)
  361. {
  362. _fileSystem.DeleteFile(file.FullName);
  363. }
  364. }
  365. private void DeleteFoldersByName(string path, string name)
  366. {
  367. var directories = _fileSystem.GetDirectories(path, true)
  368. .Where(i => string.Equals(i.Name, name, StringComparison.OrdinalIgnoreCase))
  369. .ToList();
  370. foreach (var directory in directories)
  371. {
  372. _fileSystem.DeleteDirectory(directory.FullName, true);
  373. }
  374. }
  375. private void MinifyCssDirectory(string path)
  376. {
  377. foreach (var file in Directory.GetFiles(path, "*.css", SearchOption.AllDirectories))
  378. {
  379. if (file.IndexOf(".min.", StringComparison.OrdinalIgnoreCase) != -1)
  380. {
  381. continue;
  382. }
  383. if (file.IndexOf("bower_", StringComparison.OrdinalIgnoreCase) != -1)
  384. {
  385. continue;
  386. }
  387. try
  388. {
  389. var text = _fileSystem.ReadAllText(file, Encoding.UTF8);
  390. var result = new KristensenCssMinifier().Minify(text, false, Encoding.UTF8);
  391. if (result.Errors.Count > 0)
  392. {
  393. Logger.Error("Error minifying css: " + result.Errors[0].Message);
  394. }
  395. else
  396. {
  397. text = result.MinifiedContent;
  398. _fileSystem.WriteAllText(file, text, Encoding.UTF8);
  399. }
  400. }
  401. catch (Exception ex)
  402. {
  403. Logger.ErrorException("Error minifying css", ex);
  404. }
  405. }
  406. }
  407. private void MinifyJsDirectory(string path)
  408. {
  409. foreach (var file in Directory.GetFiles(path, "*.js", SearchOption.AllDirectories))
  410. {
  411. if (file.IndexOf(".min.", StringComparison.OrdinalIgnoreCase) != -1)
  412. {
  413. continue;
  414. }
  415. if (file.IndexOf("bower_", StringComparison.OrdinalIgnoreCase) != -1)
  416. {
  417. continue;
  418. }
  419. try
  420. {
  421. var text = _fileSystem.ReadAllText(file, Encoding.UTF8);
  422. var result = new CrockfordJsMinifier().Minify(text, false, Encoding.UTF8);
  423. if (result.Errors.Count > 0)
  424. {
  425. Logger.Error("Error minifying javascript: " + result.Errors[0].Message);
  426. }
  427. else
  428. {
  429. text = result.MinifiedContent;
  430. _fileSystem.WriteAllText(file, text, Encoding.UTF8);
  431. }
  432. }
  433. catch (Exception ex)
  434. {
  435. Logger.ErrorException("Error minifying css", ex);
  436. }
  437. }
  438. }
  439. private async Task DumpHtml(string source, string destination, string mode, string culture, string appVersion)
  440. {
  441. foreach (var file in Directory.GetFiles(source, "*", SearchOption.TopDirectoryOnly))
  442. {
  443. var filename = Path.GetFileName(file);
  444. await DumpFile(filename, Path.Combine(destination, filename), mode, culture, appVersion).ConfigureAwait(false);
  445. }
  446. var excludeFiles = new List<string>();
  447. if (string.Equals(mode, "cordova", StringComparison.OrdinalIgnoreCase))
  448. {
  449. excludeFiles.Add("supporterkey.html");
  450. }
  451. foreach (var file in excludeFiles)
  452. {
  453. _fileSystem.DeleteFile(Path.Combine(destination, file));
  454. }
  455. }
  456. private async Task DumpFile(string resourceVirtualPath, string destinationFilePath, string mode, string culture, string appVersion)
  457. {
  458. using (var stream = await GetPackageCreator().GetResource(resourceVirtualPath, mode, culture, appVersion, true).ConfigureAwait(false))
  459. {
  460. using (var fs = _fileSystem.GetFileStream(destinationFilePath, FileMode.Create, FileAccess.Write, FileShare.Read))
  461. {
  462. stream.CopyTo(fs);
  463. }
  464. }
  465. }
  466. private void CopyDirectory(string source, string destination)
  467. {
  468. _fileSystem.CreateDirectory(destination);
  469. //Now Create all of the directories
  470. foreach (string dirPath in Directory.GetDirectories(source, "*",
  471. SearchOption.AllDirectories))
  472. _fileSystem.CreateDirectory(dirPath.Replace(source, destination));
  473. //Copy all the files & Replaces any files with the same name
  474. foreach (string newPath in Directory.GetFiles(source, "*.*",
  475. SearchOption.AllDirectories))
  476. _fileSystem.CopyFile(newPath, newPath.Replace(source, destination), true);
  477. }
  478. }
  479. }