packageDocumentation module import * as fs from "fs-extra" ; import * as http from "http" ; import * as https from "https" ; import * as path from "path" ; import { URL } from "url" ; import { CancelRequest , FileHandler } from "../itwin-client/FileHandler" ; import { ProgressCallback } from "./Request" ; import { downloadFileAtomic } from "./downloadFileAtomic" ; import { AccessToken } from "@itwin/core-bentley" ; internal export class UrlFileHandler implements FileHandler { public agent ? : https . Agent ; protected _uploadMethod = "POST" ; constructor ( ) { } /** Create a directory, recursively setting up the path as necessary */ private static makeDirectoryRecursive ( dirPath ) { if ( fs . existsSync ( dirPath ) ) return ; UrlFileHandler . makeDirectoryRecursive ( path . dirname ( dirPath ) ) ; fs . mkdirSync ( dirPath ) ; } public async downloadFile ( _accessToken , downloadUrl , downloadToPathname , fileSize ? , progressCallback ? , cancelRequest ? ) { if ( fs . existsSync ( downloadToPathname ) ) fs . unlinkSync ( downloadToPathname ) ; UrlFileHandler . makeDirectoryRecursive ( path . dirname ( downloadToPathname ) ) ; return downloadFileAtomic ( downloadUrl , downloadToPathname , fileSize , progressCallback , cancelRequest ) ; } public async uploadFile ( _accessToken , uploadUrlString , uploadFromPathname , progressCallback ? ) { return new Promise < void > ( ( resolve , reject ) => { const uploadUrl = new URL ( uploadUrlString ) ; const fileSize = this . getFileSize ( uploadFromPathname ) ; const requestOptions = { method : this . _uploadMethod , hostname : uploadUrl . hostname , port : uploadUrl . port , path : uploadUrl . pathname + uploadUrl . search } ; const callback = ( response ) => { if ( response . statusCode === 200 || response . statusCode === 201 ) { if ( progressCallback ) progressCallback ( { percent : 100 , total : fileSize , loaded : fileSize } ) ; resolve ( ) ; } else { reject ( new Error ( response . statusCode ! . toString ( ) ) ) ; } } ; const request = uploadUrlString . startsWith ( "https:" ) ? https . request ( requestOptions , callback ) : http . request ( requestOptions , callback ) ; let bytesWritten = 0 ; const source = fs . createReadStream ( uploadFromPathname ) ; if ( progressCallback ) { source . on ( "data" , ( chunk ) => { bytesWritten += chunk . length ; progressCallback ( { loaded : bytesWritten , total : fileSize , percent : fileSize ? bytesWritten / fileSize : 0 } ) ; } ) ; } source . on ( "error" , ( err ) => { reject ( err ) ; } ) ; source . pipe ( request ) ; } ) ; } param filePath returns public getFileSize ( filePath ) { return fs . statSync ( filePath ) . size ; } param filePath returns public isDirectory ( filePath ) { return fs . statSync ( filePath ) . isDirectory ( ) ; } param filePath returns public exists ( filePath ) { return fs . existsSync ( filePath ) ; } param filePath public unlink ( filePath ) { fs . unlinkSync ( filePath ) ; } param filePath returns public basename ( filePath ) { return path . basename ( filePath ) ; } param paths returns public join ( ... paths ) { return path . join ( ... paths ) ; } }