ApiClient.js 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  1. /**
  2. * Represents a javascript version of ApiClient.
  3. * This should be kept up to date with all possible api methods and parameters
  4. */
  5. var ApiClient = {
  6. serverProtocol: "http",
  7. /**
  8. * Gets or sets the host name of the server
  9. */
  10. serverHostName: "localhost",
  11. serverPortNumber: 8096,
  12. currentUserId: null,
  13. /**
  14. * Detects the hostname and port of MB server based on the current url
  15. */
  16. inferServerFromUrl: function () {
  17. var loc = window.location;
  18. ApiClient.serverProtocol = loc.protocol;
  19. ApiClient.serverHostName = loc.hostname;
  20. ApiClient.serverPortNumber = loc.port;
  21. },
  22. /**
  23. * Creates an api url based on a handler name and query string parameters
  24. * @param {String} name
  25. * @param {Object} params
  26. */
  27. getUrl: function (name, params) {
  28. if (!name) {
  29. throw new Error("Url name cannot be empty");
  30. }
  31. var url = ApiClient.serverProtocol + "//" + ApiClient.serverHostName + ":" + ApiClient.serverPortNumber + "/mediabrowser/" + name;
  32. if (params) {
  33. url += "?" + $.param(params);
  34. }
  35. return url;
  36. },
  37. /**
  38. * Returns the name of the current browser
  39. */
  40. getDeviceName: function () {
  41. /*if ($.browser.chrome) {
  42. return "Chrome";
  43. }
  44. if ($.browser.safari) {
  45. return "Safari";
  46. }
  47. if ($.browser.webkit) {
  48. return "WebKit";
  49. }
  50. if ($.browser.msie) {
  51. return "Internet Explorer";
  52. }
  53. if ($.browser.firefox) {
  54. return "Firefox";
  55. }
  56. if ($.browser.mozilla) {
  57. return "Firefox";
  58. }
  59. if ($.browser.opera) {
  60. return "Opera";
  61. }*/
  62. return "Web Browser";
  63. },
  64. getDeviceId: function() {
  65. return SHA1(navigator.userAgent + (navigator.cpuClass || ""));
  66. },
  67. /**
  68. * Creates a custom api url based on a handler name and query string parameters
  69. * @param {String} name
  70. * @param {Object} params
  71. */
  72. getCustomUrl: function (name, params) {
  73. if (!name) {
  74. throw new Error("Url name cannot be empty");
  75. }
  76. params = params || {};
  77. params.format = "json";
  78. var url = ApiClient.serverProtocol + "//" + ApiClient.serverHostName + ":" + ApiClient.serverPortNumber + "/mediabrowser/" + name;
  79. if (params) {
  80. url += "?" + $.param(params);
  81. }
  82. return url;
  83. },
  84. /**
  85. * Gets an item from the server
  86. * Omit itemId to get the root folder.
  87. */
  88. getItem: function (userId, itemId) {
  89. if (!userId) {
  90. throw new Error("null userId");
  91. }
  92. var url = ApiClient.getUrl("Users/" + userId + "/Items/" + itemId);
  93. return $.getJSON(url);
  94. },
  95. /**
  96. * Gets the root folder from the server
  97. */
  98. getRootFolder: function (userId) {
  99. return ApiClient.getItem(userId);
  100. },
  101. /**
  102. * Gets the current server status
  103. */
  104. getSystemInfo: function () {
  105. var url = ApiClient.getUrl("System/Info");
  106. return $.getJSON(url);
  107. },
  108. /**
  109. * Gets all cultures known to the server
  110. */
  111. getCultures: function () {
  112. var url = ApiClient.getUrl("Localization/cultures");
  113. return $.getJSON(url);
  114. },
  115. /**
  116. * Gets all countries known to the server
  117. */
  118. getCountries: function () {
  119. var url = ApiClient.getUrl("Localization/countries");
  120. return $.getJSON(url);
  121. },
  122. /**
  123. * Gets plugin security info
  124. */
  125. getPluginSecurityInfo: function () {
  126. var url = ApiClient.getUrl("Plugins/SecurityInfo");
  127. return $.getJSON(url);
  128. },
  129. /**
  130. * Gets the directory contents of a path on the server
  131. */
  132. getDirectoryContents: function (path, options) {
  133. if (!path) {
  134. throw new Error("null path");
  135. }
  136. options = options || {};
  137. options.path = path;
  138. var url = ApiClient.getUrl("Environment/DirectoryContents", options);
  139. return $.getJSON(url);
  140. },
  141. /**
  142. * Gets a list of physical drives from the server
  143. */
  144. getDrives: function () {
  145. var url = ApiClient.getUrl("Environment/Drives");
  146. return $.getJSON(url);
  147. },
  148. /**
  149. * Gets a list of network devices from the server
  150. */
  151. getNetworkDevices: function () {
  152. var url = ApiClient.getUrl("Environment/NetworkDevices");
  153. return $.getJSON(url);
  154. },
  155. /**
  156. * Cancels a package installation
  157. */
  158. cancelPackageInstallation: function (installationId) {
  159. if (!installationId) {
  160. throw new Error("null installationId");
  161. }
  162. var url = ApiClient.getUrl("Packages/Installing/" + id);
  163. return $.ajax({
  164. type: "DELETE",
  165. url: url,
  166. dataType: "json"
  167. });
  168. },
  169. /**
  170. * Installs or updates a new plugin
  171. */
  172. installPlugin: function (name, updateClass, version) {
  173. if (!name) {
  174. throw new Error("null name");
  175. }
  176. if (!updateClass) {
  177. throw new Error("null updateClass");
  178. }
  179. var options = {
  180. updateClass: updateClass
  181. };
  182. if (version) {
  183. options.version = version;
  184. }
  185. var url = ApiClient.getUrl("Packages/Installed/" + name, options);
  186. return $.post(url);
  187. },
  188. /**
  189. * Instructs the server to perform a pending kernel reload or app restart.
  190. * If a restart is not currently required, nothing will happen.
  191. */
  192. performPendingRestart: function () {
  193. var url = ApiClient.getUrl("System/Restart");
  194. return $.post(url);
  195. },
  196. /**
  197. * Gets information about an installable package
  198. */
  199. getPackageInfo: function (name) {
  200. if (!name) {
  201. throw new Error("null name");
  202. }
  203. var url = ApiClient.getUrl("Packages/" + name);
  204. return $.getJSON(url);
  205. },
  206. /**
  207. * Gets the latest available application update (if any)
  208. */
  209. getAvailableApplicationUpdate: function () {
  210. var url = ApiClient.getUrl("Packages/Updates", { PackageType: "System" });
  211. return $.getJSON(url);
  212. },
  213. /**
  214. * Gets the latest available plugin updates (if any)
  215. */
  216. getAvailablePluginUpdates: function () {
  217. var url = ApiClient.getUrl("Packages/Updates", { PackageType: "UserInstalled" });
  218. return $.getJSON(url);
  219. },
  220. /**
  221. * Gets the virtual folder for a view. Specify a userId to get a user view, or omit for the default view.
  222. */
  223. getVirtualFolders: function (userId) {
  224. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  225. url = ApiClient.getUrl(url);
  226. return $.getJSON(url);
  227. },
  228. /**
  229. * Gets all the paths of the locations in the physical root.
  230. */
  231. getPhysicalPaths: function () {
  232. var url = ApiClient.getUrl("Library/PhysicalPaths");
  233. return $.getJSON(url);
  234. },
  235. /**
  236. * Gets the current server configuration
  237. */
  238. getServerConfiguration: function () {
  239. var url = ApiClient.getUrl("System/Configuration");
  240. return $.getJSON(url);
  241. },
  242. /**
  243. * Gets the server's scheduled tasks
  244. */
  245. getScheduledTasks: function () {
  246. var url = ApiClient.getUrl("ScheduledTasks");
  247. return $.getJSON(url);
  248. },
  249. /**
  250. * Starts a scheduled task
  251. */
  252. startScheduledTask: function (id) {
  253. if (!id) {
  254. throw new Error("null id");
  255. }
  256. var url = ApiClient.getUrl("ScheduledTasks/Running/" + id);
  257. return $.post(url);
  258. },
  259. /**
  260. * Gets a scheduled task
  261. */
  262. getScheduledTask: function (id) {
  263. if (!id) {
  264. throw new Error("null id");
  265. }
  266. var url = ApiClient.getUrl("ScheduledTasks/" + id);
  267. return $.getJSON(url);
  268. },
  269. /**
  270. * Stops a scheduled task
  271. */
  272. stopScheduledTask: function (id) {
  273. if (!id) {
  274. throw new Error("null id");
  275. }
  276. var url = ApiClient.getUrl("ScheduledTasks/Running/" + id);
  277. return $.ajax({
  278. type: "DELETE",
  279. url: url,
  280. dataType: "json"
  281. });
  282. },
  283. /**
  284. * Gets the configuration of a plugin
  285. * @param {String} Id
  286. */
  287. getPluginConfiguration: function (id) {
  288. if (!id) {
  289. throw new Error("null Id");
  290. }
  291. var url = ApiClient.getUrl("Plugins/" + id + "/Configuration");
  292. return $.getJSON(url);
  293. },
  294. /**
  295. * Gets a list of plugins that are available to be installed
  296. */
  297. getAvailablePlugins: function () {
  298. var url = ApiClient.getUrl("Packages", { PackageType: "UserInstalled" });
  299. return $.getJSON(url);
  300. },
  301. /**
  302. * Uninstalls a plugin
  303. * @param {String} Id
  304. */
  305. uninstallPlugin: function (id) {
  306. if (!id) {
  307. throw new Error("null Id");
  308. }
  309. var url = ApiClient.getUrl("Plugins/" + id);
  310. return $.ajax({
  311. type: "DELETE",
  312. url: url,
  313. dataType: "json"
  314. });
  315. },
  316. /**
  317. * Removes a virtual folder from either the default view or a user view
  318. * @param {String} name
  319. */
  320. removeVirtualFolder: function (name, userId) {
  321. if (!name) {
  322. throw new Error("null name");
  323. }
  324. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  325. url += "/" + name;
  326. url = ApiClient.getUrl(url);
  327. return $.ajax({
  328. type: "DELETE",
  329. url: url,
  330. dataType: "json"
  331. });
  332. },
  333. /**
  334. * Adds a virtual folder to either the default view or a user view
  335. * @param {String} name
  336. */
  337. addVirtualFolder: function (name, userId) {
  338. if (!name) {
  339. throw new Error("null name");
  340. }
  341. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  342. url += "/" + name;
  343. url = ApiClient.getUrl(url);
  344. return $.post(url);
  345. },
  346. /**
  347. * Renames a virtual folder, within either the default view or a user view
  348. * @param {String} name
  349. */
  350. renameVirtualFolder: function (name, newName, userId) {
  351. if (!name) {
  352. throw new Error("null name");
  353. }
  354. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  355. url += "/" + name + "/Name";
  356. url = ApiClient.getUrl(url, { newName: newName });
  357. return $.post(url);
  358. },
  359. /**
  360. * Adds an additional mediaPath to an existing virtual folder, within either the default view or a user view
  361. * @param {String} name
  362. */
  363. addMediaPath: function (virtualFolderName, mediaPath, userId) {
  364. if (!virtualFolderName) {
  365. throw new Error("null virtualFolderName");
  366. }
  367. if (!mediaPath) {
  368. throw new Error("null mediaPath");
  369. }
  370. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  371. url += "/" + virtualFolderName + "/Paths";
  372. url = ApiClient.getUrl(url, { path: mediaPath });
  373. return $.post(url);
  374. },
  375. /**
  376. * Removes a media path from a virtual folder, within either the default view or a user view
  377. * @param {String} name
  378. */
  379. removeMediaPath: function (virtualFolderName, mediaPath, userId) {
  380. if (!virtualFolderName) {
  381. throw new Error("null virtualFolderName");
  382. }
  383. if (!mediaPath) {
  384. throw new Error("null mediaPath");
  385. }
  386. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  387. url += "/" + virtualFolderName + "/Paths";
  388. url = ApiClient.getUrl(url, { path: mediaPath });
  389. return $.ajax({
  390. type: "DELETE",
  391. url: url,
  392. dataType: "json"
  393. });
  394. },
  395. /**
  396. * Deletes a user
  397. * @param {String} id
  398. */
  399. deleteUser: function (id) {
  400. if (!id) {
  401. throw new Error("null id");
  402. }
  403. var url = ApiClient.getUrl("Users/" + id);
  404. return $.ajax({
  405. type: "DELETE",
  406. url: url,
  407. dataType: "json"
  408. });
  409. },
  410. /**
  411. * Deletes a user image
  412. * @param {String} userId
  413. * @param {String} imageType The type of image to delete, based on the server-side ImageType enum.
  414. */
  415. deleteUserImage: function (userId, imageType) {
  416. if (!userId) {
  417. throw new Error("null userId");
  418. }
  419. if (!imageType) {
  420. throw new Error("null imageType");
  421. }
  422. var url = ApiClient.getUrl("Users/" + userId + "/Images/" + imageType);
  423. return $.ajax({
  424. type: "DELETE",
  425. url: url,
  426. dataType: "json"
  427. });
  428. },
  429. /**
  430. * Uploads a user image
  431. * @param {String} userId
  432. * @param {String} imageType The type of image to delete, based on the server-side ImageType enum.
  433. * @param {Object} file The file from the input element
  434. */
  435. uploadUserImage: function (userId, imageType, file) {
  436. if (!userId) {
  437. throw new Error("null userId");
  438. }
  439. if (!imageType) {
  440. throw new Error("null imageType");
  441. }
  442. if (!file || !file.type.match('image.*')) {
  443. throw new Error("File must be an image.");
  444. }
  445. var deferred = $.Deferred();
  446. var reader = new FileReader();
  447. reader.onerror = function () {
  448. deferred.reject();
  449. };
  450. reader.onabort = function () {
  451. deferred.reject();
  452. };
  453. // Closure to capture the file information.
  454. reader.onload = function (e) {
  455. var data = window.btoa(e.target.result);
  456. var url = ApiClient.getUrl("Users/" + userId + "/Images/" + imageType);
  457. $.ajax({
  458. type: "POST",
  459. url: url,
  460. data: data,
  461. contentType: "image/" + file.name.substring(file.name.lastIndexOf('.') + 1)
  462. }).done(function (result) {
  463. deferred.resolveWith(null, [result]);
  464. }).fail(function () {
  465. deferred.reject();
  466. });
  467. };
  468. // Read in the image file as a data URL.
  469. reader.readAsBinaryString(file);
  470. return deferred.promise();
  471. },
  472. /**
  473. * Gets the list of installed plugins on the server
  474. */
  475. getInstalledPlugins: function () {
  476. var url = ApiClient.getUrl("Plugins");
  477. return $.getJSON(url);
  478. },
  479. /**
  480. * Gets a user by id
  481. * @param {String} id
  482. */
  483. getUser: function (id) {
  484. if (!id) {
  485. throw new Error("Must supply a userId");
  486. }
  487. var url = ApiClient.getUrl("Users/" + id);
  488. return $.getJSON(url);
  489. },
  490. /**
  491. * Gets a studio
  492. */
  493. getStudio: function (name) {
  494. if (!name) {
  495. throw new Error("null name");
  496. }
  497. var url = ApiClient.getUrl("Studios/" + name);
  498. return $.getJSON(url);
  499. },
  500. /**
  501. * Gets a genre
  502. */
  503. getGenre: function (name) {
  504. if (!name) {
  505. throw new Error("null name");
  506. }
  507. var url = ApiClient.getUrl("Genres/" + name);
  508. return $.getJSON(url);
  509. },
  510. /**
  511. * Gets a year
  512. */
  513. getYear: function (year) {
  514. if (!year) {
  515. throw new Error("null year");
  516. }
  517. var url = ApiClient.getUrl("Years/" + year);
  518. return $.getJSON(url);
  519. },
  520. /**
  521. * Gets a Person
  522. */
  523. getPerson: function (name) {
  524. if (!name) {
  525. throw new Error("null name");
  526. }
  527. var url = ApiClient.getUrl("Persons/" + name);
  528. return $.getJSON(url);
  529. },
  530. /**
  531. * Gets weather info
  532. * @param {String} location - us zip code / city, state, country / city, country
  533. * Omit location to get weather info using stored server configuration value
  534. */
  535. getWeatherInfo: function (location) {
  536. var url = ApiClient.getUrl("weather", {
  537. location: location
  538. });
  539. return $.getJSON(url);
  540. },
  541. /**
  542. * Gets all users from the server
  543. */
  544. getAllUsers: function () {
  545. var url = ApiClient.getUrl("users");
  546. return $.getJSON(url);
  547. },
  548. /**
  549. * Gets all available parental ratings from the server
  550. */
  551. getParentalRatings: function () {
  552. var url = ApiClient.getUrl("Localization/ParentalRatings");
  553. return $.getJSON(url);
  554. },
  555. /**
  556. * Gets a list of all available conrete BaseItem types from the server
  557. */
  558. getItemTypes: function (options) {
  559. var url = ApiClient.getUrl("Library/ItemTypes", options);
  560. return $.getJSON(url);
  561. },
  562. /**
  563. * Constructs a url for a user image
  564. * @param {String} userId
  565. * @param {Object} options
  566. * Options supports the following properties:
  567. * width - download the image at a fixed width
  568. * height - download the image at a fixed height
  569. * maxWidth - download the image at a maxWidth
  570. * maxHeight - download the image at a maxHeight
  571. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  572. * For best results do not specify both width and height together, as aspect ratio might be altered.
  573. */
  574. getUserImageUrl: function (userId, options) {
  575. if (!userId) {
  576. throw new Error("null userId");
  577. }
  578. options = options || {
  579. };
  580. var url = "Users/" + userId + "/Images/" + options.type;
  581. if (options.index != null) {
  582. url += "/" + options.index;
  583. }
  584. // Don't put these on the query string
  585. delete options.type;
  586. delete options.index;
  587. return ApiClient.getUrl(url, options);
  588. },
  589. /**
  590. * Constructs a url for a person image
  591. * @param {String} name
  592. * @param {Object} options
  593. * Options supports the following properties:
  594. * width - download the image at a fixed width
  595. * height - download the image at a fixed height
  596. * maxWidth - download the image at a maxWidth
  597. * maxHeight - download the image at a maxHeight
  598. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  599. * For best results do not specify both width and height together, as aspect ratio might be altered.
  600. */
  601. getPersonImageUrl: function (name, options) {
  602. if (!name) {
  603. throw new Error("null name");
  604. }
  605. options = options || {
  606. };
  607. var url = "Persons/" + name + "/Images/" + options.type;
  608. if (options.index != null) {
  609. url += "/" + options.index;
  610. }
  611. // Don't put these on the query string
  612. delete options.type;
  613. delete options.index;
  614. return ApiClient.getUrl(url, options);
  615. },
  616. /**
  617. * Constructs a url for a year image
  618. * @param {String} year
  619. * @param {Object} options
  620. * Options supports the following properties:
  621. * width - download the image at a fixed width
  622. * height - download the image at a fixed height
  623. * maxWidth - download the image at a maxWidth
  624. * maxHeight - download the image at a maxHeight
  625. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  626. * For best results do not specify both width and height together, as aspect ratio might be altered.
  627. */
  628. getYearImageUrl: function (year, options) {
  629. if (!year) {
  630. throw new Error("null year");
  631. }
  632. options = options || {
  633. };
  634. var url = "Years/" + year + "/Images/" + options.type;
  635. if (options.index != null) {
  636. url += "/" + options.index;
  637. }
  638. // Don't put these on the query string
  639. delete options.type;
  640. delete options.index;
  641. return ApiClient.getUrl(url, options);
  642. },
  643. /**
  644. * Constructs a url for a genre image
  645. * @param {String} name
  646. * @param {Object} options
  647. * Options supports the following properties:
  648. * width - download the image at a fixed width
  649. * height - download the image at a fixed height
  650. * maxWidth - download the image at a maxWidth
  651. * maxHeight - download the image at a maxHeight
  652. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  653. * For best results do not specify both width and height together, as aspect ratio might be altered.
  654. */
  655. getGenreImageUrl: function (name, options) {
  656. if (!name) {
  657. throw new Error("null name");
  658. }
  659. options = options || {
  660. };
  661. var url = "Genres/" + name + "/Images/" + options.type;
  662. if (options.index != null) {
  663. url += "/" + options.index;
  664. }
  665. // Don't put these on the query string
  666. delete options.type;
  667. delete options.index;
  668. return ApiClient.getUrl(url, options);
  669. },
  670. /**
  671. * Constructs a url for a genre image
  672. * @param {String} name
  673. * @param {Object} options
  674. * Options supports the following properties:
  675. * width - download the image at a fixed width
  676. * height - download the image at a fixed height
  677. * maxWidth - download the image at a maxWidth
  678. * maxHeight - download the image at a maxHeight
  679. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  680. * For best results do not specify both width and height together, as aspect ratio might be altered.
  681. */
  682. getStudioImageUrl: function (name, options) {
  683. if (!name) {
  684. throw new Error("null name");
  685. }
  686. options = options || {
  687. };
  688. var url = "Studios/" + name + "/Images/" + options.type;
  689. if (options.index != null) {
  690. url += "/" + options.index;
  691. }
  692. // Don't put these on the query string
  693. delete options.type;
  694. delete options.index;
  695. return ApiClient.getUrl(url, options);
  696. },
  697. /**
  698. * Constructs a url for an item image
  699. * @param {String} itemId
  700. * @param {Object} options
  701. * Options supports the following properties:
  702. * type - Primary, logo, backdrop, etc. See the server-side enum ImageType
  703. * index - When downloading a backdrop, use this to specify which one (omitting is equivalent to zero)
  704. * width - download the image at a fixed width
  705. * height - download the image at a fixed height
  706. * maxWidth - download the image at a maxWidth
  707. * maxHeight - download the image at a maxHeight
  708. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  709. * For best results do not specify both width and height together, as aspect ratio might be altered.
  710. */
  711. getImageUrl: function (itemId, options) {
  712. if (!itemId) {
  713. throw new Error("itemId cannot be empty");
  714. }
  715. options = options || {
  716. };
  717. var url = "Items/" + itemId + "/Images/" + options.type;
  718. if (options.index != null) {
  719. url += "/" + options.index;
  720. }
  721. // Don't put these on the query string
  722. delete options.type;
  723. delete options.index;
  724. return ApiClient.getUrl(url, options);
  725. },
  726. /**
  727. * Constructs a url for an item logo image
  728. * If the item doesn't have a logo, it will inherit a logo from a parent
  729. * @param {Object} item A BaseItem
  730. * @param {Object} options
  731. * Options supports the following properties:
  732. * width - download the image at a fixed width
  733. * height - download the image at a fixed height
  734. * maxWidth - download the image at a maxWidth
  735. * maxHeight - download the image at a maxHeight
  736. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  737. * For best results do not specify both width and height together, as aspect ratio might be altered.
  738. */
  739. getLogoImageUrl: function (item, options) {
  740. if (!item) {
  741. throw new Error("null item");
  742. }
  743. options = options || {
  744. };
  745. options.imageType = "logo";
  746. var logoItemId = item.HasLogo ? item.Id : item.ParentLogoItemId;
  747. return logoItemId ? ApiClient.getImageUrl(logoItemId, options) : null;
  748. },
  749. /**
  750. * Constructs an array of backdrop image url's for an item
  751. * If the item doesn't have any backdrops, it will inherit them from a parent
  752. * @param {Object} item A BaseItem
  753. * @param {Object} options
  754. * Options supports the following properties:
  755. * width - download the image at a fixed width
  756. * height - download the image at a fixed height
  757. * maxWidth - download the image at a maxWidth
  758. * maxHeight - download the image at a maxHeight
  759. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  760. * For best results do not specify both width and height together, as aspect ratio might be altered.
  761. */
  762. getBackdropImageUrl: function (item, options) {
  763. if (!item) {
  764. throw new Error("null item");
  765. }
  766. options = options || {
  767. };
  768. options.imageType = "backdrop";
  769. var backdropItemId;
  770. var backdropCount;
  771. if (!item.BackdropCount) {
  772. backdropItemId = item.ParentBackdropItemId;
  773. backdropCount = item.ParentBackdropCount || 0;
  774. } else {
  775. backdropItemId = item.Id;
  776. backdropCount = item.BackdropCount;
  777. }
  778. if (!backdropItemId) {
  779. return [];
  780. }
  781. var files = [];
  782. for (var i = 0; i < backdropCount; i++) {
  783. options.imageIndex = i;
  784. files[i] = ApiClient.getImageUrl(backdropItemId, options);
  785. }
  786. return files;
  787. },
  788. /**
  789. * Authenticates a user
  790. * @param {String} userId
  791. * @param {String} password
  792. */
  793. authenticateUser: function (userId, password) {
  794. if (!userId) {
  795. throw new Error("null userId");
  796. }
  797. var url = ApiClient.getUrl("Users/" + userId + "/authenticate");
  798. var postData = {
  799. password: SHA1(password || "")
  800. };
  801. return $.ajax({
  802. type: "POST",
  803. url: url,
  804. data: JSON.stringify(postData),
  805. dataType: "json",
  806. contentType: "application/json"
  807. });
  808. },
  809. /**
  810. * Updates a user's password
  811. * @param {String} userId
  812. * @param {String} currentPassword
  813. * @param {String} newPassword
  814. */
  815. updateUserPassword: function (userId, currentPassword, newPassword) {
  816. if (!userId) {
  817. throw new Error("null userId");
  818. }
  819. var url = ApiClient.getUrl("Users/" + userId + "/Password");
  820. var postData = {
  821. };
  822. postData.currentPassword = SHA1(currentPassword);
  823. if (newPassword) {
  824. postData.newPassword = newPassword;
  825. }
  826. return $.post(url, postData);
  827. },
  828. /**
  829. * Resets a user's password
  830. * @param {String} userId
  831. */
  832. resetUserPassword: function (userId) {
  833. if (!userId) {
  834. throw new Error("null userId");
  835. }
  836. var url = ApiClient.getUrl("Users/" + userId + "/Password");
  837. var postData = {
  838. };
  839. postData.resetPassword = true;
  840. return $.post(url, postData);
  841. },
  842. /**
  843. * Updates the server's configuration
  844. * @param {Object} configuration
  845. */
  846. updateServerConfiguration: function (configuration) {
  847. if (!configuration) {
  848. throw new Error("null configuration");
  849. }
  850. var url = ApiClient.getUrl("System/Configuration");
  851. return $.ajax({
  852. type: "POST",
  853. url: url,
  854. data: JSON.stringify(configuration),
  855. dataType: "json",
  856. contentType: "application/json"
  857. });
  858. },
  859. /**
  860. * Updates plugin security info
  861. */
  862. updatePluginSecurityInfo: function (info) {
  863. var url = ApiClient.getUrl("Plugins/SecurityInfo");
  864. return $.ajax({
  865. type: "POST",
  866. url: url,
  867. data: JSON.stringify(info),
  868. dataType: "json",
  869. contentType: "application/json"
  870. });
  871. },
  872. /**
  873. * Creates a user
  874. * @param {Object} user
  875. */
  876. createUser: function (user) {
  877. if (!user) {
  878. throw new Error("null user");
  879. }
  880. var url = ApiClient.getUrl("Users");
  881. return $.ajax({
  882. type: "POST",
  883. url: url,
  884. data: JSON.stringify(user),
  885. dataType: "json",
  886. contentType: "application/json"
  887. });
  888. },
  889. /**
  890. * Updates a user
  891. * @param {Object} user
  892. */
  893. updateUser: function (user) {
  894. if (!user) {
  895. throw new Error("null user");
  896. }
  897. var url = ApiClient.getUrl("Users/" + user.Id);
  898. return $.ajax({
  899. type: "POST",
  900. url: url,
  901. data: JSON.stringify(user),
  902. dataType: "json",
  903. contentType: "application/json"
  904. });
  905. },
  906. /**
  907. * Updates the Triggers for a ScheduledTask
  908. * @param {String} id
  909. * @param {Object} triggers
  910. */
  911. updateScheduledTaskTriggers: function (id, triggers) {
  912. if (!id) {
  913. throw new Error("null id");
  914. }
  915. if (!triggers) {
  916. throw new Error("null triggers");
  917. }
  918. var url = ApiClient.getUrl("ScheduledTasks/" + id + "/Triggers");
  919. return $.ajax({
  920. type: "POST",
  921. url: url,
  922. data: JSON.stringify(triggers),
  923. dataType: "json",
  924. contentType: "application/json"
  925. });
  926. },
  927. /**
  928. * Updates a plugin's configuration
  929. * @param {String} Id
  930. * @param {Object} configuration
  931. */
  932. updatePluginConfiguration: function (id, configuration) {
  933. if (!id) {
  934. throw new Error("null Id");
  935. }
  936. if (!configuration) {
  937. throw new Error("null configuration");
  938. }
  939. var url = ApiClient.getUrl("Plugins/" + id + "/Configuration");
  940. return $.ajax({
  941. type: "POST",
  942. url: url,
  943. data: JSON.stringify(configuration),
  944. dataType: "json",
  945. contentType: "application/json"
  946. });
  947. },
  948. /**
  949. * Gets items based on a query, typicall for children of a folder
  950. * @param {String} userId
  951. * @param {Object} options
  952. * Options accepts the following properties:
  953. * itemId - Localize the search to a specific folder (root if omitted)
  954. * startIndex - Use for paging
  955. * limit - Use to limit results to a certain number of items
  956. * filter - Specify one or more ItemFilters, comma delimeted (see server-side enum)
  957. * sortBy - Specify an ItemSortBy (comma-delimeted list see server-side enum)
  958. * sortOrder - ascending/descending
  959. * fields - additional fields to include aside from basic info. This is a comma delimited list. See server-side enum ItemFields.
  960. * index - the name of the dynamic, localized index function
  961. * dynamicSortBy - the name of the dynamic localized sort function
  962. * recursive - Whether or not the query should be recursive
  963. * searchTerm - search term to use as a filter
  964. */
  965. getItems: function (userId, options) {
  966. if (!userId) {
  967. throw new Error("null userId");
  968. }
  969. return $.getJSON(ApiClient.getUrl("Users/" + userId + "/Items", options));
  970. },
  971. /**
  972. * Marks an item as played or unplayed
  973. * This should not be used to update playstate following playback.
  974. * There are separate playstate check-in methods for that. This should be used for a
  975. * separate option to reset playstate.
  976. * @param {String} userId
  977. * @param {String} itemId
  978. * @param {Boolean} wasPlayed
  979. */
  980. updatePlayedStatus: function (userId, itemId, wasPlayed) {
  981. if (!userId) {
  982. throw new Error("null userId");
  983. }
  984. if (!itemId) {
  985. throw new Error("null itemId");
  986. }
  987. var url = "Users/" + userId + "/PlayedItems/" + itemId;
  988. var method = wasPlayed ? "POST" : "DELETE";
  989. return $.ajax({
  990. type: method,
  991. url: url,
  992. dataType: "json"
  993. });
  994. },
  995. /**
  996. * Updates a user's favorite status for an item and returns the updated UserItemData object.
  997. * @param {String} userId
  998. * @param {String} itemId
  999. * @param {Boolean} isFavorite
  1000. */
  1001. updateFavoriteStatus: function (userId, itemId, isFavorite) {
  1002. if (!userId) {
  1003. throw new Error("null userId");
  1004. }
  1005. if (!itemId) {
  1006. throw new Error("null itemId");
  1007. }
  1008. var url = "Users/" + userId + "/FavoriteItems/" + itemId;
  1009. var method = isFavorite ? "POST" : "DELETE";
  1010. return $.ajax({
  1011. type: method,
  1012. url: url,
  1013. dataType: "json"
  1014. });
  1015. },
  1016. /**
  1017. * Updates a user's personal rating for an item
  1018. * @param {String} userId
  1019. * @param {String} itemId
  1020. * @param {Boolean} likes
  1021. */
  1022. updateUserItemRating: function (userId, itemId, likes) {
  1023. if (!userId) {
  1024. throw new Error("null userId");
  1025. }
  1026. if (!itemId) {
  1027. throw new Error("null itemId");
  1028. }
  1029. var url = ApiClient.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating", {
  1030. likes: likes
  1031. });
  1032. return $.post(url);
  1033. },
  1034. /**
  1035. * Clears a user's personal rating for an item
  1036. * @param {String} userId
  1037. * @param {String} itemId
  1038. */
  1039. clearUserItemRating: function (userId, itemId) {
  1040. if (!userId) {
  1041. throw new Error("null userId");
  1042. }
  1043. if (!itemId) {
  1044. throw new Error("null itemId");
  1045. }
  1046. var url = ApiClient.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating");
  1047. return $.ajax({
  1048. type: "DELETE",
  1049. url: url,
  1050. dataType: "json"
  1051. });
  1052. }
  1053. };
  1054. // Do this initially. The consumer can always override later
  1055. ApiClient.inferServerFromUrl();
  1056. $(document).ajaxSend(function (event, jqXHR) {
  1057. if (ApiClient.currentUserId) {
  1058. var auth = 'MediaBrowser UserId="' + ApiClient.currentUserId + '", Client="Dashboard", Device="' + ApiClient.getDeviceName() + '", DeviceId="' + ApiClient.getDeviceId() + '"';
  1059. jqXHR.setRequestHeader("Authorization", auth);
  1060. }
  1061. });