2020-04-02 17:56:14 -07: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 06:36:15 -08:00
import path from 'path' ;
2020-04-02 17:56:14 -07:00
import { Page } from './page' ;
2022-04-07 12:55:44 -08:00
import { assert } from '../utils' ;
2021-03-31 10:38:05 -07:00
import { Artifact } from './artifact' ;
2020-04-02 17:56:14 -07:00
2021-02-09 14:44:48 -08:00
export class Download {
2021-03-31 10:38:05 -07:00
readonly artifact : Artifact ;
readonly url : string ;
2020-04-02 17:56:14 -07:00
private _page : Page ;
2020-05-12 19:23:08 -07:00
private _suggestedFilename : string | undefined ;
2020-04-02 17:56:14 -07:00
2020-05-12 19:23:08 -07:00
constructor ( page : Page , downloadsPath : string , uuid : string , url : string , suggestedFilename? : string ) {
2023-08-17 10:57:28 +02:00
const unaccessibleErrorMessage = page . _browserContext . _options . acceptDownloads === 'deny' ? 'Pass { acceptDownloads: true } when you are creating your browser context.' : undefined ;
2021-06-12 21:23:22 +01:00
this . artifact = new Artifact ( page , path . join ( downloadsPath , uuid ) , unaccessibleErrorMessage , ( ) = > {
2022-04-02 18:02:27 -08:00
return this . _page . _browserContext . cancelDownload ( uuid ) ;
2021-06-12 21:23:22 +01:00
} ) ;
2020-04-02 17:56:14 -07:00
this . _page = page ;
2021-03-31 10:38:05 -07:00
this . url = url ;
2020-05-12 19:23:08 -07:00
this . _suggestedFilename = suggestedFilename ;
2020-04-02 17:56:14 -07:00
page . _browserContext . _downloads . add ( this ) ;
2020-05-12 19:23:08 -07:00
if ( suggestedFilename !== undefined )
2020-08-21 16:26:33 -07:00
this . _page . emit ( Page . Events . Download , this ) ;
2020-05-12 19:23:08 -07:00
}
_filenameSuggested ( suggestedFilename : string ) {
assert ( this . _suggestedFilename === undefined ) ;
this . _suggestedFilename = suggestedFilename ;
2020-08-21 16:26:33 -07:00
this . _page . emit ( Page . Events . Download , this ) ;
2020-04-02 17:56:14 -07:00
}
2020-05-12 19:23:08 -07:00
suggestedFilename ( ) : string {
return this . _suggestedFilename ! ;
}
2020-04-02 17:56:14 -07:00
}