Spaces:
Running
Running
Update backend/server.js
Browse files- backend/server.js +12 -7
backend/server.js
CHANGED
|
@@ -28,13 +28,19 @@ apiRouter.post('/download', async (req, res) => {
|
|
| 28 |
const { url, quality, platform, sessionId, creditCost } = req.body;
|
| 29 |
|
| 30 |
try {
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
const timestamp = Date.now();
|
| 34 |
const fileBaseName = `tm_dl_${timestamp}`;
|
| 35 |
const outputDir = '/tmp';
|
| 36 |
|
| 37 |
-
// Determine file extension and format string
|
| 38 |
const extension = quality === 'audio' ? 'mp3' : 'mp4';
|
| 39 |
const outputPath = path.join(outputDir, `${fileBaseName}.${extension}`);
|
| 40 |
|
|
@@ -53,8 +59,6 @@ apiRouter.post('/download', async (req, res) => {
|
|
| 53 |
|
| 54 |
const tiktokArgs = platform === 'tiktok' ? '--referer "https://www.tiktok.com/"' : '';
|
| 55 |
|
| 56 |
-
// CRITICAL FIX: Added --js-runtime node to fix JS engine missing error.
|
| 57 |
-
// Added --no-warnings and --no-check-certificate for better reliability on Cloud Run.
|
| 58 |
const command = `yt-dlp --no-playlist --no-warnings --no-check-certificate --js-runtime node ${formatStr} ${tiktokArgs} -o "${outputPath}" "${url.trim()}"`;
|
| 59 |
|
| 60 |
console.log(`[DOWNLOADER] Executing: ${command}`);
|
|
@@ -63,18 +67,19 @@ apiRouter.post('/download', async (req, res) => {
|
|
| 63 |
await execPromise(command);
|
| 64 |
} catch (execErr) {
|
| 65 |
console.error("[DL-EXEC-FAIL]", execErr.stderr || execErr.message);
|
| 66 |
-
// Even if warnings are printed to stderr, check if file exists before crashing
|
| 67 |
if (!fs.existsSync(outputPath)) {
|
| 68 |
throw new Error(`Engine Error: ${execErr.stderr || execErr.message}`);
|
| 69 |
}
|
| 70 |
}
|
| 71 |
|
| 72 |
-
// Verify file existence explicitly
|
| 73 |
if (!fs.existsSync(outputPath)) {
|
| 74 |
throw new Error("Download engine failed. File not generated.");
|
| 75 |
}
|
| 76 |
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
res.download(outputPath, `${fileBaseName}.${extension}`, (err) => {
|
| 80 |
if (err) console.error("Stream Error:", err);
|
|
|
|
| 28 |
const { url, quality, platform, sessionId, creditCost } = req.body;
|
| 29 |
|
| 30 |
try {
|
| 31 |
+
let eligibility = null;
|
| 32 |
+
|
| 33 |
+
// BYPASS LOGIC: Only check eligibility and deduct credits if NOT tiktok
|
| 34 |
+
if (platform !== 'tiktok') {
|
| 35 |
+
eligibility = await CreditService.checkEligibility(sessionId, null, 'downloader', false, creditCost);
|
| 36 |
+
} else {
|
| 37 |
+
console.log(`[DOWNLOADER] TikTok bypass triggered for URL: ${url}`);
|
| 38 |
+
}
|
| 39 |
|
| 40 |
const timestamp = Date.now();
|
| 41 |
const fileBaseName = `tm_dl_${timestamp}`;
|
| 42 |
const outputDir = '/tmp';
|
| 43 |
|
|
|
|
| 44 |
const extension = quality === 'audio' ? 'mp3' : 'mp4';
|
| 45 |
const outputPath = path.join(outputDir, `${fileBaseName}.${extension}`);
|
| 46 |
|
|
|
|
| 59 |
|
| 60 |
const tiktokArgs = platform === 'tiktok' ? '--referer "https://www.tiktok.com/"' : '';
|
| 61 |
|
|
|
|
|
|
|
| 62 |
const command = `yt-dlp --no-playlist --no-warnings --no-check-certificate --js-runtime node ${formatStr} ${tiktokArgs} -o "${outputPath}" "${url.trim()}"`;
|
| 63 |
|
| 64 |
console.log(`[DOWNLOADER] Executing: ${command}`);
|
|
|
|
| 67 |
await execPromise(command);
|
| 68 |
} catch (execErr) {
|
| 69 |
console.error("[DL-EXEC-FAIL]", execErr.stderr || execErr.message);
|
|
|
|
| 70 |
if (!fs.existsSync(outputPath)) {
|
| 71 |
throw new Error(`Engine Error: ${execErr.stderr || execErr.message}`);
|
| 72 |
}
|
| 73 |
}
|
| 74 |
|
|
|
|
| 75 |
if (!fs.existsSync(outputPath)) {
|
| 76 |
throw new Error("Download engine failed. File not generated.");
|
| 77 |
}
|
| 78 |
|
| 79 |
+
// Only commit deduction if eligibility was checked (non-tiktok)
|
| 80 |
+
if (eligibility) {
|
| 81 |
+
await CreditService.commitDeduction(eligibility);
|
| 82 |
+
}
|
| 83 |
|
| 84 |
res.download(outputPath, `${fileBaseName}.${extension}`, (err) => {
|
| 85 |
if (err) console.error("Stream Error:", err);
|