BaseApiService.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. using MediaBrowser.Controller.Dto;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Entities.Audio;
  4. using MediaBrowser.Controller.Library;
  5. using MediaBrowser.Controller.Net;
  6. using MediaBrowser.Controller.Session;
  7. using MediaBrowser.Model.Entities;
  8. using MediaBrowser.Model.Logging;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Threading.Tasks;
  13. using MediaBrowser.Model.Services;
  14. namespace MediaBrowser.Api
  15. {
  16. /// <summary>
  17. /// Class BaseApiService
  18. /// </summary>
  19. public class BaseApiService : IService, IRequiresRequest
  20. {
  21. /// <summary>
  22. /// Gets or sets the logger.
  23. /// </summary>
  24. /// <value>The logger.</value>
  25. public ILogger Logger
  26. {
  27. get
  28. {
  29. return ApiEntryPoint.Instance.Logger;
  30. }
  31. }
  32. /// <summary>
  33. /// Gets or sets the HTTP result factory.
  34. /// </summary>
  35. /// <value>The HTTP result factory.</value>
  36. public IHttpResultFactory ResultFactory
  37. {
  38. get
  39. {
  40. return ApiEntryPoint.Instance.ResultFactory;
  41. }
  42. }
  43. /// <summary>
  44. /// Gets or sets the request context.
  45. /// </summary>
  46. /// <value>The request context.</value>
  47. public IRequest Request { get; set; }
  48. public string GetHeader(string name)
  49. {
  50. return Request.Headers[name];
  51. }
  52. /// <summary>
  53. /// To the optimized result.
  54. /// </summary>
  55. /// <typeparam name="T"></typeparam>
  56. /// <param name="result">The result.</param>
  57. /// <returns>System.Object.</returns>
  58. protected object ToOptimizedResult<T>(T result)
  59. where T : class
  60. {
  61. return ResultFactory.GetOptimizedResult(Request, result);
  62. }
  63. protected void AssertCanUpdateUser(IAuthorizationContext authContext, IUserManager userManager, string userId)
  64. {
  65. var auth = authContext.GetAuthorizationInfo(Request);
  66. var authenticatedUser = userManager.GetUserById(auth.UserId);
  67. // If they're going to update the record of another user, they must be an administrator
  68. if (!string.Equals(userId, auth.UserId, StringComparison.OrdinalIgnoreCase))
  69. {
  70. if (!authenticatedUser.Policy.IsAdministrator)
  71. {
  72. throw new SecurityException("Unauthorized access.");
  73. }
  74. }
  75. else
  76. {
  77. if (!authenticatedUser.Policy.EnableUserPreferenceAccess)
  78. {
  79. throw new SecurityException("Unauthorized access.");
  80. }
  81. }
  82. }
  83. /// <summary>
  84. /// To the optimized serialized result using cache.
  85. /// </summary>
  86. /// <typeparam name="T"></typeparam>
  87. /// <param name="result">The result.</param>
  88. /// <returns>System.Object.</returns>
  89. protected object ToOptimizedSerializedResultUsingCache<T>(T result)
  90. where T : class
  91. {
  92. return ToOptimizedResult(result);
  93. }
  94. /// <summary>
  95. /// Gets the session.
  96. /// </summary>
  97. /// <returns>SessionInfo.</returns>
  98. protected async Task<SessionInfo> GetSession(ISessionContext sessionContext)
  99. {
  100. var session = await sessionContext.GetSession(Request).ConfigureAwait(false);
  101. if (session == null)
  102. {
  103. throw new ArgumentException("Session not found.");
  104. }
  105. return session;
  106. }
  107. protected DtoOptions GetDtoOptions(IAuthorizationContext authContext, object request)
  108. {
  109. var options = new DtoOptions();
  110. var authInfo = authContext.GetAuthorizationInfo(Request);
  111. options.DeviceId = authInfo.DeviceId;
  112. var hasFields = request as IHasItemFields;
  113. if (hasFields != null)
  114. {
  115. options.Fields = hasFields.GetItemFields().ToList();
  116. }
  117. var client = authInfo.Client ?? string.Empty;
  118. if (client.IndexOf("kodi", StringComparison.OrdinalIgnoreCase) != -1 ||
  119. client.IndexOf("wmc", StringComparison.OrdinalIgnoreCase) != -1 ||
  120. client.IndexOf("media center", StringComparison.OrdinalIgnoreCase) != -1 ||
  121. client.IndexOf("classic", StringComparison.OrdinalIgnoreCase) != -1)
  122. {
  123. options.Fields.Add(Model.Querying.ItemFields.RecursiveItemCount);
  124. }
  125. if (client.IndexOf("kodi", StringComparison.OrdinalIgnoreCase) != -1 ||
  126. client.IndexOf("wmc", StringComparison.OrdinalIgnoreCase) != -1 ||
  127. client.IndexOf("media center", StringComparison.OrdinalIgnoreCase) != -1 ||
  128. client.IndexOf("classic", StringComparison.OrdinalIgnoreCase) != -1 ||
  129. client.IndexOf("roku", StringComparison.OrdinalIgnoreCase) != -1 ||
  130. client.IndexOf("samsung", StringComparison.OrdinalIgnoreCase) != -1)
  131. {
  132. options.Fields.Add(Model.Querying.ItemFields.ChildCount);
  133. }
  134. if (client.IndexOf("web", StringComparison.OrdinalIgnoreCase) == -1 &&
  135. // covers both emby mobile and emby for android mobile
  136. client.IndexOf("mobile", StringComparison.OrdinalIgnoreCase) == -1 &&
  137. client.IndexOf("ios", StringComparison.OrdinalIgnoreCase) == -1 &&
  138. client.IndexOf("theater", StringComparison.OrdinalIgnoreCase) == -1)
  139. {
  140. options.Fields.Add(Model.Querying.ItemFields.ChildCount);
  141. }
  142. var hasDtoOptions = request as IHasDtoOptions;
  143. if (hasDtoOptions != null)
  144. {
  145. options.EnableImages = hasDtoOptions.EnableImages ?? true;
  146. if (hasDtoOptions.ImageTypeLimit.HasValue)
  147. {
  148. options.ImageTypeLimit = hasDtoOptions.ImageTypeLimit.Value;
  149. }
  150. if (hasDtoOptions.EnableUserData.HasValue)
  151. {
  152. options.EnableUserData = hasDtoOptions.EnableUserData.Value;
  153. }
  154. if (!string.IsNullOrWhiteSpace(hasDtoOptions.EnableImageTypes))
  155. {
  156. options.ImageTypes = (hasDtoOptions.EnableImageTypes ?? string.Empty).Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).Select(v => (ImageType)Enum.Parse(typeof(ImageType), v, true)).ToList();
  157. }
  158. }
  159. return options;
  160. }
  161. protected MusicArtist GetArtist(string name, ILibraryManager libraryManager)
  162. {
  163. if (name.IndexOf(BaseItem.SlugChar) != -1)
  164. {
  165. var result = libraryManager.GetItemList(new InternalItemsQuery
  166. {
  167. SlugName = name,
  168. IncludeItemTypes = new[] { typeof(MusicArtist).Name }
  169. }).OfType<MusicArtist>().FirstOrDefault();
  170. if (result != null)
  171. {
  172. return result;
  173. }
  174. }
  175. return libraryManager.GetArtist(name);
  176. }
  177. protected Studio GetStudio(string name, ILibraryManager libraryManager)
  178. {
  179. if (name.IndexOf(BaseItem.SlugChar) != -1)
  180. {
  181. var result = libraryManager.GetItemList(new InternalItemsQuery
  182. {
  183. SlugName = name,
  184. IncludeItemTypes = new[] { typeof(Studio).Name }
  185. }).OfType<Studio>().FirstOrDefault();
  186. if (result != null)
  187. {
  188. return result;
  189. }
  190. }
  191. return libraryManager.GetStudio(name);
  192. }
  193. protected Genre GetGenre(string name, ILibraryManager libraryManager)
  194. {
  195. if (name.IndexOf(BaseItem.SlugChar) != -1)
  196. {
  197. var result = libraryManager.GetItemList(new InternalItemsQuery
  198. {
  199. SlugName = name,
  200. IncludeItemTypes = new[] { typeof(Genre).Name }
  201. }).OfType<Genre>().FirstOrDefault();
  202. if (result != null)
  203. {
  204. return result;
  205. }
  206. }
  207. return libraryManager.GetGenre(name);
  208. }
  209. protected MusicGenre GetMusicGenre(string name, ILibraryManager libraryManager)
  210. {
  211. if (name.IndexOf(BaseItem.SlugChar) != -1)
  212. {
  213. var result = libraryManager.GetItemList(new InternalItemsQuery
  214. {
  215. SlugName = name,
  216. IncludeItemTypes = new[] { typeof(MusicGenre).Name }
  217. }).OfType<MusicGenre>().FirstOrDefault();
  218. if (result != null)
  219. {
  220. return result;
  221. }
  222. }
  223. return libraryManager.GetMusicGenre(name);
  224. }
  225. protected GameGenre GetGameGenre(string name, ILibraryManager libraryManager)
  226. {
  227. if (name.IndexOf(BaseItem.SlugChar) != -1)
  228. {
  229. var result = libraryManager.GetItemList(new InternalItemsQuery
  230. {
  231. SlugName = name,
  232. IncludeItemTypes = new[] { typeof(GameGenre).Name }
  233. }).OfType<GameGenre>().FirstOrDefault();
  234. if (result != null)
  235. {
  236. return result;
  237. }
  238. }
  239. return libraryManager.GetGameGenre(name);
  240. }
  241. protected Person GetPerson(string name, ILibraryManager libraryManager)
  242. {
  243. if (name.IndexOf(BaseItem.SlugChar) != -1)
  244. {
  245. var result = libraryManager.GetItemList(new InternalItemsQuery
  246. {
  247. SlugName = name,
  248. IncludeItemTypes = new[] { typeof(Person).Name }
  249. }).OfType<Person>().FirstOrDefault();
  250. if (result != null)
  251. {
  252. return result;
  253. }
  254. }
  255. return libraryManager.GetPerson(name);
  256. }
  257. protected string GetPathValue(int index)
  258. {
  259. var pathInfo = Parse(Request.PathInfo);
  260. var first = pathInfo[0];
  261. // backwards compatibility
  262. if (string.Equals(first, "mediabrowser", StringComparison.OrdinalIgnoreCase) ||
  263. string.Equals(first, "emby", StringComparison.OrdinalIgnoreCase))
  264. {
  265. index++;
  266. }
  267. return pathInfo[index];
  268. }
  269. private static List<string> Parse(string pathUri)
  270. {
  271. var actionParts = pathUri.Split(new[] { "://" }, StringSplitOptions.None);
  272. var pathInfo = actionParts[actionParts.Length - 1];
  273. var optionsPos = pathInfo.LastIndexOf('?');
  274. if (optionsPos != -1)
  275. {
  276. pathInfo = pathInfo.Substring(0, optionsPos);
  277. }
  278. var args = pathInfo.Split('/');
  279. return args.Skip(1).ToList();
  280. }
  281. /// <summary>
  282. /// Gets the name of the item by.
  283. /// </summary>
  284. /// <param name="name">The name.</param>
  285. /// <param name="type">The type.</param>
  286. /// <param name="libraryManager">The library manager.</param>
  287. /// <returns>Task{BaseItem}.</returns>
  288. protected BaseItem GetItemByName(string name, string type, ILibraryManager libraryManager)
  289. {
  290. BaseItem item;
  291. if (type.IndexOf("Person", StringComparison.OrdinalIgnoreCase) == 0)
  292. {
  293. item = GetPerson(name, libraryManager);
  294. }
  295. else if (type.IndexOf("Artist", StringComparison.OrdinalIgnoreCase) == 0)
  296. {
  297. item = GetArtist(name, libraryManager);
  298. }
  299. else if (type.IndexOf("Genre", StringComparison.OrdinalIgnoreCase) == 0)
  300. {
  301. item = GetGenre(name, libraryManager);
  302. }
  303. else if (type.IndexOf("MusicGenre", StringComparison.OrdinalIgnoreCase) == 0)
  304. {
  305. item = GetMusicGenre(name, libraryManager);
  306. }
  307. else if (type.IndexOf("GameGenre", StringComparison.OrdinalIgnoreCase) == 0)
  308. {
  309. item = GetGameGenre(name, libraryManager);
  310. }
  311. else if (type.IndexOf("Studio", StringComparison.OrdinalIgnoreCase) == 0)
  312. {
  313. item = GetStudio(name, libraryManager);
  314. }
  315. else if (type.IndexOf("Year", StringComparison.OrdinalIgnoreCase) == 0)
  316. {
  317. item = libraryManager.GetYear(int.Parse(name));
  318. }
  319. else
  320. {
  321. throw new ArgumentException();
  322. }
  323. return item;
  324. }
  325. }
  326. }