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 .
* /
2025-02-19 15:32:12 +01:00
import path from 'path' ;
2025-02-07 13:54:01 -08:00
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 ) {
2025-04-30 18:57:59 -07: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 , ( ) = > {
2025-04-30 18:57:59 -07: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 ;
2025-04-30 18:57:59 -07:00
page . browserContext . _downloads . add ( this ) ;
2020-05-12 19:23:08 -07:00
if ( suggestedFilename !== undefined )
2024-09-20 13:08:33 -07:00
this . _fireDownloadEvent ( ) ;
}
page ( ) : Page {
return this . _page ;
2020-05-12 19:23:08 -07:00
}
_filenameSuggested ( suggestedFilename : string ) {
assert ( this . _suggestedFilename === undefined ) ;
this . _suggestedFilename = suggestedFilename ;
2024-09-20 13:08:33 -07:00
this . _fireDownloadEvent ( ) ;
2020-04-02 17:56:14 -07:00
}
2020-05-12 19:23:08 -07:00
suggestedFilename ( ) : string {
return this . _suggestedFilename ! ;
}
2024-09-20 13:08:33 -07:00
private _fireDownloadEvent() {
this . _page . instrumentation . onDownload ( this . _page , this ) ;
this . _page . emit ( Page . Events . Download , this ) ;
}
2020-04-02 17:56:14 -07:00
}