-
Notifications
You must be signed in to change notification settings - Fork 13
/
baas.io.min.js
1 lines (1 loc) · 22.5 KB
/
baas.io.min.js
1
!function(){var a=this,b=a.Baas||{};a.console=a.console||{},a.console.log=a.console.log||function(){},b.VERSION="0.9.2","undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(exports=module.exports=b),exports.Baas=b):a.Baas=b,b.IO=function(a){this.URI=a.URI||"https://api.baas.io",this.orgName=a.orgName,this.appName=a.appName,this.buildCurl=a.buildCurl||!1,this.logging=a.logging||!1,this._callTimeout=a.callTimeout||3e4,this._callTimeoutCallback=a.callTimeoutCallback||null,this.logoutCallback=a.logoutCallback||null},b.IO.prototype.request=function(a,c){var d=this,e=a.method||"GET",f=a.endpoint,g=a.body||{},h=a.contentType||"application/json",i=a.qs||{},j=a.mQuery||!1;if(j)var k=this.URI+"/"+f;else var k=this.URI+"/"+this.orgName+"/"+this.appName+"/"+f;var l=b.Utils.encodeParams(i);l&&(k+="?"+l),g="application/json"===h?JSON.stringify(g):g;var m=new XMLHttpRequest;m.open(e,k,!0),d.getToken()&&m.setRequestHeader("Authorization","Bearer "+d.getToken()),h&&m.setRequestHeader("Content-Type",h),m.onerror=function(){d._end=(new Date).getTime(),d.logging&&console.log("success (time: "+d.calcTimeDiff()+"): "+e+" "+k),d.logging&&console.log("Error: API call failed at the network level."),clearTimeout(n);var a=!0;"function"==typeof c&&c(a,data)},m.onload=function(a){if(d._end=(new Date).getTime(),d.logging&&console.log("success (time: "+d.calcTimeDiff()+"): "+e+" "+k),clearTimeout(n),f.indexOf("files/")>=0&&f.indexOf("/data")>=0)return c(!1),void 0;if(a=JSON.parse(m.responseText),200!=m.status){var b=a.error,g=a.error_description;if(d.logging&&console.log("Error ("+m.status+")("+b+"): "+g),("auth_expired_session_token"==b||"auth_missing_credentials"==b||"auth_unverified_oath"==b||"expired_token"==b||"unauthorized"==b||"auth_invalid"==b)&&"function"==typeof d.logoutCallback)return d.logoutCallback(!0,a);"function"==typeof c&&c(!0,a)}else"function"==typeof c&&c(!1,a)};var n=setTimeout(function(){m.abort(),"function"===d._callTimeoutCallback?d._callTimeoutCallback("API CALL TIMEOUT"):d.callback("API CALL TIMEOUT")},d._callTimeout);if(this.logging&&console.log("calling: "+e+" "+k),this.buildCurl){var o={uri:k,body:g,method:e};this.buildCurlCall(o)}this._start=(new Date).getTime(),m.send(g)},b.IO.prototype.createGroup=function(a,c){var d=a.getOnExist||!1,a={path:a.path,client:this,data:a},e=new b.Group(a);e.fetch(function(a,b){var f=a&&"Service resource not found"===b.error_description||"no_name_specified"===b.error||"null_pointer"===b.error_description||!a&&d;f?e.save(function(a){"function"==typeof c&&c(a,e)}):"function"==typeof c&&c(a,e)})},b.IO.prototype.createEntity=function(a,c){var d=a.getOnExist||!1,a={client:this,data:a},e=new b.Entity(a);e.fetch(function(b,f){var g=b&&"Service resource not found"===f.error_description||"no_name_specified"===f.error||"null_pointer"===f.error_description||!b&&d;g?(e.set(a.data),e.save(function(a){"function"==typeof c&&c(a,e)})):"function"==typeof c&&c(b,e)})},b.IO.prototype.restoreEntity=function(a){var c=JSON.parse(a),d={client:this,data:c},e=new b.Entity(d);return e},b.IO.prototype.getEntity=function(a,c){var a={client:this,data:a},d=new b.Entity(a);d.fetch(function(a){"function"==typeof c&&c(a,d)})},b.IO.prototype.createCollection=function(a,c){a.client=this;var d=new b.Collection(a,function(a){"function"==typeof c&&c(a,d)})},b.IO.prototype.restoreCollection=function(a){var c=JSON.parse(a);c.client=this;var d=new b.Collection(c);return d},b.IO.prototype.getFeedForUser=function(a,b){var c={method:"GET",endpoint:"users/"+a+"/feed"};this.request(c,function(a,c){"function"==typeof b&&(a?b(a):b(a,c,c.entities))})},b.IO.prototype.createUserActivity=function(a,c,d){c.type="users/"+a+"/activities";var c={client:this,data:c},e=new b.Entity(c);e.save(function(a){"function"==typeof d&&d(a,e)})},b.IO.prototype.createUserActivityWithEntity=function(a,b,c){var d=a.get("username"),e={actor:{displayName:d,uuid:a.get("uuid"),username:d,email:a.get("email"),picture:a.get("picture"),image:{duration:0,height:80,url:a.get("picture"),width:80}},verb:"post",content:b};this.createUserActivity(d,e,c)},b.IO.prototype.calcTimeDiff=function(){var a=0,b=this._end-this._start;try{a=(b/10/60).toFixed(2)}catch(c){return 0}return a},b.IO.prototype.setToken=function(a){var b="token"+this.appName+this.orgName;this.token=a,"undefined"!=typeof Storage&&(a?localStorage.setItem(b,a):localStorage.removeItem(b))},b.IO.prototype.getToken=function(){var a="token"+this.appName+this.orgName;return this.token?this.token:"undefined"!=typeof Storage?localStorage.getItem(a):null},b.IO.prototype.signup=function(a,b,c,d){var e={type:"users",username:a,password:b,email:c};this.createEntity(e,d)},b.IO.prototype.kakao_signup=function(a,b){var c={method:"POST",endpoint:"auth/kakaotalk",contentType:"application/x-www-form-urlencoded",token:this.getToken(),body:"kkt_access_token="+a.kkt_access_token};this.request(c,b)},b.IO.prototype.kakao_signin=function(){this.kakao_signup.apply(this,arguments)},b.IO.prototype.kakao_login=function(){this.kakao_signup.apply(this,arguments)},b.IO.prototype.login=function(a,c,d){var e=this,f={method:"POST",endpoint:"token",body:{username:a,password:c,grant_type:"password"}};this.request(f,function(a,c){var f={};if(a&&e.logging)console.log("error trying to log user in");else{var g={client:e,data:c.user};f=new b.Entity(g),e.setToken(c.access_token)}"function"==typeof d&&d(a,c,f)})},b.IO.prototype.loginFacebook=function(a,c){var d=this,e={method:"GET",endpoint:"auth/facebook",qs:{fb_access_token:a}};this.request(e,function(a,e){var f={};if(a&&d.logging)console.log("error trying to log user in");else{var g={client:d,data:e.user};f=new b.Entity(g),d.setToken(e.access_token)}"function"==typeof c&&c(a,e,f)})},b.IO.prototype.getLoggedInUser=function(a){if(this.getToken()){var c=this,d={method:"GET",endpoint:"users/me"};this.request(d,function(d,e){if(d)c.logging&&console.log("error trying to log user in"),"function"==typeof a&&a(d,e,null);else{var f={client:c,data:e.entities[0]},g=new b.Entity(f);"function"==typeof a&&a(d,e,g)}})}else a(!0,null,null)},b.IO.prototype.isLoggedIn=function(){return this.getToken()&&"null"!=this.getToken()?!0:!1},b.IO.prototype.logout=function(){this.setToken(null)},b.IO.prototype.buildCurlCall=function(a){var b="curl",c=(a.method||"GET").toUpperCase(),d=a.body||{},e=a.uri;return b+="POST"===c?" -X POST":"PUT"===c?" -X PUT":"DELETE"===c?" -X DELETE":" -X GET",b+=" "+e,'"{}"'!==d&&"GET"!==c&&"DELETE"!==c&&(b+=" -d '"+d+"'"),console.log(b),b},b.Entity=function(a){a&&(this._client=a.client,this._data=a.data||{})},b.Entity.prototype.serialize=function(){return JSON.stringify(this._data)},b.Entity.prototype.get=function(a){return a?this._data[a]:this._data},b.Entity.prototype.set=function(a,b){if("object"==typeof a)for(var c in a)this._data[c]=a[c];else"string"==typeof a?null===b?delete this._data[a]:this._data[a]=b:this._data=null},b.Entity.prototype.save=function(a){var c=this.get("type"),d="POST";b.Utils.isUUID(this.get("uuid"))?(d="PUT",c+="/"+this.get("uuid")):"users"===c&&this.get("username")?(d="PUT",c+="/"+this.get("username")):this.get("name")&&(d="PUT",c+="/"+this.get("name"));var e=this,f={},g=this.get();for(var h in g)"metadata"!==h&&"created"!==h&&"modified"!==h&&"type"!==h&&"activated"!==h&&"uuid"!==h&&(f[h]=g[h]);var i={method:d,endpoint:c,body:f};this._client.request(i,function(b,d){if(b&&e._client.logging){if(console.log("could not save entity"),"function"==typeof a)return a(b,d,e)}else{if(d.entities&&d.entities.length){var f=d.entities[0];e.set(f)}var h="user"===e.get("type")&&g.oldpassword&&g.newpassword;if(h){var i={};i.oldpassword=g.oldpassword,i.newpassword=g.newpassword;var j={method:"PUT",endpoint:c+"/password",body:i};e._client.request(j,function(b,c){b&&e._client.logging&&console.log("could not update user"),e.set("oldpassword",null),e.set("newpassword",null),"function"==typeof a&&a(b,c,e)})}else"function"==typeof a&&a(b,d,e)}})},b.Entity.prototype.fetch=function(a){var b=this.get("type"),c=this;if(this.get("uuid"))b+="/"+this.get("uuid");else if("users"===b){if(this.get("username"))b+="/"+this.get("username");else if("function"==typeof a){var d="no_name_specified";return c._client.logging&&console.log(d),a(!0,{error:d},c)}}else if("a path"===b){if(this.get("path"))b+="/"+encodeURIComponent(this.get("name"));else if("function"==typeof a){var d="no_name_specified";return c._client.logging&&console.log(d),a(!0,{error:d},c)}}else if(this.get("name"))b+="/"+encodeURIComponent(this.get("name"));else if("function"==typeof a){var d="no_name_specified";return c._client.logging&&console.log(d),a(!0,{error:d},c)}var e={method:"GET",endpoint:b};this._client.request(e,function(b,d){if(b&&c._client.logging)console.log("could not get entity");else if(d.user)c.set(d.user),c._json=JSON.stringify(d.user,null,2);else if(d.entities&&d.entities.length){var e=d.entities[0];c.set(e)}"function"==typeof a&&a(b,d,c)})},b.Entity.prototype.destroy=function(a){var c=this.get("type");if(b.Utils.isUUID(this.get("uuid")))c+="/"+this.get("uuid");else if("users"===c){if(this.get("username"))c+="/"+this.get("username");else if("function"==typeof a){var d="Error trying to delete object - no uuid specified.";e._client.logging&&console.log(d),a(!0,d)}}else if(this.get("name"))c+="/"+this.get("name");else if("function"==typeof a){var d="Error trying to delete object - no uuid specified.";e._client.logging&&console.log(d),a(!0,d)}var e=this,f={method:"DELETE",endpoint:c};this._client.request(f,function(b,c){b&&e._client.logging?console.log("entity could not be deleted"):e.set(null),"function"==typeof a&&a(b,c)})},b.Entity.prototype.connect=function(a,b,c){var d=this,e=b.get("type"),f=this.getEntityId(b);if(f){var g=this.get("type"),h=this.getEntityId(this);if(h){var i=g+"/"+h+"/"+a+"/"+e+"/"+f,j={method:"POST",endpoint:i};this._client.request(j,function(a,b){a&&d._client.logging&&console.log("entity could not be connected"),"function"==typeof c&&c(a,b)})}else if("function"==typeof c){var k="Error in connect - no uuid specified.";d._client.logging&&console.log(k),c(!0,k)}}else if("function"==typeof c){var k="Error trying to delete object - no uuid specified.";d._client.logging&&console.log(k),c(!0,k)}},b.Entity.prototype.getEntityId=function(a){var c=!1;return b.Utils.isUUID(a.get("uuid"))?c=a.get("uuid"):"users"===type?c=a.get("username"):a.get("name")&&(c=a.get("name")),c},b.Entity.prototype.getConnections=function(a,b){var c=this,d=this.get("type"),e=this.getEntityId(this);if(e){var f=d+"/"+e+"/"+a+"/",g={method:"GET",endpoint:f};this._client.request(g,function(d,e){d&&c._client.logging&&console.log("entity could not be connected"),c[a]={};for(var f=e.entities.length,g=0;f>g;g++)"user"===e.entities[g].type?c[a][e.entities[g].username]=e.entities[g]:c[a][e.entities[g].name]=e.entities[g];"function"==typeof b&&b(d,e,e.entities)})}else if("function"==typeof b){var h="Error in getConnections - no uuid specified.";c._client.logging&&console.log(h),b(!0,h)}},b.Entity.prototype.disconnect=function(a,b,c){var d=this,e=b.get("type"),f=this.getEntityId(b);if(f){var g=this.get("type"),h=this.getEntityId(this);if(h){var i=g+"/"+h+"/"+a+"/"+e+"/"+f,j={method:"DELETE",endpoint:i};this._client.request(j,function(a,b){a&&d._client.logging&&console.log("entity could not be disconnected"),"function"==typeof c&&c(a,b)})}else if("function"==typeof c){var k="Error in connect - no uuid specified.";d._client.logging&&console.log(k),c(!0,k)}}else if("function"==typeof c){var k="Error trying to delete object - no uuid specified.";d._client.logging&&console.log(k),c(!0,k)}},b.Collection=function(a,b){if(a&&(this._client=a.client,this._type=a.type,this.qs=a.qs||{},this._list=a.list||[],this._iterator=a.iterator||-1,this._previous=a.previous||[],this._next=a.next||null,this._cursor=a.cursor||null,a.list))for(var c=a.list.length,d=0;c>d;d++){var e=this._client.restoreEntity(a.list[d]);this._list[d]=e}b&&this.fetch(b)},b.Collection.prototype.serialize=function(){var a={};a.type=this._type,a.qs=this.qs,a.iterator=this._iterator,a.previous=this._previous,a.next=this._next,a.cursor=this._cursor,this.resetEntityPointer();var b=0;for(a.list=[];this.hasNextEntity();){var c=this.getNextEntity();a.list[b]=c.serialize(),b++}return a=JSON.stringify(a)},b.Collection.prototype.fetch=function(a){var c=this,d=this.qs;this._cursor?d.cursor=this._cursor:delete d.cursor;var e={method:"GET",endpoint:this._type,qs:this.qs};this._client.request(e,function(d,e){if(d&&c._client.logging)console.log("error getting collection");else{var f=e.cursor||null;if(c.saveCursor(f),e.entities){c.resetEntityPointer();var g=e.entities.length;c._list=[];for(var h=0;g>h;h++){var i=e.entities[h].uuid;if(i){var j=e.entities[h]||{},k={type:c._type,client:c._client,uuid:i,data:j},l=new b.Entity(k),m=c._list.length;c._list[m]=l}}}}"function"==typeof a&&a(d,e)})},b.Collection.prototype.addEntity=function(a,b){var c=this;a.type=this._type,this._client.createEntity(a,function(a,d){if(!a){var e=c._list.length;c._list[e]=d}"function"==typeof b&&b(a,d)})},b.Collection.prototype.destroyEntity=function(a,b){var c=this;a.destroy(function(a,d){a?(c._client.logging&&console.log("could not destroy entity"),"function"==typeof b&&b(a,d)):c.fetch(b)})},b.Collection.prototype.getEntityByUUID=function(a,c){var d={data:{type:this._type,uuid:a},client:this._client},e=new b.Entity(d);e.fetch(c)},b.Collection.prototype.getFirstEntity=function(){var a=this._list.length;return a>0?this._list[0]:null},b.Collection.prototype.getLastEntity=function(){var a=this._list.length;return a>0?this._list[a-1]:null},b.Collection.prototype.hasNextEntity=function(){var a=this._iterator+1,b=a>=0&&a<this._list.length;return b?!0:!1},b.Collection.prototype.getNextEntity=function(){this._iterator++;var a=this._iterator>=0&&this._iterator<this._list.length;return a?this._list[this._iterator]:!1},b.Collection.prototype.hasPrevEntity=function(){var a=this._iterator-1,b=a>=0&&a<this._list.length;return b?!0:!1},b.Collection.prototype.getPrevEntity=function(){this._iterator--;var a=this._iterator>=0&&this._iterator<=this._list.length;return a?this._list[this._iterator]:!1},b.Collection.prototype.resetEntityPointer=function(){this._iterator=-1},b.Collection.prototype.saveCursor=function(a){this._next!==a&&(this._next=a)},b.Collection.prototype.resetPaging=function(){this._previous=[],this._next=null,this._cursor=null},b.Collection.prototype.hasNextPage=function(){return this._next},b.Collection.prototype.getNextPage=function(a){this.hasNextPage()?(this._previous.push(this._cursor),this._cursor=this._next,this._list=[],this.fetch(a)):a(!0)},b.Collection.prototype.hasPreviousPage=function(){return this._previous.length>0},b.Collection.prototype.getPreviousPage=function(a){this.hasPreviousPage()?(this._next=null,this._cursor=this._previous.pop(),this._list=[],this.fetch(a)):a(!0)},b.Group=function(a){this._path=a.path,this._list=[],this._client=a.client,this._data=a.data||{},this._data.type="groups"},b.Group.prototype=new b.Entity,b.Group.prototype.fetch=function(a){var c=this,d="groups/"+this._path,e="groups/"+this._path+"/users",f={method:"GET",endpoint:d},g={method:"GET",endpoint:e};this._client.request(f,function(d,e){if(d)c._client.logging&&console.log("error getting group"),"function"==typeof a&&a(d,e);else if(e.entities){var f=e.entities[0];c._data=f||{},c._client.request(g,function(d,e){if(d&&c._client.logging)console.log("error getting group users");else if(e.entities){var f=e.entities.length;c._list=[];for(var g=0;f>g;g++){var h=e.entities[g].uuid;if(h){var i=e.entities[g]||{},j={type:i.type,client:c._client,uuid:h,data:i},k=new b.Entity(j);c._list.push(k)}}}"function"==typeof a&&a(d,e,c._list)})}})},b.Group.prototype.members=function(a){"function"==typeof a&&a(null,this._list)},b.Group.prototype.add=function(a,b){var c=this,a={method:"POST",endpoint:"groups/"+this._path+"/users/"+a.user.get("username")};this._client.request(a,function(a,d){a?"function"==typeof b&&b(a,d,d.entities):c.fetch(b)})},b.Group.prototype.remove=function(a,b){var c=this,a={method:"DELETE",endpoint:"groups/"+this._path+"/users/"+a.user.get("username")};this._client.request(a,function(a,d){a?"function"==typeof b&&b(a,d):c.fetch(b)})},b.Group.prototype.feed=function(a){var b=this,c="groups/"+this._path+"/feed",d={method:"GET",endpoint:c};this._client.request(d,function(c,d){c&&b.logging&&console.log("error trying to log user in"),"function"==typeof a&&a(c,d,d.entities)})},b.Group.prototype.createGroupActivity=function(a,c){var d=a.user,a={actor:{displayName:d.get("username"),uuid:d.get("uuid"),username:d.get("username"),email:d.get("email"),picture:d.get("picture"),image:{duration:0,height:80,url:d.get("picture"),width:80}},verb:"post",content:a.content};a.type="groups/"+this._path+"/activities";var a={client:this._client,data:a},e=new b.Entity(a);e.save(function(a){"function"==typeof c&&c(a,e)})},b.Push=function(a){a&&(this._client=a.client,this._data=a.data||{},this._data.type="pushes")},b.Push.prototype=new b.Entity,b.Push.prototype.send=function(a,c){var d=this.get("type"),e="POST",f=this,g={},h=this.get();if("object"!=typeof a||!a)return c(!0,{error_code:100,error_description:"not type of options"}),void 0;var i=a.platform,j=a.reserve,k=a.target,l=a.to,m=a.payload;if("string"!=typeof k)return c(!0,{error_code:100,error_description:"Bad request"}),void 0;if("all"!==k&&"user"!==k&&"device"!==k&&"tag"!==k)return c(!0,{error_code:100,error_description:"Invalid target parameter"}),void 0;if("string"==typeof l){if("user"===k&&!b.Utils.isUUID(l))return c(!0,{error_code:100,error_description:"Invalid UUID string:"+l}),void 0;if("device"===k&&""===l)return c(!0,{error_code:620,error_description:"Not existed devices"}),void 0;if("tag"===k&&""===l)return c(!0,{error_code:620,error_description:"Push to is null"}),void 0}else if("all"!==k)return c(!0,{error_code:100,error_description:"Bad request"}),void 0;if(!m)return c(!0,{error_code:100,error_description:"Bad request"}),void 0;if("string"==typeof m||"number"==typeof m||"object"==typeof m&&m instanceof Array)return c(!0,{error_code:100,error_description:"Invalid payload.badge type"}),void 0;if(!m.alert||"string"!=typeof m.alert)return c(!0,{error_code:100,error_description:"Bad request"}),void 0;if(m.badge){if("number"!=typeof m.badge)return c(!0,{error_code:100,error_description:"Invalid payload.badge type"}),void 0}else if(void 0!==m.badge)return c(!0,{error_code:100,error_description:"Invalid payload.badge parameter"}),void 0;if(m.sound){if("string"!=typeof m.sound)return c(!0,{error_code:100,error_description:"Invalid payload.sound type"}),void 0}else if(void 0!==m.sound)return c(!0,{error_code:100,error_description:"Invalid payload.sound parameter"}),void 0;if(i){if("string"!=typeof i&&c(!0,{error_code:100,error_description:"Invalid platform type"}),"I"!==i&&"G"!==i)return c(!0,{error_code:620,error_description:"Device platform invalid regxp"}),void 0}else if(void 0!==i)return c(!0,{error_code:100,error_description:"Invalid platform parameter"}),void 0;if(j){if("string"!=typeof j)return c(!0,{error_code:100,error_description:"Invalid reserve type"}),void 0;(12!==j.length||isNaN(j))&&c(!0,{error_code:620,error_description:"Push reserve invalid regxp"})}else if(void 0!==j)return c(!0,{error_code:100,error_description:"Invalid reserve parameter"}),void 0;for(var n in h)"metadata"!==n&&"created"!==n&&"modified"!==n&&"type"!==n&&"activated"!==n&&"uuid"!==n&&(g[n]=h[n]);for(var n in a)("to"!==n||"all"!==k)&&(g[n]=a[n]);var a={method:e,endpoint:d,body:g};this._client.request(a,function(a,b){if(a)f._client.logging&&console.log("error getting push");else if(b.entities&&b.entities.length){var d=b.entities[0];f.set(d)}"function"==typeof c&&c(a,b,f)})},b.File=function(a){a&&(this._client=a.client,this._data=a.data||{},this._data.type="files",this._data.minsize=a.minsize||1,this._data.maxsize=a.maxsize||10485760,this._data.allowExts=a.allowExts||"",this.count=0)},b.File.prototype=new b.Entity,b.File.prototype.upload=function(a,b){var c=this.get("type"),d=/<|>|"|\\/,e=this,f=new FormData,g=a.file[0].files[e.count].size,h=a.file[0].files[e.count].name;if(g<this._data.minsize||g>this._data.maxsize)return b(!0,{error_code:100,error_description:"min : 1byte, max : 10MB"}),void 0;if(d.test(h))return b(!0,{error_code:100,error_description:"Invalid file name"}),void 0;if(this.get("allowExts")){var i=this.get("allowExts").toLowerCase().split(",");if(h.length>0)var j=h.lastIndexOf("."),k=h.substring(j+1).toLowerCase();var l=0;if(k&&k.length>0)for(var m=0;m<i.length;m++){var n=i[m].toLowerCase();k==n&&(l+=1)}if(0===l)return b(!0,{error_code:100,error_description:"Invalid file ext"}),void 0}f.append("file",a.file[0].files[e.count]);var o={method:"POST",endpoint:c,body:f};this._client.request(o,function(a,c){if(a)e._client.logging&&console.log("error getting push"),"function"==typeof b&&b(a,c);else{if(c.entities&&c.entities.length){var d=c.entities[0];e.set(d)}b(a,c,e)}})},b.File.prototype.download=function(a){var b="files/"+this.get("uuid")+"/data",c=this,d={method:"GET",endpoint:b};this._client.request(d,function(b){b?(c._client.logging&&console.log("error getting push"),"function"==typeof a&&a(b,c)):(a(b,c),window.location=c.getDownloadURL())})},b.File.prototype.save=function(a){var c=this.get("type"),d="POST";b.Utils.isUUID(this.get("uuid"))?(d="PUT",c+="/"+this.get("uuid")):(d="PUT",c+="/"+this.get("username"));var e=this,f={},g=this.get();for(var h in g)"metadata"!==h&&"created"!==h&&"modified"!==h&&"type"!==h&&"activated"!==h&&"uuid"!==h&&"minsize"!==h&&"maxsize"!==h&&(f[h]=g[h]);var i={method:d,endpoint:c,body:f};this._client.request(i,function(b,c){if(b&&e._client.logging){if(console.log("could not save entity"),"function"==typeof a)return a(b,c,e)}else{if(c.entities&&c.entities.length){var d=c.entities[0];e.set(d)}a(b,c,e)}})},b.File.prototype.fetch=function(a){var b=this.get("type"),c=this;if(this.get("uuid"))b+="/"+this.get("uuid");else if("function"==typeof a){var d="no_uuid_specified";return c._client.logging&&console.log(d),a(!0,{error:d},c)}var e={method:"GET",endpoint:b};this._client.request(e,function(b,d){if(b&&c._client.logging)console.log("could not get entity");else if(d.entities.length){var e=d.entities[0];c.set(e)}"function"==typeof a&&a(b,d,c)})},b.File.prototype.getDownloadURL=function(){return this._client.URI+"/"+this._client.orgName+"/"+this._client.appName+"/"+this.get("type")+"/"+this.get("uuid")+"/data"},b.Utils=function(){function a(a){var b=/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;return a?b.test(a):!1}function b(a){tail=[];var b=[];if(a instanceof Array)for(i in a)b=a[i],b instanceof Array&&b.length>1&&tail.push(b[0]+"="+encodeURIComponent(b[1]));else for(var c in a)if(a.hasOwnProperty(c)){var d=a[c];if(d instanceof Array)for(i in d)b=d[i],tail.push(c+"="+encodeURIComponent(b));else tail.push(c+"="+encodeURIComponent(d))}return tail.join("&")}return{isUUID:a,encodeParams:b}}()}();