packageDocumentation module import { AccessToken , GuidString , Logger , PerfLogger } from "@itwin/core-bentley" ; import { CancelRequest , FileHandler } from "../itwin-client/FileHandler" ; import { ProgressCallback } from "../itwin-client/Request" ; import { IModelHubClientLoggerCategory } from "../IModelHubClientLoggerCategories" ; import { ECJsonTypeMap , WsgInstance } from "../wsg/ECJsonTypeMap" ; import { WsgQuery } from "../wsg/WsgQuery" ; import { IModelBaseHandler } from "./BaseHandler" ; import { ArgumentCheck , IModelHubClientError } from "./Errors" ; import { addSelectFileAccessKey } from "./HubQuery" ; import { InitializationState } from "./iModels" ; const loggerCategory = IModelHubClientLoggerCategory . IModelHub ; internal @ ECJsonTypeMap . classToJson ( "wsg" , "iModelScope.Checkpoint" , { schemaPropertyName : "schemaName" , classPropertyName : "className" } ) export class Checkpoint extends WsgInstance { /** File name of the master file. */ @ ECJsonTypeMap . propertyToJson ( "wsg" , "properties.FileName" ) public fileName ? : string ; /** Description of the master file. */ @ ECJsonTypeMap . propertyToJson ( "wsg" , "properties.FileDescription" ) public fileDescription ? : string ; /** Size of the checkpoint file. */ @ ECJsonTypeMap . propertyToJson ( "wsg" , "properties.FileSize" ) public fileSize ? : string ; /** FileId of the master file. */ @ ECJsonTypeMap . propertyToJson ( "wsg" , "properties.FileId" ) public fileId ? : GuidString ; /** State of checkpoint generation. */ @ ECJsonTypeMap . propertyToJson ( "wsg" , "properties.State" ) public state ? : InitializationState ; /** Id of the last [[ChangeSet]] that was merged into this checkpoint file. */ @ ECJsonTypeMap . propertyToJson ( "wsg" , "properties.MergedChangeSetId" ) public mergedChangeSetId ? : string ; /** Index of the last [[ChangeSet]] that was merged into this checkpoint file. */ @ ECJsonTypeMap . propertyToJson ( "wsg" , "properties.MergedChangeSetIndex" ) public mergedChangeSetIndex ? : number ; /** Date when this checkpoint file was created. */ @ ECJsonTypeMap . propertyToJson ( "wsg" , "properties.CreatedDate" ) public createdDate ? : string ; /** URL that can be used to download the checkpoint file from iModelHub. See [[CheckpointQuery.selectDownloadUrl]]. */ @ ECJsonTypeMap . propertyToJson ( "wsg" , "relationshipInstances[FileAccessKey].relatedInstance[AccessKey].properties.DownloadUrl" ) public downloadUrl ? : string ; } internal export class CheckpointQuery extends WsgQuery { returns public nearestCheckpoint ( targetChangeSetId ) { this . filter ( `NearestCheckpoint-backward-ChangeSet.Id+eq+'${ targetChangeSetId }'` ) ; return this ; } returns public precedingCheckpoint ( targetChangeSetId ) { this . filter ( `PrecedingCheckpoint-backward-ChangeSet.Id+eq+'${ targetChangeSetId }'` ) ; return this ; } returns public byChangeSetId ( changeSetId ) { this . addFilter ( `MergedChangeSetId+eq+'${ changeSetId }'` , "and" ) ; return this ; } returns public selectDownloadUrl ( ) { addSelectFileAccessKey ( this . _query ) ; return this ; } } internal export class CheckpointHandler { private _handler : IModelBaseHandler ; private _fileHandler ? : FileHandler ; param handler param fileHandler internal constructor ( handler , fileHandler ? ) { this . _handler = handler ; this . _fileHandler = fileHandler ; } param iModelId internal private getRelativeUrl ( iModelId ) { return `/Repositories/iModel--${ iModelId }/iModelScope/Checkpoint/` ; } param iModelId param query returns throws public async get ( accessToken , iModelId , query = new CheckpointQuery ( ) ) { Logger . logInfo ( loggerCategory , "Querying checkpoints for iModel" , ( ) => ( { iModelId } ) ) ; ArgumentCheck . validGuid ( "iModelId" , iModelId ) ; const checkpoints = await this . _handler . getInstances < Checkpoint > ( accessToken , Checkpoint , this . getRelativeUrl ( iModelId ) , query . getQueryOptions ( ) ) ; Logger . logTrace ( loggerCategory , "Queried checkpoints for iModel" , ( ) => ( { iModelId , count : checkpoints . length } ) ) ; return checkpoints ; } param url private static getSafeUrlForLogging ( url ) { const safeToLogDownloadUrl = new URL ( url ) ; if ( safeToLogDownloadUrl . search && safeToLogDownloadUrl . search . length > 0 ) safeToLogDownloadUrl . search = "..." ; if ( safeToLogDownloadUrl . hash && safeToLogDownloadUrl . hash . length > 0 ) safeToLogDownloadUrl . hash = "..." ; return safeToLogDownloadUrl . toString ( ) ; } param checkpoint param path param progressCallback param cancelRequest throws throws throws throws public async download ( accessToken , checkpoint , path , progressCallback ? , cancelRequest ? ) { ArgumentCheck . defined ( "checkpoint" , checkpoint ) ; ArgumentCheck . defined ( "path" , path ) ; if ( typeof window !== "undefined" ) throw IModelHubClientError . browser ( ) ; if ( ! this . _fileHandler ) throw IModelHubClientError . fileHandler ( ) ; if ( ! checkpoint . downloadUrl ) throw IModelHubClientError . missingDownloadUrl ( "checkpoint" ) ; const checkpointForLog = new Checkpoint ( ) ; Object . assign ( checkpointForLog , checkpoint ) ; checkpointForLog . downloadUrl = CheckpointHandler . getSafeUrlForLogging ( checkpointForLog . downloadUrl ! ) ; const perfLogger = new PerfLogger ( "Downloading checkpoint" , ( ) => ( { ... checkpointForLog , path , iModelId : checkpoint . fileId } ) ) ; await this . _fileHandler . downloadFile ( accessToken , checkpoint . downloadUrl , path , parseInt ( checkpoint . fileSize ! , 10 ) , progressCallback , cancelRequest ) ; perfLogger . dispose ( ) ; } }