ApiClient.js 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502
  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, userId) {
  670. if (!name) {
  671. throw new Error("null name");
  672. }
  673. var options = {};
  674. if (userId) {
  675. options.userId = userId;
  676. }
  677. var url = self.getUrl("Studios/" + encodeName(name), options);
  678. return self.ajax({
  679. type: "GET",
  680. url: url,
  681. dataType: "json"
  682. });
  683. };
  684. /**
  685. * Gets a genre
  686. */
  687. self.getGenre = function (name, userId) {
  688. if (!name) {
  689. throw new Error("null name");
  690. }
  691. var options = {};
  692. if (userId) {
  693. options.userId = userId;
  694. }
  695. var url = self.getUrl("Genres/" + encodeName(name), options);
  696. return self.ajax({
  697. type: "GET",
  698. url: url,
  699. dataType: "json"
  700. });
  701. };
  702. /**
  703. * Gets an artist
  704. */
  705. self.getArtist = function (name, userId) {
  706. if (!name) {
  707. throw new Error("null name");
  708. }
  709. var options = {};
  710. if (userId) {
  711. options.userId = userId;
  712. }
  713. var url = self.getUrl("Artists/" + encodeName(name), options);
  714. return self.ajax({
  715. type: "GET",
  716. url: url,
  717. dataType: "json"
  718. });
  719. };
  720. /**
  721. * Gets a year
  722. */
  723. self.getYear = function (yea, userId) {
  724. if (!name) {
  725. throw new Error("null name");
  726. }
  727. var options = {};
  728. if (userId) {
  729. options.userId = userId;
  730. }
  731. var url = self.getUrl("Years/" + encodeName(name), options);
  732. return self.ajax({
  733. type: "GET",
  734. url: url,
  735. dataType: "json"
  736. });
  737. };
  738. /**
  739. * Gets a Person
  740. */
  741. self.getPerson = function (name, userId) {
  742. if (!name) {
  743. throw new Error("null name");
  744. }
  745. var options = {};
  746. if (userId) {
  747. options.userId = userId;
  748. }
  749. var url = self.getUrl("Persons/" + encodeName(name), options);
  750. return self.ajax({
  751. type: "GET",
  752. url: url,
  753. dataType: "json"
  754. });
  755. };
  756. /**
  757. * Gets weather info
  758. * @param {String} location - us zip code / city, state, country / city, country
  759. * Omit location to get weather info using stored server configuration value
  760. */
  761. self.getWeatherInfo = function (location) {
  762. var url = self.getUrl("weather", {
  763. location: location
  764. });
  765. return self.ajax({
  766. type: "GET",
  767. url: url,
  768. dataType: "json"
  769. });
  770. };
  771. /**
  772. * Gets all users from the server
  773. */
  774. self.getUsers = function () {
  775. var url = self.getUrl("users");
  776. return self.ajax({
  777. type: "GET",
  778. url: url,
  779. dataType: "json"
  780. });
  781. };
  782. /**
  783. * Gets all available parental ratings from the server
  784. */
  785. self.getParentalRatings = function () {
  786. var url = self.getUrl("Localization/ParentalRatings");
  787. return self.ajax({
  788. type: "GET",
  789. url: url,
  790. dataType: "json"
  791. });
  792. };
  793. /**
  794. * Gets a list of all available conrete BaseItem types from the server
  795. */
  796. self.getItemTypes = function (options) {
  797. var url = self.getUrl("Library/ItemTypes", options);
  798. return self.ajax({
  799. type: "GET",
  800. url: url,
  801. dataType: "json"
  802. });
  803. };
  804. /**
  805. * Constructs a url for a user image
  806. * @param {String} userId
  807. * @param {Object} options
  808. * Options supports the following properties:
  809. * width - download the image at a fixed width
  810. * height - download the image at a fixed height
  811. * maxWidth - download the image at a maxWidth
  812. * maxHeight - download the image at a maxHeight
  813. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  814. * For best results do not specify both width and height together, as aspect ratio might be altered.
  815. */
  816. self.getUserImageUrl = function (userId, options) {
  817. if (!userId) {
  818. throw new Error("null userId");
  819. }
  820. options = options || {
  821. };
  822. var url = "Users/" + userId + "/Images/" + options.type;
  823. if (options.index != null) {
  824. url += "/" + options.index;
  825. }
  826. // Don't put these on the query string
  827. delete options.type;
  828. delete options.index;
  829. return self.getUrl(url, options);
  830. };
  831. /**
  832. * Constructs a url for a person image
  833. * @param {String} name
  834. * @param {Object} options
  835. * Options supports the following properties:
  836. * width - download the image at a fixed width
  837. * height - download the image at a fixed height
  838. * maxWidth - download the image at a maxWidth
  839. * maxHeight - download the image at a maxHeight
  840. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  841. * For best results do not specify both width and height together, as aspect ratio might be altered.
  842. */
  843. self.getPersonImageUrl = function (name, options) {
  844. if (!name) {
  845. throw new Error("null name");
  846. }
  847. options = options || {
  848. };
  849. var url = "Persons/" + encodeName(name) + "/Images/" + options.type;
  850. if (options.index != null) {
  851. url += "/" + options.index;
  852. }
  853. // Don't put these on the query string
  854. delete options.type;
  855. delete options.index;
  856. return self.getUrl(url, options);
  857. };
  858. /**
  859. * Constructs a url for a year image
  860. * @param {String} year
  861. * @param {Object} options
  862. * Options supports the following properties:
  863. * width - download the image at a fixed width
  864. * height - download the image at a fixed height
  865. * maxWidth - download the image at a maxWidth
  866. * maxHeight - download the image at a maxHeight
  867. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  868. * For best results do not specify both width and height together, as aspect ratio might be altered.
  869. */
  870. self.getYearImageUrl = function (year, options) {
  871. if (!year) {
  872. throw new Error("null year");
  873. }
  874. options = options || {
  875. };
  876. var url = "Years/" + year + "/Images/" + options.type;
  877. if (options.index != null) {
  878. url += "/" + options.index;
  879. }
  880. // Don't put these on the query string
  881. delete options.type;
  882. delete options.index;
  883. return self.getUrl(url, options);
  884. };
  885. /**
  886. * Constructs a url for a genre image
  887. * @param {String} name
  888. * @param {Object} options
  889. * Options supports the following properties:
  890. * width - download the image at a fixed width
  891. * height - download the image at a fixed height
  892. * maxWidth - download the image at a maxWidth
  893. * maxHeight - download the image at a maxHeight
  894. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  895. * For best results do not specify both width and height together, as aspect ratio might be altered.
  896. */
  897. self.getGenreImageUrl = function (name, options) {
  898. if (!name) {
  899. throw new Error("null name");
  900. }
  901. options = options || {
  902. };
  903. var url = "Genres/" + encodeName(name) + "/Images/" + options.type;
  904. if (options.index != null) {
  905. url += "/" + options.index;
  906. }
  907. // Don't put these on the query string
  908. delete options.type;
  909. delete options.index;
  910. return self.getUrl(url, options);
  911. };
  912. /**
  913. * Constructs a url for a artist image
  914. * @param {String} name
  915. * @param {Object} options
  916. * Options supports the following properties:
  917. * width - download the image at a fixed width
  918. * height - download the image at a fixed height
  919. * maxWidth - download the image at a maxWidth
  920. * maxHeight - download the image at a maxHeight
  921. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  922. * For best results do not specify both width and height together, as aspect ratio might be altered.
  923. */
  924. self.getArtistImageUrl = function (name, options) {
  925. if (!name) {
  926. throw new Error("null name");
  927. }
  928. options = options || {
  929. };
  930. var url = "Artists/" + encodeName(name) + "/Images/" + options.type;
  931. if (options.index != null) {
  932. url += "/" + options.index;
  933. }
  934. // Don't put these on the query string
  935. delete options.type;
  936. delete options.index;
  937. return self.getUrl(url, options);
  938. };
  939. /**
  940. * Constructs a url for a studio image
  941. * @param {String} name
  942. * @param {Object} options
  943. * Options supports the following properties:
  944. * width - download the image at a fixed width
  945. * height - download the image at a fixed height
  946. * maxWidth - download the image at a maxWidth
  947. * maxHeight - download the image at a maxHeight
  948. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  949. * For best results do not specify both width and height together, as aspect ratio might be altered.
  950. */
  951. self.getStudioImageUrl = function (name, options) {
  952. if (!name) {
  953. throw new Error("null name");
  954. }
  955. options = options || {
  956. };
  957. var url = "Studios/" + encodeName(name) + "/Images/" + options.type;
  958. if (options.index != null) {
  959. url += "/" + options.index;
  960. }
  961. // Don't put these on the query string
  962. delete options.type;
  963. delete options.index;
  964. return self.getUrl(url, options);
  965. };
  966. /**
  967. * Constructs a url for an item image
  968. * @param {String} itemId
  969. * @param {Object} options
  970. * Options supports the following properties:
  971. * type - Primary, logo, backdrop, etc. See the server-side enum ImageType
  972. * index - When downloading a backdrop, use this to specify which one (omitting is equivalent to zero)
  973. * width - download the image at a fixed width
  974. * height - download the image at a fixed height
  975. * maxWidth - download the image at a maxWidth
  976. * maxHeight - download the image at a maxHeight
  977. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  978. * For best results do not specify both width and height together, as aspect ratio might be altered.
  979. */
  980. self.getImageUrl = function (itemId, options) {
  981. if (!itemId) {
  982. throw new Error("itemId cannot be empty");
  983. }
  984. options = options || {
  985. };
  986. var url = "Items/" + itemId + "/Images/" + options.type;
  987. if (options.index != null) {
  988. url += "/" + options.index;
  989. }
  990. // Don't put these on the query string
  991. delete options.type;
  992. delete options.index;
  993. return self.getUrl(url, options);
  994. };
  995. /**
  996. * Constructs a url for an item logo image
  997. * If the item doesn't have a logo, it will inherit a logo from a parent
  998. * @param {Object} item A BaseItem
  999. * @param {Object} options
  1000. * Options supports the following properties:
  1001. * width - download the image at a fixed width
  1002. * height - download the image at a fixed height
  1003. * maxWidth - download the image at a maxWidth
  1004. * maxHeight - download the image at a maxHeight
  1005. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1006. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1007. */
  1008. self.getLogoImageUrl = function (item, options) {
  1009. if (!item) {
  1010. throw new Error("null item");
  1011. }
  1012. options = options || {
  1013. };
  1014. options.imageType = "logo";
  1015. var logoItemId = item.HasLogo ? item.Id : item.ParentLogoItemId;
  1016. return logoItemId ? self.getImageUrl(logoItemId, options) : null;
  1017. };
  1018. /**
  1019. * Constructs an array of backdrop image url's for an item
  1020. * If the item doesn't have any backdrops, it will inherit them from a parent
  1021. * @param {Object} item A BaseItem
  1022. * @param {Object} options
  1023. * Options supports the following properties:
  1024. * width - download the image at a fixed width
  1025. * height - download the image at a fixed height
  1026. * maxWidth - download the image at a maxWidth
  1027. * maxHeight - download the image at a maxHeight
  1028. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1029. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1030. */
  1031. self.getBackdropImageUrl = function (item, options) {
  1032. if (!item) {
  1033. throw new Error("null item");
  1034. }
  1035. options = options || {
  1036. };
  1037. options.imageType = "backdrop";
  1038. var backdropItemId;
  1039. var backdropCount;
  1040. if (!item.BackdropCount) {
  1041. backdropItemId = item.ParentBackdropItemId;
  1042. backdropCount = item.ParentBackdropCount || 0;
  1043. } else {
  1044. backdropItemId = item.Id;
  1045. backdropCount = item.BackdropCount;
  1046. }
  1047. if (!backdropItemId) {
  1048. return [];
  1049. }
  1050. var files = [];
  1051. for (var i = 0; i < backdropCount; i++) {
  1052. options.imageIndex = i;
  1053. files[i] = self.getImageUrl(backdropItemId, options);
  1054. }
  1055. return files;
  1056. };
  1057. /**
  1058. * Authenticates a user
  1059. * @param {String} userId
  1060. * @param {String} password
  1061. */
  1062. self.authenticateUser = function (userId, password) {
  1063. if (!userId) {
  1064. throw new Error("null userId");
  1065. }
  1066. var url = self.getUrl("Users/" + userId + "/authenticate");
  1067. var postData = {
  1068. password: MediaBrowser.SHA1(password || "")
  1069. };
  1070. return self.ajax({
  1071. type: "POST",
  1072. url: url,
  1073. data: JSON.stringify(postData),
  1074. contentType: "application/json"
  1075. });
  1076. };
  1077. /**
  1078. * Updates a user's password
  1079. * @param {String} userId
  1080. * @param {String} currentPassword
  1081. * @param {String} newPassword
  1082. */
  1083. self.updateUserPassword = function (userId, currentPassword, newPassword) {
  1084. if (!userId) {
  1085. throw new Error("null userId");
  1086. }
  1087. var url = self.getUrl("Users/" + userId + "/Password");
  1088. var postData = {
  1089. };
  1090. postData.currentPassword = MediaBrowser.SHA1(currentPassword);
  1091. if (newPassword) {
  1092. postData.newPassword = newPassword;
  1093. }
  1094. return self.ajax({
  1095. type: "POST",
  1096. url: url,
  1097. data: postData
  1098. });
  1099. };
  1100. /**
  1101. * Resets a user's password
  1102. * @param {String} userId
  1103. */
  1104. self.resetUserPassword = function (userId) {
  1105. if (!userId) {
  1106. throw new Error("null userId");
  1107. }
  1108. var url = self.getUrl("Users/" + userId + "/Password");
  1109. var postData = {
  1110. };
  1111. postData.resetPassword = true;
  1112. return self.ajax({
  1113. type: "POST",
  1114. url: url,
  1115. data: postData
  1116. });
  1117. };
  1118. /**
  1119. * Updates the server's configuration
  1120. * @param {Object} configuration
  1121. */
  1122. self.updateServerConfiguration = function (configuration) {
  1123. if (!configuration) {
  1124. throw new Error("null configuration");
  1125. }
  1126. var url = self.getUrl("System/Configuration");
  1127. return self.ajax({
  1128. type: "POST",
  1129. url: url,
  1130. data: JSON.stringify(configuration),
  1131. contentType: "application/json"
  1132. });
  1133. };
  1134. /**
  1135. * Updates plugin security info
  1136. */
  1137. self.updatePluginSecurityInfo = function (info) {
  1138. var url = self.getUrl("Plugins/SecurityInfo");
  1139. return self.ajax({
  1140. type: "POST",
  1141. url: url,
  1142. data: JSON.stringify(info),
  1143. contentType: "application/json"
  1144. });
  1145. };
  1146. /**
  1147. * Creates a user
  1148. * @param {Object} user
  1149. */
  1150. self.createUser = function (user) {
  1151. if (!user) {
  1152. throw new Error("null user");
  1153. }
  1154. var url = self.getUrl("Users");
  1155. return self.ajax({
  1156. type: "POST",
  1157. url: url,
  1158. data: JSON.stringify(user),
  1159. dataType: "json",
  1160. contentType: "application/json"
  1161. });
  1162. };
  1163. /**
  1164. * Updates a user
  1165. * @param {Object} user
  1166. */
  1167. self.updateUser = function (user) {
  1168. if (!user) {
  1169. throw new Error("null user");
  1170. }
  1171. var url = self.getUrl("Users/" + user.Id);
  1172. return self.ajax({
  1173. type: "POST",
  1174. url: url,
  1175. data: JSON.stringify(user),
  1176. contentType: "application/json"
  1177. });
  1178. };
  1179. /**
  1180. * Updates the Triggers for a ScheduledTask
  1181. * @param {String} id
  1182. * @param {Object} triggers
  1183. */
  1184. self.updateScheduledTaskTriggers = function (id, triggers) {
  1185. if (!id) {
  1186. throw new Error("null id");
  1187. }
  1188. if (!triggers) {
  1189. throw new Error("null triggers");
  1190. }
  1191. var url = self.getUrl("ScheduledTasks/" + id + "/Triggers");
  1192. return self.ajax({
  1193. type: "POST",
  1194. url: url,
  1195. data: JSON.stringify(triggers),
  1196. contentType: "application/json"
  1197. });
  1198. };
  1199. /**
  1200. * Updates a plugin's configuration
  1201. * @param {String} Id
  1202. * @param {Object} configuration
  1203. */
  1204. self.updatePluginConfiguration = function (id, configuration) {
  1205. if (!id) {
  1206. throw new Error("null Id");
  1207. }
  1208. if (!configuration) {
  1209. throw new Error("null configuration");
  1210. }
  1211. var url = self.getUrl("Plugins/" + id + "/Configuration");
  1212. return self.ajax({
  1213. type: "POST",
  1214. url: url,
  1215. data: JSON.stringify(configuration),
  1216. contentType: "application/json"
  1217. });
  1218. };
  1219. /**
  1220. * Gets items based on a query, typically for children of a folder
  1221. * @param {String} userId
  1222. * @param {Object} options
  1223. * Options accepts the following properties:
  1224. * itemId - Localize the search to a specific folder (root if omitted)
  1225. * startIndex - Use for paging
  1226. * limit - Use to limit results to a certain number of items
  1227. * filter - Specify one or more ItemFilters, comma delimeted (see server-side enum)
  1228. * sortBy - Specify an ItemSortBy (comma-delimeted list see server-side enum)
  1229. * sortOrder - ascending/descending
  1230. * fields - additional fields to include aside from basic info. This is a comma delimited list. See server-side enum ItemFields.
  1231. * index - the name of the dynamic, localized index function
  1232. * dynamicSortBy - the name of the dynamic localized sort function
  1233. * recursive - Whether or not the query should be recursive
  1234. * searchTerm - search term to use as a filter
  1235. */
  1236. self.getItems = function (userId, options) {
  1237. if (!userId) {
  1238. throw new Error("null userId");
  1239. }
  1240. var url = self.getUrl("Users/" + userId + "/Items", options);
  1241. return self.ajax({
  1242. type: "GET",
  1243. url: url,
  1244. dataType: "json"
  1245. });
  1246. };
  1247. /**
  1248. Gets artists from an item
  1249. */
  1250. self.getArtists = function (userId, options) {
  1251. if (!userId) {
  1252. throw new Error("null userId");
  1253. }
  1254. options = options || {};
  1255. options.userId = userId;
  1256. var url = self.getUrl("Artists", options);
  1257. return self.ajax({
  1258. type: "GET",
  1259. url: url,
  1260. dataType: "json"
  1261. });
  1262. };
  1263. /**
  1264. Gets genres from an item
  1265. */
  1266. self.getGenres = function (userId, options) {
  1267. if (!userId) {
  1268. throw new Error("null userId");
  1269. }
  1270. options = options || {};
  1271. options.userId = userId;
  1272. var url = self.getUrl("Genres", options);
  1273. return self.ajax({
  1274. type: "GET",
  1275. url: url,
  1276. dataType: "json"
  1277. });
  1278. };
  1279. /**
  1280. Gets people from an item
  1281. */
  1282. self.getPeople = function (userId, options) {
  1283. if (!userId) {
  1284. throw new Error("null userId");
  1285. }
  1286. options = options || {};
  1287. options.userId = userId;
  1288. var url = self.getUrl("Persons", options);
  1289. return self.ajax({
  1290. type: "GET",
  1291. url: url,
  1292. dataType: "json"
  1293. });
  1294. };
  1295. /**
  1296. Gets studios from an item
  1297. */
  1298. self.getStudios = function (userId, options) {
  1299. if (!userId) {
  1300. throw new Error("null userId");
  1301. }
  1302. options = options || {};
  1303. options.userId = userId;
  1304. var url = self.getUrl("Studios", options);
  1305. return self.ajax({
  1306. type: "GET",
  1307. url: url,
  1308. dataType: "json"
  1309. });
  1310. };
  1311. /**
  1312. * Gets local trailers for an item
  1313. */
  1314. self.getLocalTrailers = function (userId, itemId) {
  1315. if (!userId) {
  1316. throw new Error("null userId");
  1317. }
  1318. if (!itemId) {
  1319. throw new Error("null itemId");
  1320. }
  1321. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/LocalTrailers");
  1322. return self.ajax({
  1323. type: "GET",
  1324. url: url,
  1325. dataType: "json"
  1326. });
  1327. };
  1328. /**
  1329. * Gets special features for an item
  1330. */
  1331. self.getSpecialFeatures = function (userId, itemId) {
  1332. if (!userId) {
  1333. throw new Error("null userId");
  1334. }
  1335. if (!itemId) {
  1336. throw new Error("null itemId");
  1337. }
  1338. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/SpecialFeatures");
  1339. return self.ajax({
  1340. type: "GET",
  1341. url: url,
  1342. dataType: "json"
  1343. });
  1344. };
  1345. /**
  1346. * Marks an item as played or unplayed
  1347. * This should not be used to update playstate following playback.
  1348. * There are separate playstate check-in methods for that. This should be used for a
  1349. * separate option to reset playstate.
  1350. * @param {String} userId
  1351. * @param {String} itemId
  1352. * @param {Boolean} wasPlayed
  1353. */
  1354. self.updatePlayedStatus = function (userId, itemId, wasPlayed) {
  1355. if (!userId) {
  1356. throw new Error("null userId");
  1357. }
  1358. if (!itemId) {
  1359. throw new Error("null itemId");
  1360. }
  1361. var url = self.getUrl("Users/" + userId + "/PlayedItems/" + itemId);
  1362. var method = wasPlayed ? "POST" : "DELETE";
  1363. return self.ajax({
  1364. type: method,
  1365. url: url
  1366. });
  1367. };
  1368. /**
  1369. * Updates a user's favorite status for an item.
  1370. * @param {String} userId
  1371. * @param {String} itemId
  1372. * @param {Boolean} isFavorite
  1373. */
  1374. self.updateFavoriteStatus = function (userId, itemId, isFavorite) {
  1375. if (!userId) {
  1376. throw new Error("null userId");
  1377. }
  1378. if (!itemId) {
  1379. throw new Error("null itemId");
  1380. }
  1381. var url = self.getUrl("Users/" + userId + "/FavoriteItems/" + itemId);
  1382. var method = isFavorite ? "POST" : "DELETE";
  1383. return self.ajax({
  1384. type: method,
  1385. url: url
  1386. });
  1387. };
  1388. /**
  1389. * Updates a user's personal rating for an item
  1390. * @param {String} userId
  1391. * @param {String} itemId
  1392. * @param {Boolean} likes
  1393. */
  1394. self.updateUserItemRating = function (userId, itemId, likes) {
  1395. if (!userId) {
  1396. throw new Error("null userId");
  1397. }
  1398. if (!itemId) {
  1399. throw new Error("null itemId");
  1400. }
  1401. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating", {
  1402. likes: likes
  1403. });
  1404. return self.ajax({
  1405. type: "POST",
  1406. url: url
  1407. });
  1408. };
  1409. /**
  1410. * Updates a user's favorite status for an item by name.
  1411. * @param {String} userId
  1412. * @param {String} name
  1413. * @param {Boolean} isFavorite
  1414. */
  1415. self.updateFavoriteArtistStatus = function (userId, name, isFavorite) {
  1416. if (!userId) {
  1417. throw new Error("null userId");
  1418. }
  1419. if (!name) {
  1420. throw new Error("null name");
  1421. }
  1422. var url = self.getUrl("Users/" + userId + "/Favorites/Artists/" + encodeName(name));
  1423. var method = isFavorite ? "POST" : "DELETE";
  1424. return self.ajax({
  1425. type: method,
  1426. url: url
  1427. });
  1428. };
  1429. self.updateFavoritePersonStatus = function (userId, name, isFavorite) {
  1430. if (!userId) {
  1431. throw new Error("null userId");
  1432. }
  1433. if (!name) {
  1434. throw new Error("null name");
  1435. }
  1436. var url = self.getUrl("Users/" + userId + "/Favorites/Persons/" + encodeName(name));
  1437. var method = isFavorite ? "POST" : "DELETE";
  1438. return self.ajax({
  1439. type: method,
  1440. url: url
  1441. });
  1442. };
  1443. self.updateFavoriteStudioStatus = function (userId, name, isFavorite) {
  1444. if (!userId) {
  1445. throw new Error("null userId");
  1446. }
  1447. if (!name) {
  1448. throw new Error("null name");
  1449. }
  1450. var url = self.getUrl("Users/" + userId + "/Favorites/Studios/" + encodeName(name));
  1451. var method = isFavorite ? "POST" : "DELETE";
  1452. return self.ajax({
  1453. type: method,
  1454. url: url
  1455. });
  1456. };
  1457. self.updateFavoriteGenreStatus = function (userId, name, isFavorite) {
  1458. if (!userId) {
  1459. throw new Error("null userId");
  1460. }
  1461. if (!name) {
  1462. throw new Error("null name");
  1463. }
  1464. var url = self.getUrl("Users/" + userId + "/Favorites/Genres/" + encodeName(name));
  1465. var method = isFavorite ? "POST" : "DELETE";
  1466. return self.ajax({
  1467. type: method,
  1468. url: url
  1469. });
  1470. };
  1471. /**
  1472. * Updates a user's rating for an item by name.
  1473. * @param {String} userId
  1474. * @param {String} name
  1475. * @param {Boolean} likes
  1476. */
  1477. self.updateArtistRating = function (userId, name, likes) {
  1478. if (!userId) {
  1479. throw new Error("null userId");
  1480. }
  1481. if (!name) {
  1482. throw new Error("null name");
  1483. }
  1484. var url = self.getUrl("Users/" + userId + "/Ratings/Artists/" + encodeName(name), {
  1485. likes: likes
  1486. });
  1487. return self.ajax({
  1488. type: "POST",
  1489. url: url
  1490. });
  1491. };
  1492. self.updatePersonRating = function (userId, name, likes) {
  1493. if (!userId) {
  1494. throw new Error("null userId");
  1495. }
  1496. if (!name) {
  1497. throw new Error("null name");
  1498. }
  1499. var url = self.getUrl("Users/" + userId + "/Ratings/Persons/" + encodeName(name), {
  1500. likes: likes
  1501. });
  1502. return self.ajax({
  1503. type: "POST",
  1504. url: url
  1505. });
  1506. };
  1507. self.updateStudioRating = function (userId, name, likes) {
  1508. if (!userId) {
  1509. throw new Error("null userId");
  1510. }
  1511. if (!name) {
  1512. throw new Error("null name");
  1513. }
  1514. var url = self.getUrl("Users/" + userId + "/Ratings/Studios/" + encodeName(name), {
  1515. likes: likes
  1516. });
  1517. return self.ajax({
  1518. type: "POST",
  1519. url: url
  1520. });
  1521. };
  1522. self.updateGenreRating = function (userId, name, likes) {
  1523. if (!userId) {
  1524. throw new Error("null userId");
  1525. }
  1526. if (!name) {
  1527. throw new Error("null name");
  1528. }
  1529. var url = self.getUrl("Users/" + userId + "/Ratings/Genres/" + encodeName(name), {
  1530. likes: likes
  1531. });
  1532. return self.ajax({
  1533. type: "POST",
  1534. url: url
  1535. });
  1536. };
  1537. /**
  1538. * Clears a user's rating for an item by name.
  1539. * @param {String} userId
  1540. * @param {String} name
  1541. */
  1542. self.clearArtistRating = function (userId, name) {
  1543. if (!userId) {
  1544. throw new Error("null userId");
  1545. }
  1546. if (!name) {
  1547. throw new Error("null name");
  1548. }
  1549. var url = self.getUrl("Users/" + userId + "/Ratings/Artists/" + encodeName(name));
  1550. return self.ajax({
  1551. type: "DELETE",
  1552. url: url
  1553. });
  1554. };
  1555. self.clearPersonRating = function (userId, name) {
  1556. if (!userId) {
  1557. throw new Error("null userId");
  1558. }
  1559. if (!name) {
  1560. throw new Error("null name");
  1561. }
  1562. var url = self.getUrl("Users/" + userId + "/Ratings/Persons/" + encodeName(name));
  1563. return self.ajax({
  1564. type: "DELETE",
  1565. url: url
  1566. });
  1567. };
  1568. self.clearStudioRating = function (userId, name) {
  1569. if (!userId) {
  1570. throw new Error("null userId");
  1571. }
  1572. if (!name) {
  1573. throw new Error("null name");
  1574. }
  1575. var url = self.getUrl("Users/" + userId + "/Ratings/Studios/" + encodeName(name));
  1576. return self.ajax({
  1577. type: "DELETE",
  1578. url: url
  1579. });
  1580. };
  1581. self.clearGenreRating = function (userId, name) {
  1582. if (!userId) {
  1583. throw new Error("null userId");
  1584. }
  1585. if (!name) {
  1586. throw new Error("null name");
  1587. }
  1588. var url = self.getUrl("Users/" + userId + "/Ratings/Genres/" + encodeName(name));
  1589. return self.ajax({
  1590. type: "DELETE",
  1591. url: url
  1592. });
  1593. };
  1594. /**
  1595. Gets a variety of item counts that a person appears in
  1596. */
  1597. self.getPersonItemCounts = function (userId, name) {
  1598. if (!userId) {
  1599. throw new Error("null userId");
  1600. }
  1601. if (!name) {
  1602. throw new Error("null name");
  1603. }
  1604. var url = self.getUrl("Persons/" + encodeName(name) + "/Counts", {
  1605. userId: userId
  1606. });
  1607. return self.ajax({
  1608. type: "GET",
  1609. url: url,
  1610. dataType: "json"
  1611. });
  1612. };
  1613. /**
  1614. Gets a variety of item counts that a genre appears in
  1615. */
  1616. self.getGenreItemCounts = function (userId, name) {
  1617. if (!userId) {
  1618. throw new Error("null userId");
  1619. }
  1620. if (!name) {
  1621. throw new Error("null name");
  1622. }
  1623. var url = self.getUrl("Genres/" + encodeName(name) + "/Counts", {
  1624. userId: userId
  1625. });
  1626. return self.ajax({
  1627. type: "GET",
  1628. url: url,
  1629. dataType: "json"
  1630. });
  1631. };
  1632. /**
  1633. Gets a variety of item counts that an artist appears in
  1634. */
  1635. self.getArtistItemCounts = function (userId, name) {
  1636. if (!userId) {
  1637. throw new Error("null userId");
  1638. }
  1639. if (!name) {
  1640. throw new Error("null name");
  1641. }
  1642. var url = self.getUrl("Artists/" + encodeName(name) + "/Counts", {
  1643. userId: userId
  1644. });
  1645. return self.ajax({
  1646. type: "GET",
  1647. url: url,
  1648. dataType: "json"
  1649. });
  1650. };
  1651. /**
  1652. Gets a variety of item counts that a studio appears in
  1653. */
  1654. self.getStudioItemCounts = function (userId, name) {
  1655. if (!userId) {
  1656. throw new Error("null userId");
  1657. }
  1658. if (!name) {
  1659. throw new Error("null name");
  1660. }
  1661. var url = self.getUrl("Studios/" + encodeName(name) + "/Counts", {
  1662. userId: userId
  1663. });
  1664. return self.ajax({
  1665. type: "GET",
  1666. url: url,
  1667. dataType: "json"
  1668. });
  1669. };
  1670. /**
  1671. * Clears a user's personal rating for an item
  1672. * @param {String} userId
  1673. * @param {String} itemId
  1674. */
  1675. self.clearUserItemRating = function (userId, itemId) {
  1676. if (!userId) {
  1677. throw new Error("null userId");
  1678. }
  1679. if (!itemId) {
  1680. throw new Error("null itemId");
  1681. }
  1682. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating");
  1683. return self.ajax({
  1684. type: "DELETE",
  1685. url: url
  1686. });
  1687. };
  1688. /**
  1689. * Reports the user has started playing something
  1690. * @param {String} userId
  1691. * @param {String} itemId
  1692. */
  1693. self.reportPlaybackStart = function (userId, itemId) {
  1694. if (!userId) {
  1695. throw new Error("null userId");
  1696. }
  1697. if (!itemId) {
  1698. throw new Error("null itemId");
  1699. }
  1700. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId);
  1701. return self.ajax({
  1702. type: "POST",
  1703. url: url
  1704. });
  1705. };
  1706. /**
  1707. * Reports progress viewing an item
  1708. * @param {String} userId
  1709. * @param {String} itemId
  1710. */
  1711. self.reportPlaybackProgress = function (userId, itemId, positionTicks) {
  1712. if (!userId) {
  1713. throw new Error("null userId");
  1714. }
  1715. if (!itemId) {
  1716. throw new Error("null itemId");
  1717. }
  1718. var params = {
  1719. };
  1720. if (positionTicks) {
  1721. params.positionTicks = positionTicks;
  1722. }
  1723. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId + "/Progress", params);
  1724. return self.ajax({
  1725. type: "POST",
  1726. url: url
  1727. });
  1728. };
  1729. /**
  1730. * Reports a user has stopped playing an item
  1731. * @param {String} userId
  1732. * @param {String} itemId
  1733. */
  1734. self.reportPlaybackStopped = function (userId, itemId, positionTicks) {
  1735. if (!userId) {
  1736. throw new Error("null userId");
  1737. }
  1738. if (!itemId) {
  1739. throw new Error("null itemId");
  1740. }
  1741. var params = {
  1742. };
  1743. if (positionTicks) {
  1744. params.positionTicks = positionTicks;
  1745. }
  1746. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId, params);
  1747. return self.ajax({
  1748. type: "DELETE",
  1749. url: url
  1750. });
  1751. };
  1752. }
  1753. }(jQuery, navigator, JSON, window.WebSocket, setTimeout);
  1754. /**
  1755. * Provides a friendly way to create an api client instance using information from the browser's current url
  1756. */
  1757. MediaBrowser.ApiClient.create = function (clientName) {
  1758. var loc = window.location;
  1759. return new MediaBrowser.ApiClient(loc.protocol, loc.hostname, loc.port, clientName);
  1760. };
  1761. /**
  1762. *
  1763. * Secure Hash Algorithm (SHA1)
  1764. * http://www.webtoolkit.info/
  1765. *
  1766. **/
  1767. MediaBrowser.SHA1 = function (msg) {
  1768. function rotate_left(n, s) {
  1769. var t4 = (n << s) | (n >>> (32 - s));
  1770. return t4;
  1771. }
  1772. function lsb_hex(val) {
  1773. var str = "";
  1774. var i;
  1775. var vh;
  1776. var vl;
  1777. for (i = 0; i <= 6; i += 2) {
  1778. vh = (val >>> (i * 4 + 4)) & 0x0f;
  1779. vl = (val >>> (i * 4)) & 0x0f;
  1780. str += vh.toString(16) + vl.toString(16);
  1781. }
  1782. return str;
  1783. }
  1784. function cvt_hex(val) {
  1785. var str = "";
  1786. var i;
  1787. var v;
  1788. for (i = 7; i >= 0; i--) {
  1789. v = (val >>> (i * 4)) & 0x0f;
  1790. str += v.toString(16);
  1791. }
  1792. return str;
  1793. }
  1794. function Utf8Encode(string) {
  1795. string = string.replace(/\r\n/g, "\n");
  1796. var utftext = "";
  1797. for (var n = 0; n < string.length; n++) {
  1798. var c = string.charCodeAt(n);
  1799. if (c < 128) {
  1800. utftext += String.fromCharCode(c);
  1801. }
  1802. else if ((c > 127) && (c < 2048)) {
  1803. utftext += String.fromCharCode((c >> 6) | 192);
  1804. utftext += String.fromCharCode((c & 63) | 128);
  1805. }
  1806. else {
  1807. utftext += String.fromCharCode((c >> 12) | 224);
  1808. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  1809. utftext += String.fromCharCode((c & 63) | 128);
  1810. }
  1811. }
  1812. return utftext;
  1813. }
  1814. var blockstart;
  1815. var i, j;
  1816. var W = new Array(80);
  1817. var H0 = 0x67452301;
  1818. var H1 = 0xEFCDAB89;
  1819. var H2 = 0x98BADCFE;
  1820. var H3 = 0x10325476;
  1821. var H4 = 0xC3D2E1F0;
  1822. var A, B, C, D, E;
  1823. var temp;
  1824. msg = Utf8Encode(msg);
  1825. var msg_len = msg.length;
  1826. var word_array = new Array();
  1827. for (i = 0; i < msg_len - 3; i += 4) {
  1828. j = msg.charCodeAt(i) << 24 | msg.charCodeAt(i + 1) << 16 |
  1829. msg.charCodeAt(i + 2) << 8 | msg.charCodeAt(i + 3);
  1830. word_array.push(j);
  1831. }
  1832. switch (msg_len % 4) {
  1833. case 0:
  1834. i = 0x080000000;
  1835. break;
  1836. case 1:
  1837. i = msg.charCodeAt(msg_len - 1) << 24 | 0x0800000;
  1838. break;
  1839. case 2:
  1840. i = msg.charCodeAt(msg_len - 2) << 24 | msg.charCodeAt(msg_len - 1) << 16 | 0x08000;
  1841. break;
  1842. case 3:
  1843. i = msg.charCodeAt(msg_len - 3) << 24 | msg.charCodeAt(msg_len - 2) << 16 | msg.charCodeAt(msg_len - 1) << 8 | 0x80;
  1844. break;
  1845. }
  1846. word_array.push(i);
  1847. while ((word_array.length % 16) != 14) word_array.push(0);
  1848. word_array.push(msg_len >>> 29);
  1849. word_array.push((msg_len << 3) & 0x0ffffffff);
  1850. for (blockstart = 0; blockstart < word_array.length; blockstart += 16) {
  1851. for (i = 0; i < 16; i++) W[i] = word_array[blockstart + i];
  1852. for (i = 16; i <= 79; i++) W[i] = rotate_left(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
  1853. A = H0;
  1854. B = H1;
  1855. C = H2;
  1856. D = H3;
  1857. E = H4;
  1858. for (i = 0; i <= 19; i++) {
  1859. temp = (rotate_left(A, 5) + ((B & C) | (~B & D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
  1860. E = D;
  1861. D = C;
  1862. C = rotate_left(B, 30);
  1863. B = A;
  1864. A = temp;
  1865. }
  1866. for (i = 20; i <= 39; i++) {
  1867. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
  1868. E = D;
  1869. D = C;
  1870. C = rotate_left(B, 30);
  1871. B = A;
  1872. A = temp;
  1873. }
  1874. for (i = 40; i <= 59; i++) {
  1875. temp = (rotate_left(A, 5) + ((B & C) | (B & D) | (C & D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
  1876. E = D;
  1877. D = C;
  1878. C = rotate_left(B, 30);
  1879. B = A;
  1880. A = temp;
  1881. }
  1882. for (i = 60; i <= 79; i++) {
  1883. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
  1884. E = D;
  1885. D = C;
  1886. C = rotate_left(B, 30);
  1887. B = A;
  1888. A = temp;
  1889. }
  1890. H0 = (H0 + A) & 0x0ffffffff;
  1891. H1 = (H1 + B) & 0x0ffffffff;
  1892. H2 = (H2 + C) & 0x0ffffffff;
  1893. H3 = (H3 + D) & 0x0ffffffff;
  1894. H4 = (H4 + E) & 0x0ffffffff;
  1895. }
  1896. var temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4);
  1897. return temp.toLowerCase();
  1898. };
  1899. (function (jQuery, window, undefined) {
  1900. "use strict";
  1901. var matched, browser;
  1902. jQuery.uaMatch = function (ua) {
  1903. ua = ua.toLowerCase();
  1904. var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
  1905. /(webkit)[ \/]([\w.]+)/.exec(ua) ||
  1906. /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
  1907. /(msie) ([\w.]+)/.exec(ua) ||
  1908. ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
  1909. [];
  1910. var platform_match = /(ipad)/.exec(ua) ||
  1911. /(iphone)/.exec(ua) ||
  1912. /(android)/.exec(ua) ||
  1913. [];
  1914. return {
  1915. browser: match[1] || "",
  1916. version: match[2] || "0",
  1917. platform: platform_match[0] || ""
  1918. };
  1919. };
  1920. matched = jQuery.uaMatch(window.navigator.userAgent);
  1921. browser = {};
  1922. if (matched.browser) {
  1923. browser[matched.browser] = true;
  1924. browser.version = matched.version;
  1925. }
  1926. if (matched.platform) {
  1927. browser[matched.platform] = true
  1928. }
  1929. // Chrome is Webkit, but Webkit is also Safari.
  1930. if (browser.chrome) {
  1931. browser.webkit = true;
  1932. } else if (browser.webkit) {
  1933. browser.safari = true;
  1934. }
  1935. jQuery.browser = browser;
  1936. })(jQuery, window);