From 90a24454f64f05e1e53064e3a1a81879d8b95489 Mon Sep 17 00:00:00 2001 From: Peifan Li Date: Thu, 1 Jan 2026 12:13:19 -0500 Subject: [PATCH] refactor: Explicitly preserve network-related options --- .../services/downloaders/ytdlp/ytdlpConfig.ts | 26 ++++++++++++++++- .../services/downloaders/ytdlp/ytdlpVideo.ts | 28 +++++++++++++++++-- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/backend/src/services/downloaders/ytdlp/ytdlpConfig.ts b/backend/src/services/downloaders/ytdlp/ytdlpConfig.ts index b2cfd53..ebefe1c 100644 --- a/backend/src/services/downloaders/ytdlp/ytdlpConfig.ts +++ b/backend/src/services/downloaders/ytdlp/ytdlpConfig.ts @@ -37,6 +37,7 @@ export function prepareDownloadFlags( } // Prepare base flags from user config (excluding output options we manage) + // Explicitly preserve network-related options like proxy const { output: _output, // Ignore user output template (we manage this) o: _o, @@ -50,9 +51,18 @@ export function prepareDownloadFlags( convertSubs: userConvertSubs, // Extract user merge output format (use it if provided) mergeOutputFormat: userMergeOutputFormat, + proxy: _proxy, // Proxy is handled separately in networkOptions to ensure it's preserved ...safeUserConfig } = config; + // Explicitly preserve proxy and other network options to ensure they're not lost + // This is critical for download operations that need proxy settings + const networkOptions: Record = {}; + if (config.proxy) { + networkOptions.proxy = config.proxy; + logger.debug("Preserving proxy in networkOptions:", config.proxy); + } + // Get format sort option if user specified it const formatSortValue = userFormatSort || userFormatSort2; @@ -73,8 +83,10 @@ export function prepareDownloadFlags( const mergeOutputFormat = userMergeOutputFormat || defaultMergeFormat; // Prepare flags - defaults first, then user config to allow overrides + // Network options (like proxy) are applied last to ensure they're not overridden const flags: YtDlpFlags = { ...safeUserConfig, // Apply user config + ...networkOptions, // Explicitly apply network options (proxy, etc.) to ensure they're preserved output: outputPath, // Always use our output path with correct extension format: defaultFormat, // Use user preferences if provided, otherwise use defaults @@ -99,7 +111,9 @@ export function prepareDownloadFlags( "bestvideo[ext=mp4][vcodec^=avc1]+bestaudio[ext=m4a]/bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"; } // Ensure merge output format is mp4 (already handled above, but log it) - logger.info("Twitter/X URL detected - using MP4 format for Safari compatibility"); + logger.info( + "Twitter/X URL detected - using MP4 format for Safari compatibility" + ); } // Add YouTube specific flags if it's a YouTube URL @@ -153,6 +167,16 @@ export function prepareDownloadFlags( delete flags.extractorArgs; } + // Log proxy in final flags for debugging + if (flags.proxy) { + logger.debug("Proxy in final flags:", flags.proxy); + } else if (config.proxy) { + logger.warn( + "Proxy was in config but not in final flags. Config proxy:", + config.proxy + ); + } + logger.debug("Final yt-dlp flags:", flags); return { diff --git a/backend/src/services/downloaders/ytdlp/ytdlpVideo.ts b/backend/src/services/downloaders/ytdlp/ytdlpVideo.ts index fb51a26..0f9e34b 100644 --- a/backend/src/services/downloaders/ytdlp/ytdlpVideo.ts +++ b/backend/src/services/downloaders/ytdlp/ytdlpVideo.ts @@ -11,6 +11,7 @@ import { ProgressTracker } from "../../../utils/progressTracker"; import { executeYtDlpJson, executeYtDlpSpawn, + getNetworkConfigFromUserConfig, getUserYtDlpConfig, } from "../../../utils/ytDlpUtils"; import * as storageService from "../../storageService"; @@ -86,8 +87,13 @@ export async function downloadVideo( try { const PROVIDER_SCRIPT = getProviderScript(); + // Get user's yt-dlp configuration for network options (including proxy) + const userConfig = getUserYtDlpConfig(videoUrl); + const networkConfig = getNetworkConfigFromUserConfig(userConfig); + // Get video info first const info = await executeYtDlpJson(videoUrl, { + ...networkConfig, noWarnings: true, preferFreeFormats: true, ...(PROVIDER_SCRIPT @@ -142,16 +148,32 @@ export async function downloadVideo( }); } - // Get user's yt-dlp configuration - const userConfig = getUserYtDlpConfig(videoUrl); + // Get user's yt-dlp configuration (reuse from above if available, otherwise fetch again) + // Note: userConfig was already fetched above, but we need to ensure it's still valid + const downloadUserConfig = userConfig || getUserYtDlpConfig(videoUrl); + + // Log proxy configuration for debugging + if (downloadUserConfig.proxy) { + logger.info("Using proxy for download:", downloadUserConfig.proxy); + } // Prepare download flags const { flags, mergeOutputFormat } = prepareDownloadFlags( videoUrl, newVideoPath, - userConfig + downloadUserConfig ); + // Log final flags to verify proxy is included + if (flags.proxy) { + logger.info("Proxy included in download flags:", flags.proxy); + } else { + logger.warn( + "Proxy not found in download flags. User config proxy:", + downloadUserConfig.proxy + ); + } + // Update the video path to use the correct extension based on merge format const videoExtension = mergeOutputFormat; const newVideoPathWithFormat = newVideoPath.replace(