Plugin.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.ComponentModel.Composition;
  3. using System.Net;
  4. using System.Reactive.Linq;
  5. using MediaBrowser.Api.HttpHandlers;
  6. using MediaBrowser.Common.Net.Handlers;
  7. using MediaBrowser.Common.Plugins;
  8. using MediaBrowser.Controller;
  9. using MediaBrowser.Model.Plugins;
  10. namespace MediaBrowser.Api
  11. {
  12. [Export(typeof(BasePlugin))]
  13. public class Plugin : BaseGenericPlugin<BasePluginConfiguration>
  14. {
  15. public override string Name
  16. {
  17. get { return "WebAPI"; }
  18. }
  19. public override void Init()
  20. {
  21. var httpServer = Kernel.Instance.HttpServer;
  22. httpServer.Where(ctx => ctx.Request.Url.LocalPath.IndexOf("/api/", StringComparison.OrdinalIgnoreCase) != -1).Subscribe(ctx =>
  23. {
  24. BaseHandler handler = GetHandler(ctx);
  25. if (handler != null)
  26. {
  27. handler.ProcessRequest(ctx);
  28. }
  29. });
  30. }
  31. private BaseHandler GetHandler(HttpListenerContext ctx)
  32. {
  33. string localPath = ctx.Request.Url.LocalPath;
  34. if (localPath.EndsWith("/api/item", StringComparison.OrdinalIgnoreCase))
  35. {
  36. return new ItemHandler();
  37. }
  38. else if (localPath.EndsWith("/api/image", StringComparison.OrdinalIgnoreCase))
  39. {
  40. return new ImageHandler();
  41. }
  42. else if (localPath.EndsWith("/api/users", StringComparison.OrdinalIgnoreCase))
  43. {
  44. return new UsersHandler();
  45. }
  46. else if (localPath.EndsWith("/api/genre", StringComparison.OrdinalIgnoreCase))
  47. {
  48. return new GenreHandler();
  49. }
  50. else if (localPath.EndsWith("/api/genres", StringComparison.OrdinalIgnoreCase))
  51. {
  52. return new GenresHandler();
  53. }
  54. else if (localPath.EndsWith("/api/studio", StringComparison.OrdinalIgnoreCase))
  55. {
  56. return new StudioHandler();
  57. }
  58. else if (localPath.EndsWith("/api/studios", StringComparison.OrdinalIgnoreCase))
  59. {
  60. return new StudiosHandler();
  61. }
  62. else if (localPath.EndsWith("/api/recentlyaddeditems", StringComparison.OrdinalIgnoreCase))
  63. {
  64. return new RecentlyAddedItemsHandler();
  65. }
  66. else if (localPath.EndsWith("/api/inprogressitems", StringComparison.OrdinalIgnoreCase))
  67. {
  68. return new InProgressItemsHandler();
  69. }
  70. else if (localPath.EndsWith("/api/userconfiguration", StringComparison.OrdinalIgnoreCase))
  71. {
  72. return new UserConfigurationHandler();
  73. }
  74. else if (localPath.EndsWith("/api/plugins", StringComparison.OrdinalIgnoreCase))
  75. {
  76. return new PluginsHandler();
  77. }
  78. else if (localPath.EndsWith("/api/pluginconfiguration", StringComparison.OrdinalIgnoreCase))
  79. {
  80. return new PluginConfigurationHandler();
  81. }
  82. else if (localPath.EndsWith("/api/static", StringComparison.OrdinalIgnoreCase))
  83. {
  84. return new StaticFileHandler();
  85. }
  86. else if (localPath.EndsWith("/api/audio", StringComparison.OrdinalIgnoreCase))
  87. {
  88. return new AudioHandler();
  89. }
  90. return null;
  91. }
  92. }
  93. }