ApiClient.js 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245
  1. if (!window.MediaBrowser) {
  2. window.MediaBrowser = {};
  3. }
  4. MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout) {
  5. /**
  6. * Creates a new api client instance
  7. * @param {String} serverProtocol
  8. * @param {String} serverHostName
  9. * @param {String} serverPortNumber
  10. * @param {String} clientName
  11. */
  12. return function (serverProtocol, serverHostName, serverPortNumber, clientName) {
  13. if (!serverProtocol) {
  14. throw new Error("Must supply a serverProtocol, e.g. http:");
  15. }
  16. if (!serverHostName) {
  17. throw new Error("Must supply serverHostName, e.g. 192.168.1.1 or myServerName");
  18. }
  19. if (!serverPortNumber) {
  20. throw new Error("Must supply a serverPortNumber");
  21. }
  22. var self = this;
  23. var deviceName = "Web Browser";
  24. var deviceId = MediaBrowser.SHA1(navigator.userAgent + (navigator.cpuClass || ""));
  25. var currentUserId;
  26. var webSocket;
  27. /**
  28. * Gets the server host name.
  29. */
  30. self.serverHostName = function () {
  31. return serverHostName;
  32. };
  33. /**
  34. * Gets the server port number.
  35. */
  36. self.serverPortNumber = function () {
  37. return serverPortNumber;
  38. };
  39. /**
  40. * Gets or sets the current user id.
  41. */
  42. self.currentUserId = function (val) {
  43. if (val !== undefined) {
  44. currentUserId = val;
  45. } else {
  46. return currentUserId;
  47. }
  48. };
  49. deviceName = (function () {
  50. var name = "";
  51. if ($.browser.chrome) {
  52. name = "Chrome";
  53. }
  54. else if ($.browser.safari) {
  55. name = "Safari";
  56. }
  57. else if ($.browser.webkit) {
  58. name = "WebKit";
  59. }
  60. else if ($.browser.msie) {
  61. name = "Internet Explorer";
  62. }
  63. else if ($.browser.firefox || $.browser.mozilla) {
  64. name = "Firefox";
  65. }
  66. else if ($.browser.opera) {
  67. name = "Opera";
  68. }
  69. else {
  70. name = "Web Browser";
  71. }
  72. if ($.browser.ipad) {
  73. name += " Ipad";
  74. }
  75. else if ($.browser.iphone) {
  76. name += " Iphone";
  77. }
  78. else if ($.browser.android) {
  79. name += " Android";
  80. }
  81. return name;
  82. }());
  83. function encodeName(name) {
  84. return encodeURIComponent(name).replace("'", '%27');
  85. }
  86. /**
  87. * Wraps around jQuery ajax methods to add additional info to the request.
  88. */
  89. self.ajax = function (request) {
  90. if (!request) {
  91. throw new Error("Request cannot be null");
  92. }
  93. if (clientName) {
  94. var auth = 'MediaBrowser Client="' + clientName + '", Device="' + deviceName + '", DeviceId="' + deviceId + '"';
  95. if (currentUserId) {
  96. auth += ', UserId="' + currentUserId + '"';
  97. }
  98. request.headers = {
  99. Authorization: auth
  100. };
  101. }
  102. return $.ajax(request);
  103. };
  104. /**
  105. * Creates an api url based on a handler name and query string parameters
  106. * @param {String} name
  107. * @param {Object} params
  108. */
  109. self.getUrl = function (name, params) {
  110. if (!name) {
  111. throw new Error("Url name cannot be empty");
  112. }
  113. var url = serverProtocol + "//" + serverHostName + ":" + serverPortNumber + "/mediabrowser/" + name;
  114. if (params) {
  115. url += "?" + $.param(params);
  116. }
  117. return url;
  118. };
  119. self.openWebSocket = function (port) {
  120. var url = "ws://" + serverHostName + ":" + port + "/mediabrowser";
  121. webSocket = new WebSocket(url);
  122. webSocket.onmessage = function (msg) {
  123. msg = JSON.parse(msg.data);
  124. $(self).trigger("websocketmessage", [msg]);
  125. };
  126. webSocket.onopen = function () {
  127. setTimeout(function () {
  128. $(self).trigger("websocketopen");
  129. }, 500);
  130. };
  131. webSocket.onerror = function () {
  132. setTimeout(function () {
  133. $(self).trigger("websocketerror");
  134. }, 0);
  135. };
  136. webSocket.onclose = function () {
  137. setTimeout(function () {
  138. $(self).trigger("websocketclose");
  139. }, 0);
  140. };
  141. };
  142. self.sendWebSocketMessage = function (name, data) {
  143. var msg = { MessageType: name };
  144. if (data) {
  145. msg.Data = data;
  146. }
  147. msg = JSON.stringify(msg);
  148. webSocket.send(msg);
  149. };
  150. self.isWebSocketOpen = function () {
  151. return webSocket && webSocket.readyState === WebSocket.OPEN;
  152. };
  153. self.isWebSocketOpenOrConnecting = function () {
  154. return webSocket && (webSocket.readyState === WebSocket.OPEN || webSocket.readyState === WebSocket.CONNECTING);
  155. };
  156. /**
  157. * Gets an item from the server
  158. * Omit itemId to get the root folder.
  159. */
  160. self.getItem = function (userId, itemId) {
  161. if (!userId) {
  162. throw new Error("null userId");
  163. }
  164. var url = self.getUrl("Users/" + userId + "/Items/" + itemId);
  165. return self.ajax({
  166. type: "GET",
  167. url: url,
  168. dataType: "json"
  169. });
  170. };
  171. /**
  172. * Gets the root folder from the server
  173. */
  174. self.getRootFolder = function (userId) {
  175. if (!userId) {
  176. throw new Error("null userId");
  177. }
  178. var url = self.getUrl("Users/" + userId + "/Items/Root");
  179. return self.ajax({
  180. type: "GET",
  181. url: url,
  182. dataType: "json"
  183. });
  184. };
  185. /**
  186. * Gets the current server status
  187. */
  188. self.getSystemInfo = function () {
  189. var url = self.getUrl("System/Info");
  190. return self.ajax({
  191. type: "GET",
  192. url: url,
  193. dataType: "json"
  194. });
  195. };
  196. /**
  197. * Gets all cultures known to the server
  198. */
  199. self.getCultures = function () {
  200. var url = self.getUrl("Localization/cultures");
  201. return self.ajax({
  202. type: "GET",
  203. url: url,
  204. dataType: "json"
  205. });
  206. };
  207. /**
  208. * Gets all countries known to the server
  209. */
  210. self.getCountries = function () {
  211. var url = self.getUrl("Localization/countries");
  212. return self.ajax({
  213. type: "GET",
  214. url: url,
  215. dataType: "json"
  216. });
  217. };
  218. /**
  219. * Gets plugin security info
  220. */
  221. self.getPluginSecurityInfo = function () {
  222. var url = self.getUrl("Plugins/SecurityInfo");
  223. return self.ajax({
  224. type: "GET",
  225. url: url,
  226. dataType: "json"
  227. });
  228. };
  229. /**
  230. * Gets the directory contents of a path on the server
  231. */
  232. self.getDirectoryContents = function (path, options) {
  233. if (!path) {
  234. throw new Error("null path");
  235. }
  236. options = options || {};
  237. options.path = path;
  238. var url = self.getUrl("Environment/DirectoryContents", options);
  239. return self.ajax({
  240. type: "GET",
  241. url: url,
  242. dataType: "json"
  243. });
  244. };
  245. /**
  246. * Gets a list of physical drives from the server
  247. */
  248. self.getDrives = function () {
  249. var url = self.getUrl("Environment/Drives");
  250. return self.ajax({
  251. type: "GET",
  252. url: url,
  253. dataType: "json"
  254. });
  255. };
  256. /**
  257. * Gets a list of network devices from the server
  258. */
  259. self.getNetworkDevices = function () {
  260. var url = self.getUrl("Environment/NetworkDevices");
  261. return self.ajax({
  262. type: "GET",
  263. url: url,
  264. dataType: "json"
  265. });
  266. };
  267. /**
  268. * Cancels a package installation
  269. */
  270. self.cancelPackageInstallation = function (installationId) {
  271. if (!installationId) {
  272. throw new Error("null installationId");
  273. }
  274. var url = self.getUrl("Packages/Installing/" + id);
  275. return self.ajax({
  276. type: "DELETE",
  277. url: url,
  278. dataType: "json"
  279. });
  280. };
  281. /**
  282. * Installs or updates a new plugin
  283. */
  284. self.installPlugin = function (name, updateClass, version) {
  285. if (!name) {
  286. throw new Error("null name");
  287. }
  288. if (!updateClass) {
  289. throw new Error("null updateClass");
  290. }
  291. var options = {
  292. updateClass: updateClass
  293. };
  294. if (version) {
  295. options.version = version;
  296. }
  297. var url = self.getUrl("Packages/Installed/" + name, options);
  298. return self.ajax({
  299. type: "POST",
  300. url: url
  301. });
  302. };
  303. /**
  304. * Instructs the server to perform a pending kernel reload or app restart.
  305. * If a restart is not currently required, nothing will happen.
  306. */
  307. self.performPendingRestart = function () {
  308. var url = self.getUrl("System/Restart");
  309. return self.ajax({
  310. type: "POST",
  311. url: url
  312. });
  313. };
  314. /**
  315. * Gets information about an installable package
  316. */
  317. self.getPackageInfo = function (name) {
  318. if (!name) {
  319. throw new Error("null name");
  320. }
  321. var url = self.getUrl("Packages/" + name);
  322. return self.ajax({
  323. type: "GET",
  324. url: url,
  325. dataType: "json"
  326. });
  327. };
  328. /**
  329. * Gets the latest available application update (if any)
  330. */
  331. self.getAvailableApplicationUpdate = function () {
  332. var url = self.getUrl("Packages/Updates", { PackageType: "System" });
  333. return self.ajax({
  334. type: "GET",
  335. url: url,
  336. dataType: "json"
  337. });
  338. };
  339. /**
  340. * Gets the latest available plugin updates (if any)
  341. */
  342. self.getAvailablePluginUpdates = function () {
  343. var url = self.getUrl("Packages/Updates", { PackageType: "UserInstalled" });
  344. return self.ajax({
  345. type: "GET",
  346. url: url,
  347. dataType: "json"
  348. });
  349. };
  350. /**
  351. * Gets the virtual folder for a view. Specify a userId to get a user view, or omit for the default view.
  352. */
  353. self.getVirtualFolders = function (userId) {
  354. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  355. url = self.getUrl(url);
  356. return self.ajax({
  357. type: "GET",
  358. url: url,
  359. dataType: "json"
  360. });
  361. };
  362. /**
  363. * Gets all the paths of the locations in the physical root.
  364. */
  365. self.getPhysicalPaths = function () {
  366. var url = self.getUrl("Library/PhysicalPaths");
  367. return self.ajax({
  368. type: "GET",
  369. url: url,
  370. dataType: "json"
  371. });
  372. };
  373. /**
  374. * Gets the current server configuration
  375. */
  376. self.getServerConfiguration = function () {
  377. var url = self.getUrl("System/Configuration");
  378. return self.ajax({
  379. type: "GET",
  380. url: url,
  381. dataType: "json"
  382. });
  383. };
  384. /**
  385. * Gets the server's scheduled tasks
  386. */
  387. self.getScheduledTasks = function () {
  388. var url = self.getUrl("ScheduledTasks");
  389. return self.ajax({
  390. type: "GET",
  391. url: url,
  392. dataType: "json"
  393. });
  394. };
  395. /**
  396. * Starts a scheduled task
  397. */
  398. self.startScheduledTask = function (id) {
  399. if (!id) {
  400. throw new Error("null id");
  401. }
  402. var url = self.getUrl("ScheduledTasks/Running/" + id);
  403. return self.ajax({
  404. type: "POST",
  405. url: url
  406. });
  407. };
  408. /**
  409. * Gets a scheduled task
  410. */
  411. self.getScheduledTask = function (id) {
  412. if (!id) {
  413. throw new Error("null id");
  414. }
  415. var url = self.getUrl("ScheduledTasks/" + id);
  416. return self.ajax({
  417. type: "GET",
  418. url: url,
  419. dataType: "json"
  420. });
  421. };
  422. /**
  423. * Stops a scheduled task
  424. */
  425. self.stopScheduledTask = function (id) {
  426. if (!id) {
  427. throw new Error("null id");
  428. }
  429. var url = self.getUrl("ScheduledTasks/Running/" + id);
  430. return self.ajax({
  431. type: "DELETE",
  432. url: url,
  433. dataType: "json"
  434. });
  435. };
  436. /**
  437. * Gets the configuration of a plugin
  438. * @param {String} Id
  439. */
  440. self.getPluginConfiguration = function (id) {
  441. if (!id) {
  442. throw new Error("null Id");
  443. }
  444. var url = self.getUrl("Plugins/" + id + "/Configuration");
  445. return self.ajax({
  446. type: "GET",
  447. url: url,
  448. dataType: "json"
  449. });
  450. };
  451. /**
  452. * Gets a list of plugins that are available to be installed
  453. */
  454. self.getAvailablePlugins = function (options) {
  455. options = $.extend({}, options || {});
  456. options.PackageType = "UserInstalled";
  457. var url = self.getUrl("Packages", options);
  458. return self.ajax({
  459. type: "GET",
  460. url: url,
  461. dataType: "json"
  462. });
  463. };
  464. /**
  465. * Uninstalls a plugin
  466. * @param {String} Id
  467. */
  468. self.uninstallPlugin = function (id) {
  469. if (!id) {
  470. throw new Error("null Id");
  471. }
  472. var url = self.getUrl("Plugins/" + id);
  473. return self.ajax({
  474. type: "DELETE",
  475. url: url,
  476. dataType: "json"
  477. });
  478. };
  479. /**
  480. * Removes a virtual folder from either the default view or a user view
  481. * @param {String} name
  482. */
  483. self.removeVirtualFolder = function (name, userId) {
  484. if (!name) {
  485. throw new Error("null name");
  486. }
  487. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  488. url += "/" + name;
  489. url = self.getUrl(url);
  490. return self.ajax({
  491. type: "DELETE",
  492. url: url,
  493. dataType: "json"
  494. });
  495. };
  496. /**
  497. * Adds a virtual folder to either the default view or a user view
  498. * @param {String} name
  499. */
  500. self.addVirtualFolder = function (name, userId) {
  501. if (!name) {
  502. throw new Error("null name");
  503. }
  504. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  505. url += "/" + name;
  506. url = self.getUrl(url);
  507. return self.ajax({
  508. type: "POST",
  509. url: url
  510. });
  511. };
  512. /**
  513. * Renames a virtual folder, within either the default view or a user view
  514. * @param {String} name
  515. */
  516. self.renameVirtualFolder = function (name, newName, userId) {
  517. if (!name) {
  518. throw new Error("null name");
  519. }
  520. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  521. url += "/" + name + "/Name";
  522. url = self.getUrl(url, { newName: newName });
  523. return self.ajax({
  524. type: "POST",
  525. url: url
  526. });
  527. };
  528. /**
  529. * Adds an additional mediaPath to an existing virtual folder, within either the default view or a user view
  530. * @param {String} name
  531. */
  532. self.addMediaPath = function (virtualFolderName, mediaPath, userId) {
  533. if (!virtualFolderName) {
  534. throw new Error("null virtualFolderName");
  535. }
  536. if (!mediaPath) {
  537. throw new Error("null mediaPath");
  538. }
  539. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  540. url += "/" + virtualFolderName + "/Paths";
  541. url = self.getUrl(url, { path: mediaPath });
  542. return self.ajax({
  543. type: "POST",
  544. url: url
  545. });
  546. };
  547. /**
  548. * Removes a media path from a virtual folder, within either the default view or a user view
  549. * @param {String} name
  550. */
  551. self.removeMediaPath = function (virtualFolderName, mediaPath, userId) {
  552. if (!virtualFolderName) {
  553. throw new Error("null virtualFolderName");
  554. }
  555. if (!mediaPath) {
  556. throw new Error("null mediaPath");
  557. }
  558. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  559. url += "/" + virtualFolderName + "/Paths";
  560. url = self.getUrl(url, { path: mediaPath });
  561. return self.ajax({
  562. type: "DELETE",
  563. url: url,
  564. dataType: "json"
  565. });
  566. };
  567. /**
  568. * Deletes a user
  569. * @param {String} id
  570. */
  571. self.deleteUser = function (id) {
  572. if (!id) {
  573. throw new Error("null id");
  574. }
  575. var url = self.getUrl("Users/" + id);
  576. return self.ajax({
  577. type: "DELETE",
  578. url: url,
  579. dataType: "json"
  580. });
  581. };
  582. /**
  583. * Deletes a user image
  584. * @param {String} userId
  585. * @param {String} imageType The type of image to delete, based on the server-side ImageType enum.
  586. */
  587. self.deleteUserImage = function (userId, imageType) {
  588. if (!userId) {
  589. throw new Error("null userId");
  590. }
  591. if (!imageType) {
  592. throw new Error("null imageType");
  593. }
  594. var url = self.getUrl("Users/" + userId + "/Images/" + imageType);
  595. return self.ajax({
  596. type: "DELETE",
  597. url: url,
  598. dataType: "json"
  599. });
  600. };
  601. /**
  602. * Uploads a user image
  603. * @param {String} userId
  604. * @param {String} imageType The type of image to delete, based on the server-side ImageType enum.
  605. * @param {Object} file The file from the input element
  606. */
  607. self.uploadUserImage = function (userId, imageType, file) {
  608. if (!userId) {
  609. throw new Error("null userId");
  610. }
  611. if (!imageType) {
  612. throw new Error("null imageType");
  613. }
  614. if (!file) {
  615. throw new Error("File must be an image.");
  616. }
  617. if (file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/jpeg") {
  618. throw new Error("File must be an image.");
  619. }
  620. var deferred = $.Deferred();
  621. var reader = new FileReader();
  622. reader.onerror = function () {
  623. deferred.reject();
  624. };
  625. reader.onabort = function () {
  626. deferred.reject();
  627. };
  628. // Closure to capture the file information.
  629. reader.onload = function (e) {
  630. var data = window.btoa(e.target.result);
  631. var url = self.getUrl("Users/" + userId + "/Images/" + imageType);
  632. self.ajax({
  633. type: "POST",
  634. url: url,
  635. data: data,
  636. contentType: "image/" + file.name.substring(file.name.lastIndexOf('.') + 1)
  637. }).done(function (result) {
  638. deferred.resolveWith(null, [result]);
  639. }).fail(function () {
  640. deferred.reject();
  641. });
  642. };
  643. // Read in the image file as a data URL.
  644. reader.readAsBinaryString(file);
  645. return deferred.promise();
  646. };
  647. /**
  648. * Gets the list of installed plugins on the server
  649. */
  650. self.getInstalledPlugins = function () {
  651. var url = self.getUrl("Plugins");
  652. return self.ajax({
  653. type: "GET",
  654. url: url,
  655. dataType: "json"
  656. });
  657. };
  658. /**
  659. * Gets a user by id
  660. * @param {String} id
  661. */
  662. self.getUser = function (id) {
  663. if (!id) {
  664. throw new Error("Must supply a userId");
  665. }
  666. var url = self.getUrl("Users/" + id);
  667. return self.ajax({
  668. type: "GET",
  669. url: url,
  670. dataType: "json"
  671. });
  672. };
  673. /**
  674. * Gets a studio
  675. */
  676. self.getStudio = function (name) {
  677. if (!name) {
  678. throw new Error("null name");
  679. }
  680. var url = self.getUrl("Studios/" + encodeName(name));
  681. return self.ajax({
  682. type: "GET",
  683. url: url,
  684. dataType: "json"
  685. });
  686. };
  687. /**
  688. * Gets a genre
  689. */
  690. self.getGenre = function (name) {
  691. if (!name) {
  692. throw new Error("null name");
  693. }
  694. var url = self.getUrl("Genres/" + encodeName(name));
  695. return self.ajax({
  696. type: "GET",
  697. url: url,
  698. dataType: "json"
  699. });
  700. };
  701. /**
  702. * Gets a year
  703. */
  704. self.getYear = function (year) {
  705. if (!year) {
  706. throw new Error("null year");
  707. }
  708. var url = self.getUrl("Years/" + year);
  709. return self.ajax({
  710. type: "GET",
  711. url: url,
  712. dataType: "json"
  713. });
  714. };
  715. /**
  716. * Gets a Person
  717. */
  718. self.getPerson = function (name) {
  719. if (!name) {
  720. throw new Error("null name");
  721. }
  722. var url = self.getUrl("Persons/" + encodeName(name));
  723. return self.ajax({
  724. type: "GET",
  725. url: url,
  726. dataType: "json"
  727. });
  728. };
  729. /**
  730. * Gets weather info
  731. * @param {String} location - us zip code / city, state, country / city, country
  732. * Omit location to get weather info using stored server configuration value
  733. */
  734. self.getWeatherInfo = function (location) {
  735. var url = self.getUrl("weather", {
  736. location: location
  737. });
  738. return self.ajax({
  739. type: "GET",
  740. url: url,
  741. dataType: "json"
  742. });
  743. };
  744. /**
  745. * Gets all users from the server
  746. */
  747. self.getUsers = function () {
  748. var url = self.getUrl("users");
  749. return self.ajax({
  750. type: "GET",
  751. url: url,
  752. dataType: "json"
  753. });
  754. };
  755. /**
  756. * Gets all available parental ratings from the server
  757. */
  758. self.getParentalRatings = function () {
  759. var url = self.getUrl("Localization/ParentalRatings");
  760. return self.ajax({
  761. type: "GET",
  762. url: url,
  763. dataType: "json"
  764. });
  765. };
  766. /**
  767. * Gets a list of all available conrete BaseItem types from the server
  768. */
  769. self.getItemTypes = function (options) {
  770. var url = self.getUrl("Library/ItemTypes", options);
  771. return self.ajax({
  772. type: "GET",
  773. url: url,
  774. dataType: "json"
  775. });
  776. };
  777. /**
  778. * Constructs a url for a user image
  779. * @param {String} userId
  780. * @param {Object} options
  781. * Options supports the following properties:
  782. * width - download the image at a fixed width
  783. * height - download the image at a fixed height
  784. * maxWidth - download the image at a maxWidth
  785. * maxHeight - download the image at a maxHeight
  786. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  787. * For best results do not specify both width and height together, as aspect ratio might be altered.
  788. */
  789. self.getUserImageUrl = function (userId, options) {
  790. if (!userId) {
  791. throw new Error("null userId");
  792. }
  793. options = options || {
  794. };
  795. var url = "Users/" + userId + "/Images/" + options.type;
  796. if (options.index != null) {
  797. url += "/" + options.index;
  798. }
  799. // Don't put these on the query string
  800. delete options.type;
  801. delete options.index;
  802. return self.getUrl(url, options);
  803. };
  804. /**
  805. * Constructs a url for a person image
  806. * @param {String} name
  807. * @param {Object} options
  808. * Options supports the following properties:
  809. * width - download the image at a fixed width
  810. * height - download the image at a fixed height
  811. * maxWidth - download the image at a maxWidth
  812. * maxHeight - download the image at a maxHeight
  813. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  814. * For best results do not specify both width and height together, as aspect ratio might be altered.
  815. */
  816. self.getPersonImageUrl = function (name, options) {
  817. if (!name) {
  818. throw new Error("null name");
  819. }
  820. options = options || {
  821. };
  822. var url = "Persons/" + encodeName(name) + "/Images/" + options.type;
  823. if (options.index != null) {
  824. url += "/" + options.index;
  825. }
  826. // Don't put these on the query string
  827. delete options.type;
  828. delete options.index;
  829. return self.getUrl(url, options);
  830. };
  831. /**
  832. * Constructs a url for a year image
  833. * @param {String} year
  834. * @param {Object} options
  835. * Options supports the following properties:
  836. * width - download the image at a fixed width
  837. * height - download the image at a fixed height
  838. * maxWidth - download the image at a maxWidth
  839. * maxHeight - download the image at a maxHeight
  840. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  841. * For best results do not specify both width and height together, as aspect ratio might be altered.
  842. */
  843. self.getYearImageUrl = function (year, options) {
  844. if (!year) {
  845. throw new Error("null year");
  846. }
  847. options = options || {
  848. };
  849. var url = "Years/" + year + "/Images/" + options.type;
  850. if (options.index != null) {
  851. url += "/" + options.index;
  852. }
  853. // Don't put these on the query string
  854. delete options.type;
  855. delete options.index;
  856. return self.getUrl(url, options);
  857. };
  858. /**
  859. * Constructs a url for a genre image
  860. * @param {String} name
  861. * @param {Object} options
  862. * Options supports the following properties:
  863. * width - download the image at a fixed width
  864. * height - download the image at a fixed height
  865. * maxWidth - download the image at a maxWidth
  866. * maxHeight - download the image at a maxHeight
  867. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  868. * For best results do not specify both width and height together, as aspect ratio might be altered.
  869. */
  870. self.getGenreImageUrl = function (name, options) {
  871. if (!name) {
  872. throw new Error("null name");
  873. }
  874. options = options || {
  875. };
  876. var url = "Genres/" + encodeName(name) + "/Images/" + options.type;
  877. if (options.index != null) {
  878. url += "/" + options.index;
  879. }
  880. // Don't put these on the query string
  881. delete options.type;
  882. delete options.index;
  883. return self.getUrl(url, options);
  884. };
  885. /**
  886. * Constructs a url for a studio image
  887. * @param {String} name
  888. * @param {Object} options
  889. * Options supports the following properties:
  890. * width - download the image at a fixed width
  891. * height - download the image at a fixed height
  892. * maxWidth - download the image at a maxWidth
  893. * maxHeight - download the image at a maxHeight
  894. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  895. * For best results do not specify both width and height together, as aspect ratio might be altered.
  896. */
  897. self.getStudioImageUrl = function (name, options) {
  898. if (!name) {
  899. throw new Error("null name");
  900. }
  901. options = options || {
  902. };
  903. var url = "Studios/" + encodeName(name) + "/Images/" + options.type;
  904. if (options.index != null) {
  905. url += "/" + options.index;
  906. }
  907. // Don't put these on the query string
  908. delete options.type;
  909. delete options.index;
  910. return self.getUrl(url, options);
  911. };
  912. /**
  913. * Constructs a url for an item image
  914. * @param {String} itemId
  915. * @param {Object} options
  916. * Options supports the following properties:
  917. * type - Primary, logo, backdrop, etc. See the server-side enum ImageType
  918. * index - When downloading a backdrop, use this to specify which one (omitting is equivalent to zero)
  919. * width - download the image at a fixed width
  920. * height - download the image at a fixed height
  921. * maxWidth - download the image at a maxWidth
  922. * maxHeight - download the image at a maxHeight
  923. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  924. * For best results do not specify both width and height together, as aspect ratio might be altered.
  925. */
  926. self.getImageUrl = function (itemId, options) {
  927. if (!itemId) {
  928. throw new Error("itemId cannot be empty");
  929. }
  930. options = options || {
  931. };
  932. var url = "Items/" + itemId + "/Images/" + options.type;
  933. if (options.index != null) {
  934. url += "/" + options.index;
  935. }
  936. // Don't put these on the query string
  937. delete options.type;
  938. delete options.index;
  939. return self.getUrl(url, options);
  940. };
  941. /**
  942. * Constructs a url for an item logo image
  943. * If the item doesn't have a logo, it will inherit a logo from a parent
  944. * @param {Object} item A BaseItem
  945. * @param {Object} options
  946. * Options supports the following properties:
  947. * width - download the image at a fixed width
  948. * height - download the image at a fixed height
  949. * maxWidth - download the image at a maxWidth
  950. * maxHeight - download the image at a maxHeight
  951. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  952. * For best results do not specify both width and height together, as aspect ratio might be altered.
  953. */
  954. self.getLogoImageUrl = function (item, options) {
  955. if (!item) {
  956. throw new Error("null item");
  957. }
  958. options = options || {
  959. };
  960. options.imageType = "logo";
  961. var logoItemId = item.HasLogo ? item.Id : item.ParentLogoItemId;
  962. return logoItemId ? self.getImageUrl(logoItemId, options) : null;
  963. };
  964. /**
  965. * Constructs an array of backdrop image url's for an item
  966. * If the item doesn't have any backdrops, it will inherit them from a parent
  967. * @param {Object} item A BaseItem
  968. * @param {Object} options
  969. * Options supports the following properties:
  970. * width - download the image at a fixed width
  971. * height - download the image at a fixed height
  972. * maxWidth - download the image at a maxWidth
  973. * maxHeight - download the image at a maxHeight
  974. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  975. * For best results do not specify both width and height together, as aspect ratio might be altered.
  976. */
  977. self.getBackdropImageUrl = function (item, options) {
  978. if (!item) {
  979. throw new Error("null item");
  980. }
  981. options = options || {
  982. };
  983. options.imageType = "backdrop";
  984. var backdropItemId;
  985. var backdropCount;
  986. if (!item.BackdropCount) {
  987. backdropItemId = item.ParentBackdropItemId;
  988. backdropCount = item.ParentBackdropCount || 0;
  989. } else {
  990. backdropItemId = item.Id;
  991. backdropCount = item.BackdropCount;
  992. }
  993. if (!backdropItemId) {
  994. return [];
  995. }
  996. var files = [];
  997. for (var i = 0; i < backdropCount; i++) {
  998. options.imageIndex = i;
  999. files[i] = self.getImageUrl(backdropItemId, options);
  1000. }
  1001. return files;
  1002. };
  1003. /**
  1004. * Authenticates a user
  1005. * @param {String} userId
  1006. * @param {String} password
  1007. */
  1008. self.authenticateUser = function (userId, password) {
  1009. if (!userId) {
  1010. throw new Error("null userId");
  1011. }
  1012. var url = self.getUrl("Users/" + userId + "/authenticate");
  1013. var postData = {
  1014. password: MediaBrowser.SHA1(password || "")
  1015. };
  1016. return self.ajax({
  1017. type: "POST",
  1018. url: url,
  1019. data: JSON.stringify(postData),
  1020. dataType: "json",
  1021. contentType: "application/json"
  1022. });
  1023. };
  1024. /**
  1025. * Updates a user's password
  1026. * @param {String} userId
  1027. * @param {String} currentPassword
  1028. * @param {String} newPassword
  1029. */
  1030. self.updateUserPassword = function (userId, currentPassword, newPassword) {
  1031. if (!userId) {
  1032. throw new Error("null userId");
  1033. }
  1034. var url = self.getUrl("Users/" + userId + "/Password");
  1035. var postData = {
  1036. };
  1037. postData.currentPassword = MediaBrowser.SHA1(currentPassword);
  1038. if (newPassword) {
  1039. postData.newPassword = newPassword;
  1040. }
  1041. return self.ajax({
  1042. type: "POST",
  1043. url: url,
  1044. data: postData
  1045. });
  1046. };
  1047. /**
  1048. * Resets a user's password
  1049. * @param {String} userId
  1050. */
  1051. self.resetUserPassword = function (userId) {
  1052. if (!userId) {
  1053. throw new Error("null userId");
  1054. }
  1055. var url = self.getUrl("Users/" + userId + "/Password");
  1056. var postData = {
  1057. };
  1058. postData.resetPassword = true;
  1059. return self.ajax({
  1060. type: "POST",
  1061. url: url,
  1062. data: postData
  1063. });
  1064. };
  1065. /**
  1066. * Updates the server's configuration
  1067. * @param {Object} configuration
  1068. */
  1069. self.updateServerConfiguration = function (configuration) {
  1070. if (!configuration) {
  1071. throw new Error("null configuration");
  1072. }
  1073. var url = self.getUrl("System/Configuration");
  1074. return self.ajax({
  1075. type: "POST",
  1076. url: url,
  1077. data: JSON.stringify(configuration),
  1078. dataType: "json",
  1079. contentType: "application/json"
  1080. });
  1081. };
  1082. /**
  1083. * Updates plugin security info
  1084. */
  1085. self.updatePluginSecurityInfo = function (info) {
  1086. var url = self.getUrl("Plugins/SecurityInfo");
  1087. return self.ajax({
  1088. type: "POST",
  1089. url: url,
  1090. data: JSON.stringify(info),
  1091. dataType: "json",
  1092. contentType: "application/json"
  1093. });
  1094. };
  1095. /**
  1096. * Creates a user
  1097. * @param {Object} user
  1098. */
  1099. self.createUser = function (user) {
  1100. if (!user) {
  1101. throw new Error("null user");
  1102. }
  1103. var url = self.getUrl("Users");
  1104. return self.ajax({
  1105. type: "POST",
  1106. url: url,
  1107. data: JSON.stringify(user),
  1108. dataType: "json",
  1109. contentType: "application/json"
  1110. });
  1111. };
  1112. /**
  1113. * Updates a user
  1114. * @param {Object} user
  1115. */
  1116. self.updateUser = function (user) {
  1117. if (!user) {
  1118. throw new Error("null user");
  1119. }
  1120. var url = self.getUrl("Users/" + user.Id);
  1121. return self.ajax({
  1122. type: "POST",
  1123. url: url,
  1124. data: JSON.stringify(user),
  1125. dataType: "json",
  1126. contentType: "application/json"
  1127. });
  1128. };
  1129. /**
  1130. * Updates the Triggers for a ScheduledTask
  1131. * @param {String} id
  1132. * @param {Object} triggers
  1133. */
  1134. self.updateScheduledTaskTriggers = function (id, triggers) {
  1135. if (!id) {
  1136. throw new Error("null id");
  1137. }
  1138. if (!triggers) {
  1139. throw new Error("null triggers");
  1140. }
  1141. var url = self.getUrl("ScheduledTasks/" + id + "/Triggers");
  1142. return self.ajax({
  1143. type: "POST",
  1144. url: url,
  1145. data: JSON.stringify(triggers),
  1146. dataType: "json",
  1147. contentType: "application/json"
  1148. });
  1149. };
  1150. /**
  1151. * Updates a plugin's configuration
  1152. * @param {String} Id
  1153. * @param {Object} configuration
  1154. */
  1155. self.updatePluginConfiguration = function (id, configuration) {
  1156. if (!id) {
  1157. throw new Error("null Id");
  1158. }
  1159. if (!configuration) {
  1160. throw new Error("null configuration");
  1161. }
  1162. var url = self.getUrl("Plugins/" + id + "/Configuration");
  1163. return self.ajax({
  1164. type: "POST",
  1165. url: url,
  1166. data: JSON.stringify(configuration),
  1167. dataType: "json",
  1168. contentType: "application/json"
  1169. });
  1170. };
  1171. /**
  1172. * Gets items based on a query, typically for children of a folder
  1173. * @param {String} userId
  1174. * @param {Object} options
  1175. * Options accepts the following properties:
  1176. * itemId - Localize the search to a specific folder (root if omitted)
  1177. * startIndex - Use for paging
  1178. * limit - Use to limit results to a certain number of items
  1179. * filter - Specify one or more ItemFilters, comma delimeted (see server-side enum)
  1180. * sortBy - Specify an ItemSortBy (comma-delimeted list see server-side enum)
  1181. * sortOrder - ascending/descending
  1182. * fields - additional fields to include aside from basic info. This is a comma delimited list. See server-side enum ItemFields.
  1183. * index - the name of the dynamic, localized index function
  1184. * dynamicSortBy - the name of the dynamic localized sort function
  1185. * recursive - Whether or not the query should be recursive
  1186. * searchTerm - search term to use as a filter
  1187. */
  1188. self.getItems = function (userId, options) {
  1189. if (!userId) {
  1190. throw new Error("null userId");
  1191. }
  1192. var url = self.getUrl("Users/" + userId + "/Items", options);
  1193. return self.ajax({
  1194. type: "GET",
  1195. url: url,
  1196. dataType: "json"
  1197. });
  1198. };
  1199. /**
  1200. Gets genres from an item
  1201. */
  1202. self.getGenres = function (userId, options) {
  1203. if (!userId) {
  1204. throw new Error("null userId");
  1205. }
  1206. var parentId = options.parentId || "root";
  1207. // Don't put these on the query string
  1208. delete options.parentId;
  1209. var url = self.getUrl("Users/" + userId + "/Items/" + parentId + "/Genres", options);
  1210. return self.ajax({
  1211. type: "GET",
  1212. url: url,
  1213. dataType: "json"
  1214. });
  1215. };
  1216. /**
  1217. Gets people from an item
  1218. */
  1219. self.getPeople = function (userId, options) {
  1220. if (!userId) {
  1221. throw new Error("null userId");
  1222. }
  1223. var parentId = options.parentId || "root";
  1224. // Don't put these on the query string
  1225. delete options.parentId;
  1226. var url = self.getUrl("Users/" + userId + "/Items/" + parentId + "/Persons", options);
  1227. return self.ajax({
  1228. type: "GET",
  1229. url: url,
  1230. dataType: "json"
  1231. });
  1232. };
  1233. /**
  1234. Gets studios from an item
  1235. */
  1236. self.getStudios = function (userId, options) {
  1237. if (!userId) {
  1238. throw new Error("null userId");
  1239. }
  1240. var parentId = options.parentId || "root";
  1241. // Don't put these on the query string
  1242. delete options.parentId;
  1243. var url = self.getUrl("Users/" + userId + "/Items/" + parentId + "/Studios", options);
  1244. return self.ajax({
  1245. type: "GET",
  1246. url: url,
  1247. dataType: "json"
  1248. });
  1249. };
  1250. /**
  1251. * Gets local trailers for an item
  1252. */
  1253. self.getLocalTrailers = function (userId, itemId) {
  1254. if (!userId) {
  1255. throw new Error("null userId");
  1256. }
  1257. if (!itemId) {
  1258. throw new Error("null itemId");
  1259. }
  1260. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/LocalTrailers");
  1261. return self.ajax({
  1262. type: "GET",
  1263. url: url,
  1264. dataType: "json"
  1265. });
  1266. };
  1267. /**
  1268. * Gets special features for an item
  1269. */
  1270. self.getSpecialFeatures = function (userId, itemId) {
  1271. if (!userId) {
  1272. throw new Error("null userId");
  1273. }
  1274. if (!itemId) {
  1275. throw new Error("null itemId");
  1276. }
  1277. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/SpecialFeatures");
  1278. return self.ajax({
  1279. type: "GET",
  1280. url: url,
  1281. dataType: "json"
  1282. });
  1283. };
  1284. /**
  1285. * Marks an item as played or unplayed
  1286. * This should not be used to update playstate following playback.
  1287. * There are separate playstate check-in methods for that. This should be used for a
  1288. * separate option to reset playstate.
  1289. * @param {String} userId
  1290. * @param {String} itemId
  1291. * @param {Boolean} wasPlayed
  1292. */
  1293. self.updatePlayedStatus = function (userId, itemId, wasPlayed) {
  1294. if (!userId) {
  1295. throw new Error("null userId");
  1296. }
  1297. if (!itemId) {
  1298. throw new Error("null itemId");
  1299. }
  1300. var url = self.getUrl("Users/" + userId + "/PlayedItems/" + itemId);
  1301. var method = wasPlayed ? "POST" : "DELETE";
  1302. return self.ajax({
  1303. type: method,
  1304. url: url,
  1305. dataType: "json"
  1306. });
  1307. };
  1308. /**
  1309. * Updates a user's favorite status for an item.
  1310. * @param {String} userId
  1311. * @param {String} itemId
  1312. * @param {Boolean} isFavorite
  1313. */
  1314. self.updateFavoriteStatus = function (userId, itemId, isFavorite) {
  1315. if (!userId) {
  1316. throw new Error("null userId");
  1317. }
  1318. if (!itemId) {
  1319. throw new Error("null itemId");
  1320. }
  1321. var url = self.getUrl("Users/" + userId + "/FavoriteItems/" + itemId);
  1322. var method = isFavorite ? "POST" : "DELETE";
  1323. return self.ajax({
  1324. type: method,
  1325. url: url,
  1326. dataType: "json"
  1327. });
  1328. };
  1329. /**
  1330. * Updates a user's personal rating for an item
  1331. * @param {String} userId
  1332. * @param {String} itemId
  1333. * @param {Boolean} likes
  1334. */
  1335. self.updateUserItemRating = function (userId, itemId, likes) {
  1336. if (!userId) {
  1337. throw new Error("null userId");
  1338. }
  1339. if (!itemId) {
  1340. throw new Error("null itemId");
  1341. }
  1342. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating", {
  1343. likes: likes
  1344. });
  1345. return self.ajax({
  1346. type: "POST",
  1347. url: url
  1348. });
  1349. };
  1350. /**
  1351. * Updates a user's favorite status for an item by name.
  1352. * @param {String} userId
  1353. * @param {String} name
  1354. * @param {Boolean} isFavorite
  1355. */
  1356. self.updateItemByNameFavoriteStatus = function (userId, name, isFavorite) {
  1357. if (!userId) {
  1358. throw new Error("null userId");
  1359. }
  1360. if (!name) {
  1361. throw new Error("null name");
  1362. }
  1363. var url = self.getUrl("Users/" + userId + "/ItemsByName/Favorites/" + encodeName(name));
  1364. var method = isFavorite ? "POST" : "DELETE";
  1365. return self.ajax({
  1366. type: method,
  1367. url: url,
  1368. dataType: "json"
  1369. });
  1370. };
  1371. /**
  1372. * Updates a user's rating for an item by name.
  1373. * @param {String} userId
  1374. * @param {String} name
  1375. * @param {Boolean} likes
  1376. */
  1377. self.updateItemByNameRating = function (userId, name, likes) {
  1378. if (!userId) {
  1379. throw new Error("null userId");
  1380. }
  1381. if (!name) {
  1382. throw new Error("null name");
  1383. }
  1384. var url = self.getUrl("Users/" + userId + "/ItemsByName/" + encodeName(name) + "/Rating", {
  1385. likes: likes
  1386. });
  1387. return self.ajax({
  1388. type: "POST",
  1389. url: url
  1390. });
  1391. };
  1392. /**
  1393. * Clears a user's rating for an item by name.
  1394. * @param {String} userId
  1395. * @param {String} name
  1396. */
  1397. self.clearItemByNameRating = function (userId, name) {
  1398. if (!userId) {
  1399. throw new Error("null userId");
  1400. }
  1401. if (!name) {
  1402. throw new Error("null name");
  1403. }
  1404. var url = self.getUrl("Users/" + userId + "/ItemsByName/" + encodeName(name) + "/Rating");
  1405. return self.ajax({
  1406. type: "DELETE",
  1407. url: url,
  1408. dataType: "json"
  1409. });
  1410. };
  1411. /**
  1412. * Gets the full user data object for an item by name.
  1413. * @param {String} userId
  1414. * @param {String} name
  1415. */
  1416. self.getItembyNameUserData = function (userId, name) {
  1417. if (!userId) {
  1418. throw new Error("null userId");
  1419. }
  1420. if (!name) {
  1421. throw new Error("null name");
  1422. }
  1423. var url = self.getUrl("Users/" + userId + "/ItemsByName/" + encodeName(name) + "/UserData");
  1424. return self.ajax({
  1425. type: "GET",
  1426. url: url,
  1427. dataType: "json"
  1428. });
  1429. };
  1430. /**
  1431. Gets a variety of item counts that a person appears in
  1432. */
  1433. self.getPersonItemCounts = function (userId, name) {
  1434. if (!userId) {
  1435. throw new Error("null userId");
  1436. }
  1437. if (!name) {
  1438. throw new Error("null name");
  1439. }
  1440. var url = self.getUrl("Users/" + userId + "/Persons/" + encodeName(name) + "/Counts");
  1441. return self.ajax({
  1442. type: "GET",
  1443. url: url,
  1444. dataType: "json"
  1445. });
  1446. };
  1447. /**
  1448. Gets a variety of item counts that a genre appears in
  1449. */
  1450. self.getGenreItemCounts = function (userId, name) {
  1451. if (!userId) {
  1452. throw new Error("null userId");
  1453. }
  1454. if (!name) {
  1455. throw new Error("null name");
  1456. }
  1457. var url = self.getUrl("Users/" + userId + "/Genres/" + encodeName(name) + "/Counts");
  1458. return self.ajax({
  1459. type: "GET",
  1460. url: url,
  1461. dataType: "json"
  1462. });
  1463. };
  1464. /**
  1465. Gets a variety of item counts that a studio appears in
  1466. */
  1467. self.getStudioItemCounts = function (userId, name) {
  1468. if (!userId) {
  1469. throw new Error("null userId");
  1470. }
  1471. if (!name) {
  1472. throw new Error("null name");
  1473. }
  1474. var url = self.getUrl("Users/" + userId + "/Studios/" + encodeName(name) + "/Counts");
  1475. return self.ajax({
  1476. type: "GET",
  1477. url: url,
  1478. dataType: "json"
  1479. });
  1480. };
  1481. /**
  1482. * Clears a user's personal rating for an item
  1483. * @param {String} userId
  1484. * @param {String} itemId
  1485. */
  1486. self.clearUserItemRating = function (userId, itemId) {
  1487. if (!userId) {
  1488. throw new Error("null userId");
  1489. }
  1490. if (!itemId) {
  1491. throw new Error("null itemId");
  1492. }
  1493. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating");
  1494. return self.ajax({
  1495. type: "DELETE",
  1496. url: url,
  1497. dataType: "json"
  1498. });
  1499. };
  1500. /**
  1501. * Reports the user has started playing something
  1502. * @param {String} userId
  1503. * @param {String} itemId
  1504. */
  1505. self.reportPlaybackStart = function (userId, itemId) {
  1506. if (!userId) {
  1507. throw new Error("null userId");
  1508. }
  1509. if (!itemId) {
  1510. throw new Error("null itemId");
  1511. }
  1512. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId);
  1513. return self.ajax({
  1514. type: "POST",
  1515. url: url,
  1516. dataType: "json"
  1517. });
  1518. };
  1519. /**
  1520. * Reports progress viewing an item
  1521. * @param {String} userId
  1522. * @param {String} itemId
  1523. */
  1524. self.reportPlaybackProgress = function (userId, itemId, positionTicks) {
  1525. if (!userId) {
  1526. throw new Error("null userId");
  1527. }
  1528. if (!itemId) {
  1529. throw new Error("null itemId");
  1530. }
  1531. var params = {
  1532. };
  1533. if (positionTicks) {
  1534. params.positionTicks = positionTicks;
  1535. }
  1536. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId + "/Progress", params);
  1537. return self.ajax({
  1538. type: "POST",
  1539. url: url,
  1540. dataType: "json"
  1541. });
  1542. };
  1543. /**
  1544. * Reports a user has stopped playing an item
  1545. * @param {String} userId
  1546. * @param {String} itemId
  1547. */
  1548. self.reportPlaybackStopped = function (userId, itemId, positionTicks) {
  1549. if (!userId) {
  1550. throw new Error("null userId");
  1551. }
  1552. if (!itemId) {
  1553. throw new Error("null itemId");
  1554. }
  1555. var params = {
  1556. };
  1557. if (positionTicks) {
  1558. params.positionTicks = positionTicks;
  1559. }
  1560. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId, params);
  1561. return self.ajax({
  1562. type: "DELETE",
  1563. url: url,
  1564. dataType: "json"
  1565. });
  1566. };
  1567. }
  1568. }(jQuery, navigator, JSON, window.WebSocket, setTimeout);
  1569. /**
  1570. * Provides a friendly way to create an api client instance using information from the browser's current url
  1571. */
  1572. MediaBrowser.ApiClient.create = function (clientName) {
  1573. var loc = window.location;
  1574. return new MediaBrowser.ApiClient(loc.protocol, loc.hostname, loc.port, clientName);
  1575. };
  1576. /**
  1577. *
  1578. * Secure Hash Algorithm (SHA1)
  1579. * http://www.webtoolkit.info/
  1580. *
  1581. **/
  1582. MediaBrowser.SHA1 = function (msg) {
  1583. function rotate_left(n, s) {
  1584. var t4 = (n << s) | (n >>> (32 - s));
  1585. return t4;
  1586. }
  1587. function lsb_hex(val) {
  1588. var str = "";
  1589. var i;
  1590. var vh;
  1591. var vl;
  1592. for (i = 0; i <= 6; i += 2) {
  1593. vh = (val >>> (i * 4 + 4)) & 0x0f;
  1594. vl = (val >>> (i * 4)) & 0x0f;
  1595. str += vh.toString(16) + vl.toString(16);
  1596. }
  1597. return str;
  1598. }
  1599. function cvt_hex(val) {
  1600. var str = "";
  1601. var i;
  1602. var v;
  1603. for (i = 7; i >= 0; i--) {
  1604. v = (val >>> (i * 4)) & 0x0f;
  1605. str += v.toString(16);
  1606. }
  1607. return str;
  1608. }
  1609. function Utf8Encode(string) {
  1610. string = string.replace(/\r\n/g, "\n");
  1611. var utftext = "";
  1612. for (var n = 0; n < string.length; n++) {
  1613. var c = string.charCodeAt(n);
  1614. if (c < 128) {
  1615. utftext += String.fromCharCode(c);
  1616. }
  1617. else if ((c > 127) && (c < 2048)) {
  1618. utftext += String.fromCharCode((c >> 6) | 192);
  1619. utftext += String.fromCharCode((c & 63) | 128);
  1620. }
  1621. else {
  1622. utftext += String.fromCharCode((c >> 12) | 224);
  1623. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  1624. utftext += String.fromCharCode((c & 63) | 128);
  1625. }
  1626. }
  1627. return utftext;
  1628. }
  1629. var blockstart;
  1630. var i, j;
  1631. var W = new Array(80);
  1632. var H0 = 0x67452301;
  1633. var H1 = 0xEFCDAB89;
  1634. var H2 = 0x98BADCFE;
  1635. var H3 = 0x10325476;
  1636. var H4 = 0xC3D2E1F0;
  1637. var A, B, C, D, E;
  1638. var temp;
  1639. msg = Utf8Encode(msg);
  1640. var msg_len = msg.length;
  1641. var word_array = new Array();
  1642. for (i = 0; i < msg_len - 3; i += 4) {
  1643. j = msg.charCodeAt(i) << 24 | msg.charCodeAt(i + 1) << 16 |
  1644. msg.charCodeAt(i + 2) << 8 | msg.charCodeAt(i + 3);
  1645. word_array.push(j);
  1646. }
  1647. switch (msg_len % 4) {
  1648. case 0:
  1649. i = 0x080000000;
  1650. break;
  1651. case 1:
  1652. i = msg.charCodeAt(msg_len - 1) << 24 | 0x0800000;
  1653. break;
  1654. case 2:
  1655. i = msg.charCodeAt(msg_len - 2) << 24 | msg.charCodeAt(msg_len - 1) << 16 | 0x08000;
  1656. break;
  1657. case 3:
  1658. i = msg.charCodeAt(msg_len - 3) << 24 | msg.charCodeAt(msg_len - 2) << 16 | msg.charCodeAt(msg_len - 1) << 8 | 0x80;
  1659. break;
  1660. }
  1661. word_array.push(i);
  1662. while ((word_array.length % 16) != 14) word_array.push(0);
  1663. word_array.push(msg_len >>> 29);
  1664. word_array.push((msg_len << 3) & 0x0ffffffff);
  1665. for (blockstart = 0; blockstart < word_array.length; blockstart += 16) {
  1666. for (i = 0; i < 16; i++) W[i] = word_array[blockstart + i];
  1667. for (i = 16; i <= 79; i++) W[i] = rotate_left(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
  1668. A = H0;
  1669. B = H1;
  1670. C = H2;
  1671. D = H3;
  1672. E = H4;
  1673. for (i = 0; i <= 19; i++) {
  1674. temp = (rotate_left(A, 5) + ((B & C) | (~B & D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
  1675. E = D;
  1676. D = C;
  1677. C = rotate_left(B, 30);
  1678. B = A;
  1679. A = temp;
  1680. }
  1681. for (i = 20; i <= 39; i++) {
  1682. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
  1683. E = D;
  1684. D = C;
  1685. C = rotate_left(B, 30);
  1686. B = A;
  1687. A = temp;
  1688. }
  1689. for (i = 40; i <= 59; i++) {
  1690. temp = (rotate_left(A, 5) + ((B & C) | (B & D) | (C & D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
  1691. E = D;
  1692. D = C;
  1693. C = rotate_left(B, 30);
  1694. B = A;
  1695. A = temp;
  1696. }
  1697. for (i = 60; i <= 79; i++) {
  1698. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
  1699. E = D;
  1700. D = C;
  1701. C = rotate_left(B, 30);
  1702. B = A;
  1703. A = temp;
  1704. }
  1705. H0 = (H0 + A) & 0x0ffffffff;
  1706. H1 = (H1 + B) & 0x0ffffffff;
  1707. H2 = (H2 + C) & 0x0ffffffff;
  1708. H3 = (H3 + D) & 0x0ffffffff;
  1709. H4 = (H4 + E) & 0x0ffffffff;
  1710. }
  1711. var temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4);
  1712. return temp.toLowerCase();
  1713. };
  1714. (function (jQuery, window, undefined) {
  1715. "use strict";
  1716. var matched, browser;
  1717. jQuery.uaMatch = function (ua) {
  1718. ua = ua.toLowerCase();
  1719. var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
  1720. /(webkit)[ \/]([\w.]+)/.exec(ua) ||
  1721. /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
  1722. /(msie) ([\w.]+)/.exec(ua) ||
  1723. ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
  1724. [];
  1725. var platform_match = /(ipad)/.exec(ua) ||
  1726. /(iphone)/.exec(ua) ||
  1727. /(android)/.exec(ua) ||
  1728. [];
  1729. return {
  1730. browser: match[1] || "",
  1731. version: match[2] || "0",
  1732. platform: platform_match[0] || ""
  1733. };
  1734. };
  1735. matched = jQuery.uaMatch(window.navigator.userAgent);
  1736. browser = {};
  1737. if (matched.browser) {
  1738. browser[matched.browser] = true;
  1739. browser.version = matched.version;
  1740. }
  1741. if (matched.platform) {
  1742. browser[matched.platform] = true
  1743. }
  1744. // Chrome is Webkit, but Webkit is also Safari.
  1745. if (browser.chrome) {
  1746. browser.webkit = true;
  1747. } else if (browser.webkit) {
  1748. browser.safari = true;
  1749. }
  1750. jQuery.browser = browser;
  1751. })(jQuery, window);