Skip to content

Commit db8e80a

Browse files
committed
Fix YT-dlp error handler for Direct
fix deletion of Sharedown working directory fix deadlock on file/folder deletion
1 parent d083a53 commit db8e80a

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

preload.js

+29-24
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ const SharedownAPI = (() => {
8282
for (const logf of logsP) {
8383
const old = `${logf}.old`;
8484

85-
if (_fs.existsSync(old))
86-
_fs.unlinkSync(old);
85+
_unlinkSync(old);
8786

8887
if (_fs.existsSync(logf))
8988
_fs.renameSync(logf, old);
@@ -377,6 +376,16 @@ const SharedownAPI = (() => {
377376
return '';
378377
}
379378

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+
380389
api.hasFFmpeg = () => {
381390
const proc = require('child_process');
382391

@@ -523,8 +532,7 @@ const SharedownAPI = (() => {
523532

524533
ffmpegCmd.on('error', (err) => {
525534
try {
526-
if (_fs.existsSync(outFile))
527-
_fs.unlinkSync(outFile);
535+
_unlinkSync(outFile);
528536

529537
} catch (e) {
530538
api.showMessage('error', e.message, 'FFmpeg');
@@ -554,9 +562,9 @@ const SharedownAPI = (() => {
554562
const videoProgBarTx = videoProgBar.parentNode.querySelector('.progtext');
555563
const args = ['--no-part'];
556564
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;
560568

561569
if (!isDirect) {
562570
const outFPath = _path.parse(outFile);
@@ -566,9 +574,7 @@ const SharedownAPI = (() => {
566574
tmpFold = _path.normalize(_path.join(outFolder, 'sharedownTmp'));
567575
tmpOutFile = _path.normalize(_path.join(tmpFold, filename));
568576

569-
if (_fs.existsSync(tmpFold))
570-
_fs.rmSync(tmpFold, {force: true, recursive: true});
571-
577+
_rmSync(tmpFold);
572578
_fs.mkdirSync(tmpFold);
573579
args.push('-N', settings.ytdlpN.toString(), '-o', tmpOutFile, '-v', videoData.m);
574580

@@ -621,14 +627,11 @@ const SharedownAPI = (() => {
621627
videoProgBar.style.width = '0%'; // windows workaround
622628

623629
if (isDirect)
624-
_fs.rmSync(outFile);
630+
_unlinkSync(outFile);
625631
else
626-
_fs.rmSync(tmpFold, { force: true, recursive: true });
627-
628-
if (code !== null)
629-
throw new Error(`Exit code: ${code}`);
632+
_rmSync(tmpFold);
630633

631-
return;
634+
throw new Error("Exit code: " + (code ?? "aborted"));
632635
}
633636

634637
if (isDirect) {
@@ -641,12 +644,12 @@ const SharedownAPI = (() => {
641644
if (!f.includes(filename))
642645
continue;
643646

644-
_fs.copyFileSync(_path.resolve(tmpOutFile), _path.resolve(outFile));
647+
_fs.copyFileSync(tmpOutFile, outFile);
645648
found = true;
646649
break;
647650
}
648651

649-
_fs.rmSync(_path.resolve(tmpFold), { force: true, recursive: true });
652+
_rmSync(tmpFold);
650653

651654
if (!found)
652655
throw new Error(`Cannot find video file in output folder!\n\nSrc:\n${tmpOutFile}\n\nDest:\n${outFile}`);
@@ -656,7 +659,11 @@ const SharedownAPI = (() => {
656659
} catch (e) {
657660
const failEvt = new CustomEvent('DownloadFail', {detail: `YT-dlp error:\n\n${e.message}`});
658661

659-
_fs.rmSync(_path.resolve(tmpFold), { force: true, recursive: true });
662+
if (isDirect)
663+
_unlinkSync(outFile);
664+
else
665+
_rmSync(tmpFold);
666+
660667
_writeLog(`YT-dlp: download failed:\n${e.message}`);
661668
window.dispatchEvent(failEvt);
662669
}
@@ -736,11 +743,10 @@ const SharedownAPI = (() => {
736743
const _ologFilePath = _path.normalize(_sharedownAppDataPath+'/sharedownLog.log');
737744
const oldF = _ologFilePath+'.old';
738745

739-
if (_fs.existsSync(oldF))
740-
_fs.unlinkSync(oldF);
746+
_unlinkSync(oldF);
741747

742748
if (_fs.existsSync(_logFilePath))
743-
_fs.unlinkSync(_ologFilePath)
749+
_unlinkSync(_ologFilePath);
744750
}
745751
}
746752

@@ -772,8 +778,7 @@ const SharedownAPI = (() => {
772778
}
773779

774780
api.deleteUserdataFold = () => {
775-
if (_fs.existsSync(_chromeUserdataPath))
776-
_fs.rmSync(_chromeUserdataPath, {force: true, recursive: true});
781+
_rmSync(_chromeUserdataPath);
777782
}
778783

779784
api.md5sum = s => {

0 commit comments

Comments
 (0)