UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 1.73 kB
import{result as r}from"../../core/asyncUtils.js";import e from"../../core/Error.js";import{isSome as o}from"../../core/maybe.js";import{eachAlways as t,throwIfAborted as s}from"../../core/promiseUtils.js";import{generateUUID as c}from"../../core/uuid.js";import{getSiblingOfSameTypeI as p}from"../../portal/support/resourceUtils.js";async function a(r,o,a){if(!o||!o.resources)return;const h=o.portalItem===r.portalItem?new Set(r.paths):new Set;r.paths.length=0,r.portalItem=o.portalItem;const i=new Set(o.resources.toKeep.map((r=>r.resource.path))),m=new Set,f=[];i.forEach((e=>{h.delete(e),r.paths.push(e)}));for(const e of o.resources.toUpdate)if(h.delete(e.resource.path),i.has(e.resource.path)||m.has(e.resource.path)){const{resource:o,content:t,finish:s,error:u}=e,h=p(o,c());r.paths.push(h.path),f.push(n({resource:h,content:t,compress:e.compress,finish:s,error:u},a))}else r.paths.push(e.resource.path),f.push(u(e,a)),m.add(e.resource.path);for(const e of o.resources.toAdd)f.push(n(e,a)),r.paths.push(e.resource.path);if(h.forEach((r=>{if(o.portalItem){const e=o.portalItem.resourceFromPath(r);f.push(e.portalItem.removeResource(e).catch((()=>{})))}})),0===f.length)return;const l=await t(f);s(a);const d=l.filter((r=>"error"in r)).map((r=>r.error));if(d.length>0)throw new e("save:resources","Failed to save one or more resources",{errors:d})}async function n(e,t){const s={...o(t)?t:{},compress:e.compress},c=await r(e.resource.portalItem.addResource(e.resource,e.content,s));if(!0!==c.ok)throw e.error?.(c.error),c.error;e.finish?.(e.resource)}async function u(e,o){const t=await r(e.resource.update(e.content,o));if(!0!==t.ok)throw e.error?.(t.error),t.error;e.finish?.(e.resource)}export{a as saveResources};