yt-dlp download/update logic fixes

release
aandrew-me 6 days ago
parent 217c10967b
commit f81ed1c7ac

@ -277,49 +277,43 @@ class YtDownloaderApp {
const defaultYtDlpName = platform() === "win32" ? "ytdlp.exe" : "ytdlp"; const defaultYtDlpName = platform() === "win32" ? "ytdlp.exe" : "ytdlp";
const defaultYtDlpPath = join(hiddenDir, defaultYtDlpName); const defaultYtDlpPath = join(hiddenDir, defaultYtDlpName);
const isMacOS = platform() === "darwin"; const isMacOS = platform() === "darwin";
const isFreeBSD = platform() === "freebsd";
// Priority 1: Environment Variable let executablePath = null;
// PRIORITY 1: Environment Variable
if (process.env.YTDOWNLOADER_YTDLP_PATH) { if (process.env.YTDOWNLOADER_YTDLP_PATH) {
if (existsSync(process.env.YTDOWNLOADER_YTDLP_PATH)) { if (existsSync(process.env.YTDOWNLOADER_YTDLP_PATH)) {
localStorage.setItem( executablePath = process.env.YTDOWNLOADER_YTDLP_PATH;
CONSTANTS.LOCAL_STORAGE_KEYS.YT_DLP_PATH, } else {
process.env.YTDOWNLOADER_YTDLP_PATH
);
return process.env.YTDOWNLOADER_YTDLP_PATH;
}
throw new Error( throw new Error(
"YTDOWNLOADER_YTDLP_PATH is set, but no file exists there." "YTDOWNLOADER_YTDLP_PATH is set, but no file exists there."
); );
} }
}
// Priority 2: System-installed versions (macOS, BSD) // PRIORITY 2: macOS homebrew
if (isMacOS) { else if (isMacOS) {
const possiblePaths = [ const possiblePaths = [
"/opt/homebrew/bin/yt-dlp", "/opt/homebrew/bin/yt-dlp", // Apple Silicon
"/usr/local/bin/yt-dlp", "/usr/local/bin/yt-dlp", // Intel
]; ];
const foundPath = possiblePaths.find((p) => existsSync(p));
if (foundPath) {
localStorage.setItem(
CONSTANTS.LOCAL_STORAGE_KEYS.YT_DLP_PATH,
foundPath
);
return foundPath; executablePath = possiblePaths.find((p) => existsSync(p));
}
// If Homebrew check fails, show popup and abort
if (!executablePath) {
$(CONSTANTS.DOM_IDS.POPUP_BOX_MAC).style.display = "block"; $(CONSTANTS.DOM_IDS.POPUP_BOX_MAC).style.display = "block";
console.warn("Homebrew yt-dlp not found. Prompting user.");
return "" return "";
} else if (platform() === "freebsd") { }
}
// PRIORITY 3: FreeBSD
else if (isFreeBSD) {
try { try {
const foundPath = execSync("which yt-dlp").toString().trim(); executablePath = execSync("which yt-dlp").toString().trim();
localStorage.setItem(
CONSTANTS.LOCAL_STORAGE_KEYS.YT_DLP_PATH,
foundPath
);
} catch { } catch {
throw new Error( throw new Error(
"No yt-dlp found in PATH on FreeBSD. Please install it." "No yt-dlp found in PATH on FreeBSD. Please install it."
@ -327,52 +321,54 @@ class YtDownloaderApp {
} }
} }
// Priority 3: localStorage // PRIORITY 4: LocalStorage or Download (Windows/Linux)
else {
const storedPath = localStorage.getItem( const storedPath = localStorage.getItem(
CONSTANTS.LOCAL_STORAGE_KEYS.YT_DLP_PATH CONSTANTS.LOCAL_STORAGE_KEYS.YT_DLP_PATH
); );
if (isMacOS) { if (storedPath && existsSync(storedPath)) {
const brewPaths = ["/opt/homebrew/bin/brew", "/usr/local/bin/brew"]; executablePath = storedPath;
}
// Download if missing
else {
executablePath = await this.ensureYtDlpBinary(defaultYtDlpPath);
}
}
localStorage.setItem(
CONSTANTS.LOCAL_STORAGE_KEYS.YT_DLP_PATH,
executablePath
);
// Auto update
this._runBackgroundUpdate(executablePath, isMacOS);
let brewFound = false; return executablePath;
}
for (const brewPath of brewPaths) { /**
* yt-dlp background update
*/
_runBackgroundUpdate(executablePath, isMacOS) {
try { try {
await access(brewPath, constants.X_OK); if (isMacOS) {
const brewPaths = [
"/opt/homebrew/bin/brew",
"/usr/local/bin/brew",
];
const brewExec = brewPaths.find((p) => existsSync(p)) || "brew";
const brewUpdate = spawn(brewPath, ["upgrade", "yt-dlp"], { const brewUpdate = spawn(brewExec, ["upgrade", "yt-dlp"]);
shell: true,
});
brewUpdate.on("error", (err) => brewUpdate.on("error", (err) =>
console.error( console.error("Failed to run 'brew upgrade yt-dlp':", err)
"Failed to run 'brew upgrade yt-dlp':",
err
)
); );
brewUpdate.stdout.on("data", (data) => brewUpdate.stdout.on("data", (data) =>
console.log("yt-dlp brew update:", data.toString()) console.log("yt-dlp brew update:", data.toString())
); );
} else {
brewFound = true; const updateProc = spawn(executablePath, ["-U"]);
break;
} catch {}
}
if (!brewFound) {
console.error(
"Homebrew not found in expected locations (/opt/homebrew/bin/brew or /usr/local/bin/brew)."
);
}
} else if (storedPath) {
try {
await access(storedPath, constants.F_OK);
const updateProc = spawn(`"${storedPath}"`, ["-U"], {
shell: true,
});
updateProc.on("error", (err) => updateProc.on("error", (err) =>
console.error( console.error(
@ -382,32 +378,21 @@ class YtDownloaderApp {
); );
updateProc.stdout.on("data", (data) => { updateProc.stdout.on("data", (data) => {
console.log("yt-dlp update check:", data.toString()); const output = data.toString();
console.log("yt-dlp update check:", output);
if (data.toString().toLowerCase().includes("updating to")) { if (output.toLowerCase().includes("updating to")) {
this._showPopup(i18n.__("updatingYtdlp")); this._showPopup(i18n.__("updatingYtdlp"));
} else if ( } else if (
data output.toLowerCase().includes("updated yt-dlp to")
.toString()
.toLowerCase()
.includes("updated yt-dlp to")
) { ) {
this._showPopup(i18n.__("updatedYtdlp")); this._showPopup(i18n.__("updatedYtdlp"));
} }
}); });
} catch {
console.warn("yt-dlp path not found, no update performed.");
} }
} catch (err) {
console.warn("Error initiating background update:", err);
} }
// Priority 4: Default location or download
const ytDlpPath = await this.ensureYtDlpBinary(defaultYtDlpPath);
localStorage.setItem(
CONSTANTS.LOCAL_STORAGE_KEYS.YT_DLP_PATH,
ytDlpPath
);
return ytDlpPath;
} }
/** /**
@ -535,7 +520,7 @@ class YtDownloaderApp {
const possiblePaths = [ const possiblePaths = [
"/opt/homebrew/bin/deno", "/opt/homebrew/bin/deno",
"/usr/local/bin/deno", "/usr/local/bin/deno",
] ];
for (const p of possiblePaths) { for (const p of possiblePaths) {
if (existsSync(p)) { if (existsSync(p)) {
@ -623,7 +608,9 @@ class YtDownloaderApp {
document.addEventListener("keydown", (event) => { document.addEventListener("keydown", (event) => {
if ( if (
((event.ctrlKey && event.key === "v") || ((event.ctrlKey && event.key === "v") ||
(event.metaKey && event.key === "v" && platform() === "darwin")) && (event.metaKey &&
event.key === "v" &&
platform() === "darwin")) &&
document.activeElement.tagName !== "INPUT" && document.activeElement.tagName !== "INPUT" &&
document.activeElement.tagName !== "TEXTAREA" document.activeElement.tagName !== "TEXTAREA"
) { ) {

Loading…
Cancel
Save