ApiClient.js 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171
  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 people from an item
  1212. */
  1213. self.getPeople = 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 + "/Persons", options);
  1221. return self.ajax({
  1222. type: "GET",
  1223. url: url,
  1224. dataType: "json"
  1225. });
  1226. };
  1227. /**
  1228. Gets studios from an item
  1229. */
  1230. self.getStudios = function (userId, options) {
  1231. if (!userId) {
  1232. throw new Error("null userId");
  1233. }
  1234. var parentId = options.parentId || "root";
  1235. // Don't put these on the query string
  1236. delete options.parentId;
  1237. var url = self.getUrl("Users/" + userId + "/Items/" + parentId + "/Studios", options);
  1238. return self.ajax({
  1239. type: "GET",
  1240. url: url,
  1241. dataType: "json"
  1242. });
  1243. };
  1244. /**
  1245. * Gets local trailers for an item
  1246. */
  1247. self.getLocalTrailers = 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 + "/LocalTrailers");
  1255. return self.ajax({
  1256. type: "GET",
  1257. url: url,
  1258. dataType: "json"
  1259. });
  1260. };
  1261. /**
  1262. * Gets special features for an item
  1263. */
  1264. self.getSpecialFeatures = function (userId, itemId) {
  1265. if (!userId) {
  1266. throw new Error("null userId");
  1267. }
  1268. if (!itemId) {
  1269. throw new Error("null itemId");
  1270. }
  1271. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/SpecialFeatures");
  1272. return self.ajax({
  1273. type: "GET",
  1274. url: url,
  1275. dataType: "json"
  1276. });
  1277. };
  1278. /**
  1279. * Marks an item as played or unplayed
  1280. * This should not be used to update playstate following playback.
  1281. * There are separate playstate check-in methods for that. This should be used for a
  1282. * separate option to reset playstate.
  1283. * @param {String} userId
  1284. * @param {String} itemId
  1285. * @param {Boolean} wasPlayed
  1286. */
  1287. self.updatePlayedStatus = function (userId, itemId, wasPlayed) {
  1288. if (!userId) {
  1289. throw new Error("null userId");
  1290. }
  1291. if (!itemId) {
  1292. throw new Error("null itemId");
  1293. }
  1294. var url = self.getUrl("Users/" + userId + "/PlayedItems/" + itemId);
  1295. var method = wasPlayed ? "POST" : "DELETE";
  1296. return self.ajax({
  1297. type: method,
  1298. url: url,
  1299. dataType: "json"
  1300. });
  1301. };
  1302. /**
  1303. * Updates a user's favorite status for an item.
  1304. * @param {String} userId
  1305. * @param {String} itemId
  1306. * @param {Boolean} isFavorite
  1307. */
  1308. self.updateFavoriteStatus = function (userId, itemId, isFavorite) {
  1309. if (!userId) {
  1310. throw new Error("null userId");
  1311. }
  1312. if (!itemId) {
  1313. throw new Error("null itemId");
  1314. }
  1315. var url = self.getUrl("Users/" + userId + "/FavoriteItems/" + itemId);
  1316. var method = isFavorite ? "POST" : "DELETE";
  1317. return self.ajax({
  1318. type: method,
  1319. url: url,
  1320. dataType: "json"
  1321. });
  1322. };
  1323. /**
  1324. * Updates a user's personal rating for an item
  1325. * @param {String} userId
  1326. * @param {String} itemId
  1327. * @param {Boolean} likes
  1328. */
  1329. self.updateUserItemRating = function (userId, itemId, likes) {
  1330. if (!userId) {
  1331. throw new Error("null userId");
  1332. }
  1333. if (!itemId) {
  1334. throw new Error("null itemId");
  1335. }
  1336. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating", {
  1337. likes: likes
  1338. });
  1339. return self.ajax({
  1340. type: "POST",
  1341. url: url
  1342. });
  1343. };
  1344. /**
  1345. * Updates a user's favorite status for an item by name.
  1346. * @param {String} userId
  1347. * @param {String} name
  1348. * @param {Boolean} isFavorite
  1349. */
  1350. self.updateItemByNameFavoriteStatus = function (userId, name, isFavorite) {
  1351. if (!userId) {
  1352. throw new Error("null userId");
  1353. }
  1354. if (!name) {
  1355. throw new Error("null name");
  1356. }
  1357. var url = self.getUrl("Users/" + userId + "/ItemsByName/Favorites/" + name);
  1358. var method = isFavorite ? "POST" : "DELETE";
  1359. return self.ajax({
  1360. type: method,
  1361. url: url,
  1362. dataType: "json"
  1363. });
  1364. };
  1365. /**
  1366. * Updates a user's rating for an item by name.
  1367. * @param {String} userId
  1368. * @param {String} name
  1369. * @param {Boolean} likes
  1370. */
  1371. self.updateItemByNameRating = function (userId, name, likes) {
  1372. if (!userId) {
  1373. throw new Error("null userId");
  1374. }
  1375. if (!name) {
  1376. throw new Error("null name");
  1377. }
  1378. var url = self.getUrl("Users/" + userId + "/ItemsByName/" + name + "/Rating", {
  1379. likes: likes
  1380. });
  1381. return self.ajax({
  1382. type: "POST",
  1383. url: url
  1384. });
  1385. };
  1386. /**
  1387. * Clears a user's rating for an item by name.
  1388. * @param {String} userId
  1389. * @param {String} name
  1390. */
  1391. self.clearItemByNameRating = function (userId, name) {
  1392. if (!userId) {
  1393. throw new Error("null userId");
  1394. }
  1395. if (!name) {
  1396. throw new Error("null name");
  1397. }
  1398. var url = self.getUrl("Users/" + userId + "/ItemsByName/" + name + "/Rating");
  1399. return self.ajax({
  1400. type: "DELETE",
  1401. url: url,
  1402. dataType: "json"
  1403. });
  1404. };
  1405. /**
  1406. * Gets the full user data object for an item by name.
  1407. * @param {String} userId
  1408. * @param {String} name
  1409. */
  1410. self.getItembyNameUserData = function (userId, name) {
  1411. if (!userId) {
  1412. throw new Error("null userId");
  1413. }
  1414. if (!name) {
  1415. throw new Error("null name");
  1416. }
  1417. var url = self.getUrl("Users/" + userId + "/ItemsByName/" + name + "/UserData");
  1418. return self.ajax({
  1419. type: "GET",
  1420. url: url,
  1421. dataType: "json"
  1422. });
  1423. };
  1424. /**
  1425. * Clears a user's personal rating for an item
  1426. * @param {String} userId
  1427. * @param {String} itemId
  1428. */
  1429. self.clearUserItemRating = function (userId, itemId) {
  1430. if (!userId) {
  1431. throw new Error("null userId");
  1432. }
  1433. if (!itemId) {
  1434. throw new Error("null itemId");
  1435. }
  1436. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating");
  1437. return self.ajax({
  1438. type: "DELETE",
  1439. url: url,
  1440. dataType: "json"
  1441. });
  1442. };
  1443. /**
  1444. * Reports the user has started playing something
  1445. * @param {String} userId
  1446. * @param {String} itemId
  1447. */
  1448. self.reportPlaybackStart = function (userId, itemId) {
  1449. if (!userId) {
  1450. throw new Error("null userId");
  1451. }
  1452. if (!itemId) {
  1453. throw new Error("null itemId");
  1454. }
  1455. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId);
  1456. return self.ajax({
  1457. type: "POST",
  1458. url: url,
  1459. dataType: "json"
  1460. });
  1461. };
  1462. /**
  1463. * Reports progress viewing an item
  1464. * @param {String} userId
  1465. * @param {String} itemId
  1466. */
  1467. self.reportPlaybackProgress = function (userId, itemId, positionTicks) {
  1468. if (!userId) {
  1469. throw new Error("null userId");
  1470. }
  1471. if (!itemId) {
  1472. throw new Error("null itemId");
  1473. }
  1474. var params = {
  1475. };
  1476. if (positionTicks) {
  1477. params.positionTicks = positionTicks;
  1478. }
  1479. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId + "/Progress", params);
  1480. return self.ajax({
  1481. type: "POST",
  1482. url: url,
  1483. dataType: "json"
  1484. });
  1485. };
  1486. /**
  1487. * Reports a user has stopped playing an item
  1488. * @param {String} userId
  1489. * @param {String} itemId
  1490. */
  1491. self.reportPlaybackStopped = function (userId, itemId, positionTicks) {
  1492. if (!userId) {
  1493. throw new Error("null userId");
  1494. }
  1495. if (!itemId) {
  1496. throw new Error("null itemId");
  1497. }
  1498. var params = {
  1499. };
  1500. if (positionTicks) {
  1501. params.positionTicks = positionTicks;
  1502. }
  1503. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId, params);
  1504. return self.ajax({
  1505. type: "DELETE",
  1506. url: url,
  1507. dataType: "json"
  1508. });
  1509. };
  1510. }
  1511. }(jQuery, navigator, JSON, window.WebSocket, setTimeout);
  1512. /**
  1513. * Provides a friendly way to create an api client instance using information from the browser's current url
  1514. */
  1515. MediaBrowser.ApiClient.create = function (clientName) {
  1516. var loc = window.location;
  1517. return new MediaBrowser.ApiClient(loc.protocol, loc.hostname, loc.port, clientName);
  1518. };
  1519. /**
  1520. *
  1521. * Secure Hash Algorithm (SHA1)
  1522. * http://www.webtoolkit.info/
  1523. *
  1524. **/
  1525. MediaBrowser.SHA1 = function (msg) {
  1526. function rotate_left(n, s) {
  1527. var t4 = (n << s) | (n >>> (32 - s));
  1528. return t4;
  1529. }
  1530. function lsb_hex(val) {
  1531. var str = "";
  1532. var i;
  1533. var vh;
  1534. var vl;
  1535. for (i = 0; i <= 6; i += 2) {
  1536. vh = (val >>> (i * 4 + 4)) & 0x0f;
  1537. vl = (val >>> (i * 4)) & 0x0f;
  1538. str += vh.toString(16) + vl.toString(16);
  1539. }
  1540. return str;
  1541. }
  1542. function cvt_hex(val) {
  1543. var str = "";
  1544. var i;
  1545. var v;
  1546. for (i = 7; i >= 0; i--) {
  1547. v = (val >>> (i * 4)) & 0x0f;
  1548. str += v.toString(16);
  1549. }
  1550. return str;
  1551. }
  1552. function Utf8Encode(string) {
  1553. string = string.replace(/\r\n/g, "\n");
  1554. var utftext = "";
  1555. for (var n = 0; n < string.length; n++) {
  1556. var c = string.charCodeAt(n);
  1557. if (c < 128) {
  1558. utftext += String.fromCharCode(c);
  1559. }
  1560. else if ((c > 127) && (c < 2048)) {
  1561. utftext += String.fromCharCode((c >> 6) | 192);
  1562. utftext += String.fromCharCode((c & 63) | 128);
  1563. }
  1564. else {
  1565. utftext += String.fromCharCode((c >> 12) | 224);
  1566. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  1567. utftext += String.fromCharCode((c & 63) | 128);
  1568. }
  1569. }
  1570. return utftext;
  1571. }
  1572. var blockstart;
  1573. var i, j;
  1574. var W = new Array(80);
  1575. var H0 = 0x67452301;
  1576. var H1 = 0xEFCDAB89;
  1577. var H2 = 0x98BADCFE;
  1578. var H3 = 0x10325476;
  1579. var H4 = 0xC3D2E1F0;
  1580. var A, B, C, D, E;
  1581. var temp;
  1582. msg = Utf8Encode(msg);
  1583. var msg_len = msg.length;
  1584. var word_array = new Array();
  1585. for (i = 0; i < msg_len - 3; i += 4) {
  1586. j = msg.charCodeAt(i) << 24 | msg.charCodeAt(i + 1) << 16 |
  1587. msg.charCodeAt(i + 2) << 8 | msg.charCodeAt(i + 3);
  1588. word_array.push(j);
  1589. }
  1590. switch (msg_len % 4) {
  1591. case 0:
  1592. i = 0x080000000;
  1593. break;
  1594. case 1:
  1595. i = msg.charCodeAt(msg_len - 1) << 24 | 0x0800000;
  1596. break;
  1597. case 2:
  1598. i = msg.charCodeAt(msg_len - 2) << 24 | msg.charCodeAt(msg_len - 1) << 16 | 0x08000;
  1599. break;
  1600. case 3:
  1601. i = msg.charCodeAt(msg_len - 3) << 24 | msg.charCodeAt(msg_len - 2) << 16 | msg.charCodeAt(msg_len - 1) << 8 | 0x80;
  1602. break;
  1603. }
  1604. word_array.push(i);
  1605. while ((word_array.length % 16) != 14) word_array.push(0);
  1606. word_array.push(msg_len >>> 29);
  1607. word_array.push((msg_len << 3) & 0x0ffffffff);
  1608. for (blockstart = 0; blockstart < word_array.length; blockstart += 16) {
  1609. for (i = 0; i < 16; i++) W[i] = word_array[blockstart + i];
  1610. for (i = 16; i <= 79; i++) W[i] = rotate_left(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
  1611. A = H0;
  1612. B = H1;
  1613. C = H2;
  1614. D = H3;
  1615. E = H4;
  1616. for (i = 0; i <= 19; i++) {
  1617. temp = (rotate_left(A, 5) + ((B & C) | (~B & D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
  1618. E = D;
  1619. D = C;
  1620. C = rotate_left(B, 30);
  1621. B = A;
  1622. A = temp;
  1623. }
  1624. for (i = 20; i <= 39; i++) {
  1625. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
  1626. E = D;
  1627. D = C;
  1628. C = rotate_left(B, 30);
  1629. B = A;
  1630. A = temp;
  1631. }
  1632. for (i = 40; i <= 59; i++) {
  1633. temp = (rotate_left(A, 5) + ((B & C) | (B & D) | (C & D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
  1634. E = D;
  1635. D = C;
  1636. C = rotate_left(B, 30);
  1637. B = A;
  1638. A = temp;
  1639. }
  1640. for (i = 60; i <= 79; i++) {
  1641. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
  1642. E = D;
  1643. D = C;
  1644. C = rotate_left(B, 30);
  1645. B = A;
  1646. A = temp;
  1647. }
  1648. H0 = (H0 + A) & 0x0ffffffff;
  1649. H1 = (H1 + B) & 0x0ffffffff;
  1650. H2 = (H2 + C) & 0x0ffffffff;
  1651. H3 = (H3 + D) & 0x0ffffffff;
  1652. H4 = (H4 + E) & 0x0ffffffff;
  1653. }
  1654. var temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4);
  1655. return temp.toLowerCase();
  1656. };
  1657. (function (jQuery, window, undefined) {
  1658. "use strict";
  1659. var matched, browser;
  1660. jQuery.uaMatch = function (ua) {
  1661. ua = ua.toLowerCase();
  1662. var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
  1663. /(webkit)[ \/]([\w.]+)/.exec(ua) ||
  1664. /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
  1665. /(msie) ([\w.]+)/.exec(ua) ||
  1666. ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
  1667. [];
  1668. var platform_match = /(ipad)/.exec(ua) ||
  1669. /(iphone)/.exec(ua) ||
  1670. /(android)/.exec(ua) ||
  1671. [];
  1672. return {
  1673. browser: match[1] || "",
  1674. version: match[2] || "0",
  1675. platform: platform_match[0] || ""
  1676. };
  1677. };
  1678. matched = jQuery.uaMatch(window.navigator.userAgent);
  1679. browser = {};
  1680. if (matched.browser) {
  1681. browser[matched.browser] = true;
  1682. browser.version = matched.version;
  1683. }
  1684. if (matched.platform) {
  1685. browser[matched.platform] = true
  1686. }
  1687. // Chrome is Webkit, but Webkit is also Safari.
  1688. if (browser.chrome) {
  1689. browser.webkit = true;
  1690. } else if (browser.webkit) {
  1691. browser.safari = true;
  1692. }
  1693. jQuery.browser = browser;
  1694. })(jQuery, window);