@@ -82,8 +82,7 @@ const SharedownAPI = (() => {
82
82
for ( const logf of logsP ) {
83
83
const old = `${ logf } .old` ;
84
84
85
- if ( _fs . existsSync ( old ) )
86
- _fs . unlinkSync ( old ) ;
85
+ _unlinkSync ( old ) ;
87
86
88
87
if ( _fs . existsSync ( logf ) )
89
88
_fs . renameSync ( logf , old ) ;
@@ -377,6 +376,16 @@ const SharedownAPI = (() => {
377
376
return '' ;
378
377
}
379
378
379
+ function _rmSync ( path , recur = true ) {
380
+ if ( _fs . existsSync ( path ) )
381
+ _fs . rmSync ( path , { recursive : recur , force : true } ) ;
382
+ }
383
+
384
+ function _unlinkSync ( path ) {
385
+ if ( _fs . existsSync ( path ) )
386
+ _fs . unlinkSync ( path ) ;
387
+ }
388
+
380
389
api . hasFFmpeg = ( ) => {
381
390
const proc = require ( 'child_process' ) ;
382
391
@@ -523,8 +532,7 @@ const SharedownAPI = (() => {
523
532
524
533
ffmpegCmd . on ( 'error' , ( err ) => {
525
534
try {
526
- if ( _fs . existsSync ( outFile ) )
527
- _fs . unlinkSync ( outFile ) ;
535
+ _unlinkSync ( outFile ) ;
528
536
529
537
} catch ( e ) {
530
538
api . showMessage ( 'error' , e . message , 'FFmpeg' ) ;
@@ -554,9 +562,9 @@ const SharedownAPI = (() => {
554
562
const videoProgBarTx = videoProgBar . parentNode . querySelector ( '.progtext' ) ;
555
563
const args = [ '--no-part' ] ;
556
564
const isDirect = videoData . c !== null ;
557
- let tmpFold = '' ;
558
- let tmpOutFile = '' ;
559
- let filename = '' ;
565
+ let tmpFold = null ;
566
+ let tmpOutFile = null ;
567
+ let filename = null ;
560
568
561
569
if ( ! isDirect ) {
562
570
const outFPath = _path . parse ( outFile ) ;
@@ -566,9 +574,7 @@ const SharedownAPI = (() => {
566
574
tmpFold = _path . normalize ( _path . join ( outFolder , 'sharedownTmp' ) ) ;
567
575
tmpOutFile = _path . normalize ( _path . join ( tmpFold , filename ) ) ;
568
576
569
- if ( _fs . existsSync ( tmpFold ) )
570
- _fs . rmSync ( tmpFold , { force : true , recursive : true } ) ;
571
-
577
+ _rmSync ( tmpFold ) ;
572
578
_fs . mkdirSync ( tmpFold ) ;
573
579
args . push ( '-N' , settings . ytdlpN . toString ( ) , '-o' , tmpOutFile , '-v' , videoData . m ) ;
574
580
@@ -621,14 +627,11 @@ const SharedownAPI = (() => {
621
627
videoProgBar . style . width = '0%' ; // windows workaround
622
628
623
629
if ( isDirect )
624
- _fs . rmSync ( outFile ) ;
630
+ _unlinkSync ( outFile ) ;
625
631
else
626
- _fs . rmSync ( tmpFold , { force : true , recursive : true } ) ;
627
-
628
- if ( code !== null )
629
- throw new Error ( `Exit code: ${ code } ` ) ;
632
+ _rmSync ( tmpFold ) ;
630
633
631
- return ;
634
+ throw new Error ( "Exit code: " + ( code ?? "aborted" ) ) ;
632
635
}
633
636
634
637
if ( isDirect ) {
@@ -641,12 +644,12 @@ const SharedownAPI = (() => {
641
644
if ( ! f . includes ( filename ) )
642
645
continue ;
643
646
644
- _fs . copyFileSync ( _path . resolve ( tmpOutFile ) , _path . resolve ( outFile ) ) ;
647
+ _fs . copyFileSync ( tmpOutFile , outFile ) ;
645
648
found = true ;
646
649
break ;
647
650
}
648
651
649
- _fs . rmSync ( _path . resolve ( tmpFold ) , { force : true , recursive : true } ) ;
652
+ _rmSync ( tmpFold ) ;
650
653
651
654
if ( ! found )
652
655
throw new Error ( `Cannot find video file in output folder!\n\nSrc:\n${ tmpOutFile } \n\nDest:\n${ outFile } ` ) ;
@@ -656,7 +659,11 @@ const SharedownAPI = (() => {
656
659
} catch ( e ) {
657
660
const failEvt = new CustomEvent ( 'DownloadFail' , { detail : `YT-dlp error:\n\n${ e . message } ` } ) ;
658
661
659
- _fs . rmSync ( _path . resolve ( tmpFold ) , { force : true , recursive : true } ) ;
662
+ if ( isDirect )
663
+ _unlinkSync ( outFile ) ;
664
+ else
665
+ _rmSync ( tmpFold ) ;
666
+
660
667
_writeLog ( `YT-dlp: download failed:\n${ e . message } ` ) ;
661
668
window . dispatchEvent ( failEvt ) ;
662
669
}
@@ -736,11 +743,10 @@ const SharedownAPI = (() => {
736
743
const _ologFilePath = _path . normalize ( _sharedownAppDataPath + '/sharedownLog.log' ) ;
737
744
const oldF = _ologFilePath + '.old' ;
738
745
739
- if ( _fs . existsSync ( oldF ) )
740
- _fs . unlinkSync ( oldF ) ;
746
+ _unlinkSync ( oldF ) ;
741
747
742
748
if ( _fs . existsSync ( _logFilePath ) )
743
- _fs . unlinkSync ( _ologFilePath )
749
+ _unlinkSync ( _ologFilePath ) ;
744
750
}
745
751
}
746
752
@@ -772,8 +778,7 @@ const SharedownAPI = (() => {
772
778
}
773
779
774
780
api . deleteUserdataFold = ( ) => {
775
- if ( _fs . existsSync ( _chromeUserdataPath ) )
776
- _fs . rmSync ( _chromeUserdataPath , { force : true , recursive : true } ) ;
781
+ _rmSync ( _chromeUserdataPath ) ;
777
782
}
778
783
779
784
api . md5sum = s => {
0 commit comments