import Uploader from "./Uploader" ; import BaseTask from "./task/BaseTask" ; import TokenFunc from "./TokenFunc" ; import Interceptor from "./interceptor/UploadInterceptor" ; import SimpleUploadInterceptor from "./interceptor/SimpleUploadInterceptor" ; import UploadListener from "./hook/UploadListener" ; import { Scheme , Domain } from "./url/Domain" ; /**
 * UploaderBuilder
 *
 */ class UploaderBuilder { public static MAX_CHUNK_SIZE = 4 * 1024 * 1024 ; public static BLOCK_SIZE = UploaderBuilder . MAX_CHUNK_SIZE ; public static UPLOAD_DOMAIN = { http : 'http://upload.qiniu.com' , https : 'https://up.qbox.me' } ; private _retry : number = 0 ; private _domain : Domain = UploaderBuilder . UPLOAD_DOMAIN ; private _scheme : Scheme = null ; private _size : number = 1024 * 1024 ; private _chunk : boolean = true ; private _auto : boolean = true ; private _multiple : boolean = true ; private _accept : string [ ] = [ ] ; private _button : string ; private _buttonEventName : string ; private _compress : number = 1 ; private _scale : number [ ] = [ 0 , 0 ] ; private _listener : UploadListener ; private _saveKey : boolean | string = false ; private _tokenFunc : TokenFunc ; private _tokenShare : boolean = true ; private _interceptors : Interceptor [ ] = [ ] ; private _isDebug : boolean = false ; param domain returns { UploaderBuilder } public domain ( domain ) { this . _domain = domain ; return this ; } param scheme returns { UploaderBuilder } public scheme ( scheme ) { this . _scheme = scheme ; return this ; } param interceptor returns { UploaderBuilder } public interceptor ( interceptor ) { this . _interceptors . push ( Object . assign ( new SimpleUploadInterceptor ( ) , interceptor ) ) ; return this ; } param retry returns { UploaderBuilder } public retry ( retry ) { this . _retry = retry ; return this ; } param size returns { UploaderBuilder } public size ( size ) { this . _size = Math . min ( Math . max ( size , 1 ) , UploaderBuilder . MAX_CHUNK_SIZE ) ; return this ; } param auto returns { UploaderBuilder } public auto ( auto ) { this . _auto = auto ; return this ; } param multiple returns { UploaderBuilder } public multiple ( multiple ) { this . _multiple = multiple ; return this ; } param accept returns { UploaderBuilder } public accept ( accept ) { this . _accept = accept ; return this ; } param button param eventName returns { UploaderBuilder } public button ( button , eventName = "click" ) { this . _button = button ; this . _buttonEventName = eventName ; return this ; } param compress returns { UploaderBuilder } public compress ( compress ) { this . _compress = Math . max ( Math . min ( compress , 1 ) , 0 ) ; return this ; } returns { UploaderBuilder } param scale public scale ( scale ) { this . _scale = scale ; return this ; } param saveKey returns { UploaderBuilder } public saveKey ( saveKey ) { this . _saveKey = saveKey ; return this ; } param tokenUrl returns { UploaderBuilder } public tokenUrl ( tokenUrl ) { this . _tokenFunc = ( uploader , task ) => { return uploader . requestTaskToken ( task , tokenUrl ) ; } ; return this ; } param tokenFunc returns { UploaderBuilder } public tokenFunc ( tokenFunc ) { this . _tokenFunc = ( uploader , task ) => { return new Promise ( ( resolve ) => { tokenFunc ( resolve , task ) ; } ) ; } ; return this ; } param listener returns { UploaderBuilder } public listener ( listener ) { this . _listener = listener ; return this ; } param tokenShare returns { UploaderBuilder } public tokenShare ( tokenShare ) { this . _tokenShare = tokenShare ; return this ; } param chunk returns { UploaderBuilder } public chunk ( chunk ) { this . _chunk = chunk ; return this ; } param debug returns { UploaderBuilder } public debug ( debug ) { this . _isDebug = debug ; return this ; } get getRetry ( ) : number { return this . _retry ; } get getSize ( ) : number { return this . _size ; } get getAuto ( ) : boolean { return this . _auto ; } get getMultiple ( ) : boolean { return this . _multiple ; } get getAccept ( ) : string [ ] { return this . _accept ; } get getButton ( ) : string { return this . _button ; } get getButtonEventName ( ) : string { return this . _buttonEventName ; } get getCompress ( ) : number { return this . _compress ; } get getScale ( ) : number [ ] { return this . _scale ; } get getListener ( ) : UploadListener { return this . _listener ; } get getSaveKey ( ) : boolean | string { return this . _saveKey ; } get getTokenFunc ( ) : TokenFunc { return this . _tokenFunc ; } get getTokenShare ( ) : boolean { return this . _tokenShare ; } get getChunk ( ) : boolean { return this . _chunk ; } get getIsDebug ( ) : boolean { return this . _isDebug ; } get getInterceptors ( ) : Interceptor [ ] { return this . _interceptors ; } get getDomain ( ) : string { let domain = this . _domain ; if ( ! domain ) { domain = UploaderBuilder . UPLOAD_DOMAIN ; } if ( typeof domain != "string" ) { let scheme = this . _scheme ; if ( typeof scheme != "string" ) { let protocol = window . location . protocol ; scheme = protocol . substring ( 0 , protocol . length - 1 ) as Scheme ; } domain = domain [ scheme ] ; } return domain . endsWith ( '/' ) ? domain . substring ( 0 , domain . length - 1 ) : domain ; } public build ( ) { return new Uploader ( this ) ; } } export default UploaderBuilder ;