BaseApiService.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. client.IndexOf("androidtv", StringComparison.OrdinalIgnoreCase) != -1)
  132. {
  133. options.Fields.Add(Model.Querying.ItemFields.ChildCount);
  134. }
  135. if (client.IndexOf("web", StringComparison.OrdinalIgnoreCase) == -1 &&
  136. // covers both emby mobile and emby for android mobile
  137. client.IndexOf("mobile", StringComparison.OrdinalIgnoreCase) == -1 &&
  138. client.IndexOf("ios", StringComparison.OrdinalIgnoreCase) == -1 &&
  139. client.IndexOf("theater", StringComparison.OrdinalIgnoreCase) == -1)
  140. {
  141. options.Fields.Add(Model.Querying.ItemFields.ChildCount);
  142. }
  143. var hasDtoOptions = request as IHasDtoOptions;
  144. if (hasDtoOptions != null)
  145. {
  146. options.EnableImages = hasDtoOptions.EnableImages ?? true;
  147. if (hasDtoOptions.ImageTypeLimit.HasValue)
  148. {
  149. options.ImageTypeLimit = hasDtoOptions.ImageTypeLimit.Value;
  150. }
  151. if (hasDtoOptions.EnableUserData.HasValue)
  152. {
  153. options.EnableUserData = hasDtoOptions.EnableUserData.Value;
  154. }
  155. if (!string.IsNullOrWhiteSpace(hasDtoOptions.EnableImageTypes))
  156. {
  157. options.ImageTypes = (hasDtoOptions.EnableImageTypes ?? string.Empty).Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).Select(v => (ImageType)Enum.Parse(typeof(ImageType), v, true)).ToList();
  158. }
  159. }
  160. return options;
  161. }
  162. protected MusicArtist GetArtist(string name, ILibraryManager libraryManager)
  163. {
  164. if (name.IndexOf(BaseItem.SlugChar) != -1)
  165. {
  166. var result = libraryManager.GetItemList(new InternalItemsQuery
  167. {
  168. SlugName = name,
  169. IncludeItemTypes = new[] { typeof(MusicArtist).Name }
  170. }).OfType<MusicArtist>().FirstOrDefault();
  171. if (result != null)
  172. {
  173. return result;
  174. }
  175. }
  176. return libraryManager.GetArtist(name);
  177. }
  178. protected Studio GetStudio(string name, ILibraryManager libraryManager)
  179. {
  180. if (name.IndexOf(BaseItem.SlugChar) != -1)
  181. {
  182. var result = libraryManager.GetItemList(new InternalItemsQuery
  183. {
  184. SlugName = name,
  185. IncludeItemTypes = new[] { typeof(Studio).Name }
  186. }).OfType<Studio>().FirstOrDefault();
  187. if (result != null)
  188. {
  189. return result;
  190. }
  191. }
  192. return libraryManager.GetStudio(name);
  193. }
  194. protected Genre GetGenre(string name, ILibraryManager libraryManager)
  195. {
  196. if (name.IndexOf(BaseItem.SlugChar) != -1)
  197. {
  198. var result = libraryManager.GetItemList(new InternalItemsQuery
  199. {
  200. SlugName = name,
  201. IncludeItemTypes = new[] { typeof(Genre).Name }
  202. }).OfType<Genre>().FirstOrDefault();
  203. if (result != null)
  204. {
  205. return result;
  206. }
  207. }
  208. return libraryManager.GetGenre(name);
  209. }
  210. protected MusicGenre GetMusicGenre(string name, ILibraryManager libraryManager)
  211. {
  212. if (name.IndexOf(BaseItem.SlugChar) != -1)
  213. {
  214. var result = libraryManager.GetItemList(new InternalItemsQuery
  215. {
  216. SlugName = name,
  217. IncludeItemTypes = new[] { typeof(MusicGenre).Name }
  218. }).OfType<MusicGenre>().FirstOrDefault();
  219. if (result != null)
  220. {
  221. return result;
  222. }
  223. }
  224. return libraryManager.GetMusicGenre(name);
  225. }
  226. protected GameGenre GetGameGenre(string name, ILibraryManager libraryManager)
  227. {
  228. if (name.IndexOf(BaseItem.SlugChar) != -1)
  229. {
  230. var result = libraryManager.GetItemList(new InternalItemsQuery
  231. {
  232. SlugName = name,
  233. IncludeItemTypes = new[] { typeof(GameGenre).Name }
  234. }).OfType<GameGenre>().FirstOrDefault();
  235. if (result != null)
  236. {
  237. return result;
  238. }
  239. }
  240. return libraryManager.GetGameGenre(name);
  241. }
  242. protected Person GetPerson(string name, ILibraryManager libraryManager)
  243. {
  244. if (name.IndexOf(BaseItem.SlugChar) != -1)
  245. {
  246. var result = libraryManager.GetItemList(new InternalItemsQuery
  247. {
  248. SlugName = name,
  249. IncludeItemTypes = new[] { typeof(Person).Name }
  250. }).OfType<Person>().FirstOrDefault();
  251. if (result != null)
  252. {
  253. return result;
  254. }
  255. }
  256. return libraryManager.GetPerson(name);
  257. }
  258. protected string GetPathValue(int index)
  259. {
  260. var pathInfo = Parse(Request.PathInfo);
  261. var first = pathInfo[0];
  262. // backwards compatibility
  263. if (string.Equals(first, "mediabrowser", StringComparison.OrdinalIgnoreCase) ||
  264. string.Equals(first, "emby", StringComparison.OrdinalIgnoreCase))
  265. {
  266. index++;
  267. }
  268. return pathInfo[index];
  269. }
  270. private static List<string> Parse(string pathUri)
  271. {
  272. var actionParts = pathUri.Split(new[] { "://" }, StringSplitOptions.None);
  273. var pathInfo = actionParts[actionParts.Length - 1];
  274. var optionsPos = pathInfo.LastIndexOf('?');
  275. if (optionsPos != -1)
  276. {
  277. pathInfo = pathInfo.Substring(0, optionsPos);
  278. }
  279. var args = pathInfo.Split('/');
  280. return args.Skip(1).ToList();
  281. }
  282. /// <summary>
  283. /// Gets the name of the item by.
  284. /// </summary>
  285. /// <param name="name">The name.</param>
  286. /// <param name="type">The type.</param>
  287. /// <param name="libraryManager">The library manager.</param>
  288. /// <returns>Task{BaseItem}.</returns>
  289. protected BaseItem GetItemByName(string name, string type, ILibraryManager libraryManager)
  290. {
  291. BaseItem item;
  292. if (type.IndexOf("Person", StringComparison.OrdinalIgnoreCase) == 0)
  293. {
  294. item = GetPerson(name, libraryManager);
  295. }
  296. else if (type.IndexOf("Artist", StringComparison.OrdinalIgnoreCase) == 0)
  297. {
  298. item = GetArtist(name, libraryManager);
  299. }
  300. else if (type.IndexOf("Genre", StringComparison.OrdinalIgnoreCase) == 0)
  301. {
  302. item = GetGenre(name, libraryManager);
  303. }
  304. else if (type.IndexOf("MusicGenre", StringComparison.OrdinalIgnoreCase) == 0)
  305. {
  306. item = GetMusicGenre(name, libraryManager);
  307. }
  308. else if (type.IndexOf("GameGenre", StringComparison.OrdinalIgnoreCase) == 0)
  309. {
  310. item = GetGameGenre(name, libraryManager);
  311. }
  312. else if (type.IndexOf("Studio", StringComparison.OrdinalIgnoreCase) == 0)
  313. {
  314. item = GetStudio(name, libraryManager);
  315. }
  316. else if (type.IndexOf("Year", StringComparison.OrdinalIgnoreCase) == 0)
  317. {
  318. item = libraryManager.GetYear(int.Parse(name));
  319. }
  320. else
  321. {
  322. throw new ArgumentException();
  323. }
  324. return item;
  325. }
  326. }
  327. }