import * as path from 'path' ; import { ForgeClient , IAuthOptions } from 'forge-server-utils/dist/common' ; const ApiHost = 'https://otg.autodesk.com' ; const RootPath = 'modeldata' ; const ReadTokenScopes = [ 'bucket:read' , 'data:read' ] ; const WriteTokenScopes = [ 'data:write' ] ; export interface IView { urn : string ; id : string ; role : string ; mime : string ; resolvedUrn : string ; } interface IPropertyDbAsset { tag : string ; type : string ; resolvedUrn : string ; } export class ManifestHelper { constructor ( protected manifest ) { console . assert ( manifest . version === 1 ) ; console . assert ( manifest . progress === 'complete' ) ; console . assert ( manifest . status === 'success' ) ; } get sharedRoot ( ) { return this . manifest . paths . shared_root ; } listViews ( ) { const versionRoot = this . manifest . paths . version_root ; return Object . entries ( this . manifest . views ) . map ( ( [ id , view ] ) => { return { id , role : view . role , mime : view . mime , urn : view . urn , resolvedUrn : path . normalize ( path . join ( versionRoot , view . urn ) ) } ; } ) ; } listSharedDatabaseAssets ( ) { const pdbManifest = this . manifest . pdb_manifest ; const sharedRoot = this . manifest . paths . shared_root ; return pdbManifest . assets . filter ( ( asset ) => asset . isShared ) . map ( ( asset ) => { return { tag : asset . tag , type : asset . type , uri : asset . uri , resolvedUrn : path . normalize ( path . join ( sharedRoot , pdbManifest . pdb_shared_rel_path , asset . uri ) ) } ; } ) ; } } export class ViewHelper { constructor ( protected view , protected resolvedViewUrn ) { } listPrivateModelAssets ( ) { const assets = this . view . manifest && this . view . manifest . assets ; if ( assets ) { let result = { } ; if ( assets . fragments ) { result . fragments = { uri : assets . fragments , resolvedUrn : this . resolveAssetUrn ( assets . fragments ) } ; } if ( assets . fragments_extra ) { result . fragments_extra = { uri : assets . fragments_extra , resolvedUrn : this . resolveAssetUrn ( assets . fragments_extra ) } ; } if ( assets . materials_ptrs ) { result . materials_ptrs = { uri : assets . materials_ptrs , resolvedUrn : this . resolveAssetUrn ( assets . materials_ptrs ) } ; } if ( assets . geometry_ptrs ) { result . geometry_ptrs = { uri : assets . geometry_ptrs , resolvedUrn : this . resolveAssetUrn ( assets . geometry_ptrs ) } ; } return result ; } else { return undefined ; } } listSharedModelAssets ( ) { const assets = this . view . manifest && this . view . manifest . shared_assets ; if ( assets ) { let result = { } ; if ( assets . geometry ) { result . geometry = { uri : assets . geometry , resolvedUrn : this . resolveAssetUrn ( assets . geometry ) } ; } if ( assets . materials ) { result . materials = { uri : assets . materials , resolvedUrn : this . resolveAssetUrn ( assets . materials ) } ; } if ( assets . textures ) { result . textures = { uri : assets . textures , resolvedUrn : this . resolveAssetUrn ( assets . textures ) } ; } return result ; } else { return undefined ; } } listPrivateDatabaseAssets ( ) { const pdb = this . view . manifest && this . view . manifest . assets && this . view . manifest . assets . pdb ; if ( pdb ) { return { avs : { uri : pdb . avs , resolvedUrn : this . resolveAssetUrn ( pdb . avs ) } , offsets : { uri : pdb . offsets , resolvedUrn : this . resolveAssetUrn ( pdb . offsets ) } , dbid : { uri : pdb . dbid , resolvedUrn : this . resolveAssetUrn ( pdb . dbid ) } } ; } else { return undefined ; } } listSharedDatabaseAssets ( ) { const pdb = this . view . manifest && this . view . manifest . shared_assets && this . view . manifest . shared_assets . pdb ; if ( pdb ) { return { attrs : { uri : pdb . attrs , resolvedUrn : this . resolveAssetUrn ( pdb . attrs ) } , values : { uri : pdb . values , resolvedUrn : this . resolveAssetUrn ( pdb . values ) } , ids : { uri : pdb . ids , resolvedUrn : this . resolveAssetUrn ( pdb . ids ) } } ; } else { return undefined ; } } resolveAssetUrn ( assetUrn ) { if ( assetUrn . startsWith ( 'urn:' ) ) { return assetUrn ; } else { return path . normalize ( path . join ( path . dirname ( this . resolvedViewUrn ) , assetUrn ) ) ; } } getGeometryUrn ( hash ) { return this . view . manifest . shared_assets . geometry + hash ; } getMaterialUrn ( hash ) { return this . view . manifest . shared_assets . materials + hash ; } getTextureUrn ( hash ) { return this . view . manifest . shared_assets . textures + hash ; } } /**
 * Client for the OTG service (https://otg.autodesk.com).
 */ export class Client extends ForgeClient { param { IAuthOptions } auth param { string } host constructor ( auth , host = ApiHost ) { super ( RootPath , auth , host ) ; this . axios . defaults . headers = this . axios . defaults . headers || { } ; this . axios . defaults . headers [ 'Pragma' ] = 'no-cache' ; } async param { string } urn param { string } account param { boolean } force throws async createDerivatives ( urn , account ? , force ? ) { const params = { urn } ; if ( account ) { params [ 'account_id' ] = account ; } if ( force ) { params [ 'force_conversion' ] = force ; } return this . post ( `` , params , { } , WriteTokenScopes ) ; } async param { string } urn throws async deleteDerivatives ( urn ) { return this . delete ( urn , { } , WriteTokenScopes ) ; } async param { string } urn returns { Promise < any > } throws async getManifest ( urn ) { return this . get ( `manifest/${ urn }` , { } , ReadTokenScopes ) ; } async param { string } urn param { string } assetUrn returns { Promise < Buffer > } throws async getAsset ( urn , assetUrn ) { return this . getBuffer ( `file/${ assetUrn }?acmsession=${ urn }` , { } , ReadTokenScopes ) ; } } export class SharedClient extends ForgeClient { protected sharding : number = 4 ; constructor ( auth , host = ApiHost ) { super ( 'cdn' , auth , host ) ; this . axios . defaults . headers = this . axios . defaults . headers || { } ; this . axios . defaults . headers [ 'Pragma' ] = 'no-cache' ; } async getAsset ( urn , assetUrn ) { const assetUrnTokens = assetUrn . split ( '/' ) ; const account = assetUrnTokens [ 1 ] ; const assetType = assetUrnTokens [ 2 ] ; const assetHash = assetUrnTokens [ 3 ] ; const cdnUrn = `${ assetHash . substr ( 0 , 4 ) }/${ account }/${ assetType }/${ assetHash . substr ( 4 ) }` ; return this . getBuffer ( cdnUrn + `?acmsession=${ urn }` , { } , ReadTokenScopes ) ; } }