ApiClient.js 55 KB

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