2020-04-03 02:56:14 +02:00
/ * *
* Copyright ( c ) Microsoft Corporation .
*
* Licensed under the Apache License , Version 2.0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
*
* http : //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an "AS IS" BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
* See the License for the specific language governing permissions and
* limitations under the License .
* /
2021-02-11 15:36:15 +01:00
import path from 'path' ;
2020-04-03 02:56:14 +02:00
import { Page } from './page' ;
2020-08-26 21:46:30 +02:00
import { assert } from '../utils/utils' ;
2021-03-31 19:38:05 +02:00
import { Artifact } from './artifact' ;
2020-04-03 02:56:14 +02:00
2021-02-09 23:44:48 +01:00
export class Download {
2021-03-31 19:38:05 +02:00
readonly artifact : Artifact ;
readonly url : string ;
2020-04-03 02:56:14 +02:00
private _page : Page ;
2020-05-13 04:23:08 +02:00
private _suggestedFilename : string | undefined ;
2020-04-03 02:56:14 +02:00
2020-05-13 04:23:08 +02:00
constructor ( page : Page , downloadsPath : string , uuid : string , url : string , suggestedFilename? : string ) {
2021-03-31 19:38:05 +02:00
const unaccessibleErrorMessage = ! page . _browserContext . _options . acceptDownloads ? 'Pass { acceptDownloads: true } when you are creating your browser context.' : undefined ;
2021-06-12 22:23:22 +02:00
this . artifact = new Artifact ( page , path . join ( downloadsPath , uuid ) , unaccessibleErrorMessage , ( ) = > {
return this . _page . _browserContext . _doCancelDownload ( uuid ) ;
} ) ;
2020-04-03 02:56:14 +02:00
this . _page = page ;
2021-03-31 19:38:05 +02:00
this . url = url ;
2020-05-13 04:23:08 +02:00
this . _suggestedFilename = suggestedFilename ;
2020-04-03 02:56:14 +02:00
page . _browserContext . _downloads . add ( this ) ;
2020-05-13 04:23:08 +02:00
if ( suggestedFilename !== undefined )
2020-08-22 01:26:33 +02:00
this . _page . emit ( Page . Events . Download , this ) ;
2020-05-13 04:23:08 +02:00
}
_filenameSuggested ( suggestedFilename : string ) {
assert ( this . _suggestedFilename === undefined ) ;
this . _suggestedFilename = suggestedFilename ;
2020-08-22 01:26:33 +02:00
this . _page . emit ( Page . Events . Download , this ) ;
2020-04-03 02:56:14 +02:00
}
2020-05-13 04:23:08 +02:00
suggestedFilename ( ) : string {
return this . _suggestedFilename ! ;
}
2020-04-03 02:56:14 +02:00
}