import { IRequestOptions } from "@esri/arcgis-rest-request" ; import { IUserRequestOptions } from "@esri/arcgis-rest-auth" ; import { addItemResource , IItemResourceResponse } from "@esri/arcgis-rest-portal" ; import { getPortalApiUrl } from "@esri/hub-common" ; export param { string } sourceItemId param { string } targetItemId param { string } owner param { [ string ] } assets param { IRequestOptions } requestOptions returns { Promise < boolean > } export function copyImageResources ( sourceItemId , targetItemId , owner , assets , requestOptions ) { const itemResourceUrl = `${ getPortalApiUrl ( requestOptions ) }/content/items/${ sourceItemId }/resources` ; return requestOptions . authentication . getToken ( itemResourceUrl ) . then ( token => { const assetPromises = assets . map ( assetName => { const sourceUrl = `${ itemResourceUrl }/${ assetName }?token=${ token }` ; return addImageAsResource ( targetItemId , owner , assetName , sourceUrl , requestOptions as IUserRequestOptions ) ; } ) ; return Promise . all ( assetPromises ) ; } ) . then ( ( ) => { return true ; } ) ; } export param { string } targetItemId param { string } owner param { any [ ] } assets param { IRequestOptions } requestOptions returns { Promise < boolean > } export function copyEmbeddedImageResources ( targetItemId , owner , assets , requestOptions ) { const promises = assets . map ( ( asset ) => { return addImageAsResource ( targetItemId , owner , asset . name , asset . url , requestOptions as IUserRequestOptions ) . then ( ( ) => { return true ; } ) . catch ( ( ) => { return true ; } ) ; } ) ; return Promise . all ( promises ) . then ( ( ) => { return true ; } ) ; } export param { string } itemId param { string } owner param { string } filename param { string } url param { IRequestOptions } requestOptions returns { Promise < boolean > } export function addImageAsResource ( id , owner , name , url , requestOptions ) { const fetchOptions = { method : "GET" , credentials : "same-origin" } ; return ( fetch ( url , fetchOptions ) . then ( x => { return x . blob ( ) ; } ) . then ( blob => { return addItemResource ( { id , owner , name , resource : blob , authentication : requestOptions . authentication } ) . then ( ( response ) => { return response . success ; } ) ; } ) ) ; }