const Service=require("../service"),getUniIDInstance=require("../../lib/uni-id"),{verifyStoragePermission:verifyStoragePermission}=require("../../lib/storage");module.exports=class extends Service{async getAuth(){const{clientInfo:e,token:t}=this.ctx.data;e||this.ctx.throw("Client info is required");const i=getUniIDInstance(e),o=await i.checkToken(t,{needPermission:!0});if(o.code)return{uid:void 0,role:[],permission:[]};const r={uid:o.uid,role:o.role,permission:o.permission};return o.token&&o.tokenExpired&&(r.token=o.token,r.tokenExpired=o.tokenExpired),r}async verifyPermission(e,t,i){const o=global.uniCloudLocalConfig,r=o&&o.storage&&o.storage.permission||{};i.auth||(i.auth=await this.getAuth()),i.resource||(i.resource=await this.getResource(e));if(!0===verifyStoragePermission({permission:r,cloudPath:e,operation:t},i))return!0;this.ctx.throw("storage_permission_denied: "+e)}async getResource(e){const t=await global.uniCloud.getFileInfo(e);if(!t)return{uid:null,cloudPath:e,createDate:0,updateDate:0};const{uid:i,createDate:o,updateDate:r}=t;return{uid:i,cloudPath:e,createDate:o,updateDate:r}}async batchVerifyPermission(e){const{fileList:t,operation:i}=e||{};t&&t.length||this.ctx.throw("param_required: fileList");const o=await this.getAuth(),r=global.uniCloud.$scope.file,a=await this.batchGetResource(t.map((e=>r.getCloudPath(e))));for(let e=0;e<a.length;e++){const t=a[e];!0!==await this.verifyPermission(t.cloudPath,i,{resource:t,auth:o})&&this.ctx.throw("storage_permission_denied: "+t.cloudPath)}return!0}async batchGetResource(e){return(await global.uniCloud.batchGetFileInfo(e)).map((e=>{const{uid:t,cloudPath:i,createDate:o,updateDate:r}=e||{};return{uid:t,cloudPath:i,createDate:o,updateDate:r}}))}};