|
| 1 | +import { CesiumIonAuth } from './auth/CesiumIonAuth.js'; |
| 2 | +import { GoogleCloudAuthPlugin } from './GoogleCloudAuthPlugin.js'; |
| 3 | + |
| 4 | +export class CesiumIonAuthPlugin { |
| 5 | + |
| 6 | + get apiToken() { |
| 7 | + |
| 8 | + return this.auth.apiToken; |
| 9 | + |
| 10 | + } |
| 11 | + |
| 12 | + set apiToken( v ) { |
| 13 | + |
| 14 | + this.auth.apiToken = v; |
| 15 | + |
| 16 | + } |
| 17 | + |
| 18 | + get autoRefreshToken() { |
| 19 | + |
| 20 | + return this.auth.autoRefreshToken; |
| 21 | + |
| 22 | + } |
| 23 | + |
| 24 | + set autoRefreshToken( v ) { |
| 25 | + |
| 26 | + this.auth.autoRefreshToken = v; |
| 27 | + |
| 28 | + } |
| 29 | + |
| 30 | + constructor( options = {} ) { |
| 31 | + |
| 32 | + const { |
| 33 | + apiToken, |
| 34 | + assetId = null, |
| 35 | + autoRefreshToken = false, |
| 36 | + useRecommendedSettings = true, |
| 37 | + assetTypeHandler = ( type, tiles, asset ) => { |
| 38 | + |
| 39 | + console.warn( `CesiumIonAuthPlugin: Cesium Ion asset type "${ type }" unhandled.` ); |
| 40 | + |
| 41 | + }, |
| 42 | + } = options; |
| 43 | + |
| 44 | + this.name = 'CESIUM_ION_AUTH_PLUGIN'; |
| 45 | + this.auth = new CesiumIonAuth( { apiToken, autoRefreshToken } ); |
| 46 | + |
| 47 | + this.assetId = assetId; |
| 48 | + this.autoRefreshToken = autoRefreshToken; |
| 49 | + this.useRecommendedSettings = useRecommendedSettings; |
| 50 | + this.assetTypeHandler = assetTypeHandler; |
| 51 | + this.tiles = null; |
| 52 | + |
| 53 | + this._tileSetVersion = - 1; |
| 54 | + this._attributions = []; |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + init( tiles ) { |
| 59 | + |
| 60 | + if ( this.assetId !== null ) { |
| 61 | + |
| 62 | + tiles.rootURL = `https://api.cesium.com/v1/assets/${ this.assetId }/endpoint`; |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + this.tiles = tiles; |
| 67 | + this.auth.authURL = tiles.rootURL; |
| 68 | + |
| 69 | + // reset the tiles in case this plugin was removed and re-added |
| 70 | + tiles.resetFailedTiles(); |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + loadRootTileSet() { |
| 75 | + |
| 76 | + // ensure we have an up-to-date token and root url, then trigger the internal |
| 77 | + // root tile set load function |
| 78 | + return this |
| 79 | + .auth |
| 80 | + .refreshToken() |
| 81 | + .then( json => { |
| 82 | + |
| 83 | + this._initializeFromAsset( json ); |
| 84 | + return this.tiles.invokeOnePlugin( plugin => plugin !== this && plugin.loadRootTileSet && plugin.loadRootTileSet() ); |
| 85 | + |
| 86 | + } ) |
| 87 | + .catch( error => { |
| 88 | + |
| 89 | + this.tiles.dispatchEvent( { |
| 90 | + type: 'load-error', |
| 91 | + tile: null, |
| 92 | + error, |
| 93 | + url: this.auth.authURL, |
| 94 | + } ); |
| 95 | + |
| 96 | + } ); |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + preprocessURL( uri ) { |
| 101 | + |
| 102 | + uri = new URL( uri ); |
| 103 | + if ( /^http/.test( uri.protocol ) && this._tileSetVersion != - 1 ) { |
| 104 | + |
| 105 | + uri.searchParams.set( 'v', this._tileSetVersion ); |
| 106 | + |
| 107 | + } |
| 108 | + return uri.toString(); |
| 109 | + |
| 110 | + } |
| 111 | + |
| 112 | + fetchData( uri, options ) { |
| 113 | + |
| 114 | + const tiles = this.tiles; |
| 115 | + if ( tiles.getPluginByName( 'GOOGLE_CLOUD_AUTH_PLUGIN' ) !== null ) { |
| 116 | + |
| 117 | + return null; |
| 118 | + |
| 119 | + } else { |
| 120 | + |
| 121 | + return this.auth.fetch( uri, options ); |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | + } |
| 126 | + |
| 127 | + getAttributions( target ) { |
| 128 | + |
| 129 | + if ( this.tiles.visibleTiles.size > 0 ) { |
| 130 | + |
| 131 | + target.push( ...this._attributions ); |
| 132 | + |
| 133 | + } |
| 134 | + |
| 135 | + } |
| 136 | + |
| 137 | + _initializeFromAsset( json ) { |
| 138 | + |
| 139 | + const tiles = this.tiles; |
| 140 | + if ( 'externalType' in json ) { |
| 141 | + |
| 142 | + const url = new URL( json.options.url ); |
| 143 | + tiles.rootURL = json.options.url; |
| 144 | + |
| 145 | + // if the tile set is "external" then assume it's a google API tile set |
| 146 | + tiles.registerPlugin( new GoogleCloudAuthPlugin( { |
| 147 | + apiToken: url.searchParams.get( 'key' ), |
| 148 | + autoRefreshToken: this.autoRefreshToken, |
| 149 | + useRecommendedSettings: this.useRecommendedSettings, |
| 150 | + } ) ); |
| 151 | + |
| 152 | + } else { |
| 153 | + |
| 154 | + // fire callback for unhandled asset types |
| 155 | + if ( json.type !== '3DTILES' ) { |
| 156 | + |
| 157 | + // Other types include: |
| 158 | + // - GLTF |
| 159 | + // - CZML |
| 160 | + // - KML |
| 161 | + // - GEOJSON |
| 162 | + // - TERRAIN (QuantizedMesh) |
| 163 | + // - IMAGERY (TSM Tiles) |
| 164 | + |
| 165 | + this.assetTypeHandler( json.type, tiles, json ); |
| 166 | + |
| 167 | + } |
| 168 | + |
| 169 | + tiles.rootURL = json.url; |
| 170 | + |
| 171 | + // save the version key if present |
| 172 | + const url = new URL( json.url ); |
| 173 | + if ( url.searchParams.has( 'v' ) && this._tileSetVersion === - 1 ) { |
| 174 | + |
| 175 | + this._tileSetVersion = url.searchParams.get( 'v' ); |
| 176 | + |
| 177 | + } |
| 178 | + |
| 179 | + if ( json.attributions ) { |
| 180 | + |
| 181 | + this._attributions = json.attributions.map( att => ( { |
| 182 | + value: att.html, |
| 183 | + type: 'html', |
| 184 | + collapsible: att.collapsible, |
| 185 | + } ) ); |
| 186 | + |
| 187 | + } |
| 188 | + |
| 189 | + } |
| 190 | + |
| 191 | + } |
| 192 | + |
| 193 | +} |
0 commit comments