jellyfin-web/dashboard-ui/bower_components/emby-apiclient/apiclient.js

2 lines
50 KiB
JavaScript
Raw Normal View History

define(["events"],function(events){"use strict";return function(serverAddress,clientName,applicationVersion,deviceName,deviceId,devicePixelRatio){function redetectBitrate(){stopBitrateDetection(),self.accessToken()&&self.enableAutomaticBitrateDetection!==!1&&setTimeout(redetectBitrateInternal,6e3)}function redetectBitrateInternal(){self.detectBitrate()}function stopBitrateDetection(){detectTimeout&&clearTimeout(detectTimeout)}function onFetchFail(url,response){events.trigger(self,"requestfail",[{url:url,status:response.status,errorCode:response.headers?response.headers.get("X-Application-Error-Code"):null}])}function getFetchPromise(request){var headers=request.headers||{};"json"===request.dataType&&(headers.accept="application/json");var fetchRequest={headers:headers,method:request.type,credentials:"same-origin"},contentType=request.contentType;return request.data&&("string"==typeof request.data?fetchRequest.body=request.data:(fetchRequest.body=paramsToString(request.data),contentType=contentType||"application/x-www-form-urlencoded; charset=UTF-8")),contentType&&(headers["Content-Type"]=contentType),request.timeout?fetchWithTimeout(request.url,fetchRequest,request.timeout):fetch(request.url,fetchRequest)}function fetchWithTimeout(url,options,timeoutMs){return new Promise(function(resolve,reject){var timeout=setTimeout(reject,timeoutMs);options=options||{},options.credentials="same-origin",fetch(url,options).then(function(response){clearTimeout(timeout),resolve(response)},function(error){clearTimeout(timeout),reject(error)})})}function paramsToString(params){var values=[];for(var key in params){var value=params[key];null!==value&&void 0!==value&&""!==value&&values.push(encodeURIComponent(key)+"="+encodeURIComponent(value))}return values.join("&")}function switchConnectionMode(connectionMode){var currentServerInfo=self.serverInfo(),newConnectionMode=connectionMode;return newConnectionMode--,newConnectionMode<0&&(newConnectionMode=MediaBrowser.ConnectionMode.Manual),MediaBrowser.ServerInfo.getServerAddress(currentServerInfo,newConnectionMode)?newConnectionMode:(newConnectionMode--,newConnectionMode<0&&(newConnectionMode=MediaBrowser.ConnectionMode.Manual),MediaBrowser.ServerInfo.getServerAddress(currentServerInfo,newConnectionMode)?newConnectionMode:connectionMode)}function tryReconnectInternal(resolve,reject,connectionMode,currentRetryCount){connectionMode=switchConnectionMode(connectionMode);var url=MediaBrowser.ServerInfo.getServerAddress(self.serverInfo(),connectionMode);console.log("Attempting reconnection to "+url);var timeout=connectionMode===MediaBrowser.ConnectionMode.Local?7e3:15e3;fetchWithTimeout(url+"/system/info/public",{method:"GET",accept:"application/json"},timeout).then(function(){console.log("Reconnect succeeded to "+url),self.serverInfo().LastConnectionMode=connectionMode,self.serverAddress(url),resolve()},function(){if(console.log("Reconnect attempt failed to "+url),currentRetryCount<5){var newConnectionMode=switchConnectionMode(connectionMode);setTimeout(function(){tryReconnectInternal(resolve,reject,newConnectionMode,currentRetryCount+1)},300)}else reject()})}function tryReconnect(){return new Promise(function(resolve,reject){setTimeout(function(){tryReconnectInternal(resolve,reject,self.serverInfo().LastConnectionMode,0)},300)})}function replaceAll(originalString,strReplace,strWith){var reg=new RegExp(strReplace,"ig");return originalString.replace(reg,strWith)}function onWebSocketMessage(msg){if("UserDeleted"===msg.MessageType)currentUser=null;else if("UserUpdated"===msg.MessageType||"UserConfigurationUpdated"===msg.MessageType){var user=msg.Data;user.Id===self.getCurrentUserId()&&(currentUser=null)}events.trigger(self,"websocketmessage",[msg])}function normalizeReturnBitrate(bitrate){if(!bitrate)return lastDetectedBitrate?lastDetectedBitrate:Promise.reject();var result=Math.round(.8*bitrate);return lastDetectedBitrate=result,lastDetectedBitrateTime=(new Date).getTime(),result}function detectBitrateInternal(tests,index,currentBitrate){if(index>=tests.length)return normalizeReturnBitrate(currentBitr
contentType:"image/"+file.name.substring(file.name.lastIndexOf(".")+1)}).then(function(result){resolve(result)},function(){reject()})},reader.readAsDataURL(file)})},self.uploadItemImage=function(itemId,imageType,file){if(!itemId)throw new Error("null itemId");if(!imageType)throw new Error("null imageType");if(!file)throw new Error("File must be an image.");if("image/png"!==file.type&&"image/jpeg"!==file.type&&"image/jpeg"!==file.type)throw new Error("File must be an image.");var url=self.getUrl("Items/"+itemId+"/Images");return url+="/"+imageType,new Promise(function(resolve,reject){var reader=new FileReader;reader.onerror=function(){reject()},reader.onabort=function(){reject()},reader.onload=function(e){var data=e.target.result.split(",")[1];self.ajax({type:"POST",url:url,data:data,contentType:"image/"+file.name.substring(file.name.lastIndexOf(".")+1)}).then(function(result){resolve(result)},function(){reject()})},reader.readAsDataURL(file)})},self.getInstalledPlugins=function(){var options={},url=self.getUrl("Plugins",options);return self.getJSON(url)},self.getUser=function(id){if(!id)throw new Error("Must supply a userId");var url=self.getUrl("Users/"+id);return self.getJSON(url)},self.getOfflineUser=function(id){if(!id)throw new Error("Must supply a userId");var url=self.getUrl("Users/"+id+"/Offline");return self.getJSON(url)},self.getStudio=function(name,userId){if(!name)throw new Error("null name");var options={};userId&&(options.userId=userId);var url=self.getUrl("Studios/"+self.encodeName(name),options);return self.getJSON(url)},self.getGenre=function(name,userId){if(!name)throw new Error("null name");var options={};userId&&(options.userId=userId);var url=self.getUrl("Genres/"+self.encodeName(name),options);return self.getJSON(url)},self.getMusicGenre=function(name,userId){if(!name)throw new Error("null name");var options={};userId&&(options.userId=userId);var url=self.getUrl("MusicGenres/"+self.encodeName(name),options);return self.getJSON(url)},self.getGameGenre=function(name,userId){if(!name)throw new Error("null name");var options={};userId&&(options.userId=userId);var url=self.getUrl("GameGenres/"+self.encodeName(name),options);return self.getJSON(url)},self.getArtist=function(name,userId){if(!name)throw new Error("null name");var options={};userId&&(options.userId=userId);var url=self.getUrl("Artists/"+self.encodeName(name),options);return self.getJSON(url)},self.getPerson=function(name,userId){if(!name)throw new Error("null name");var options={};userId&&(options.userId=userId);var url=self.getUrl("Persons/"+self.encodeName(name),options);return self.getJSON(url)},self.getPublicUsers=function(){var url=self.getUrl("users/public");return self.ajax({type:"GET",url:url,dataType:"json"},!1)},self.getUsers=function(options){var url=self.getUrl("users",options||{});return self.getJSON(url)},self.getParentalRatings=function(){var url=self.getUrl("Localization/ParentalRatings");return self.getJSON(url)},self.getDefaultImageQuality=function(imageType){return"backdrop"===imageType.toLowerCase()?80:90},self.getUserImageUrl=function(userId,options){if(!userId)throw new Error("null userId");options=options||{};var url="Users/"+userId+"/Images/"+options.type;return null!=options.index&&(url+="/"+options.index),normalizeImageOptions(options),delete options.type,delete options.index,self.getUrl(url,options)},self.getImageUrl=function(itemId,options){if(!itemId)throw new Error("itemId cannot be empty");options=options||{};var url="Items/"+itemId+"/Images/"+options.type;return null!=options.index&&(url+="/"+options.index),options.quality=options.quality||self.getDefaultImageQuality(options.type),self.normalizeImageOptions&&self.normalizeImageOptions(options),delete options.type,delete options.index,self.getUrl(url,options)},self.getScaledImageUrl=function(itemId,options){if(!itemId)throw new Error("itemId cannot be empty");options=options||{};var url="Items/"+itemId+"/Images/"+options.type;return null!=options.index&&(url+="/"+options.index),normalizeImageOptions(options),delete options.type,delete options.index,delete opti