ApiClient.js 59 KB

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