@@ -21,37 +21,41 @@ export default class VideoDownloader {
21
21
* Returns a `Node.js` stream obtained by sending a GET request to `this.url`.
22
22
* @param headers Stores optional headers as object
23
23
*/
24
- public async stream ( headers ?: object , preText ?: string ) {
25
- const stream = await axios ( {
26
- method : 'get' ,
27
- url : this . url ,
28
- responseType : 'stream' ,
29
- headers,
30
- } ) ;
31
-
32
- if ( this . logger ) {
33
- const progressBar = new ProgressBar ( stream . headers [ 'content-length' ] , preText , this . logger ) ;
34
- stream . data . on ( 'data' , ( chunk : string ) => {
35
- progressBar . update ( chunk . length ) ;
36
- } ) ;
37
- }
24
+ public async stream ( headers ?: object ) {
25
+ const stream = await this . request ( headers ) ;
38
26
return stream . data ;
39
27
}
40
28
41
29
/**
42
30
* Saves the downloaded stream to a file specified by `filename`.
43
31
* @param filename Stores the filename to store the downloaded stream in
44
32
*/
45
- public async download ( filename : string ) : Promise < void > {
46
- const videoStream = await this . stream ( { } , 'Downloading' ) ;
33
+ public async download ( filename : string , headers ?: object ) : Promise < void > {
34
+ const stream = await this . request ( headers ) ;
35
+
36
+ if ( this . logger ) {
37
+ const progressBar = new ProgressBar ( stream . headers [ 'content-length' ] , 'Downloading' , this . logger ) ;
38
+ stream . data . on ( 'data' , ( chunk : string ) => {
39
+ progressBar . update ( chunk . length ) ;
40
+ } ) ;
41
+ }
47
42
48
43
return new Promise ( ( resolve , reject ) => {
49
- videoStream
44
+ stream . data
50
45
. pipe ( fs . createWriteStream ( filename ) )
51
46
. on ( 'finish' , ( err : Error ) => {
52
47
if ( err ) reject ( err ) ;
53
48
else resolve ( ) ;
54
49
} ) ;
55
50
} ) ;
56
51
}
52
+
53
+ private async request ( headers ?: object ) {
54
+ return axios ( {
55
+ method : 'get' ,
56
+ url : this . url ,
57
+ responseType : 'stream' ,
58
+ headers,
59
+ } ) ;
60
+ }
57
61
}
0 commit comments