(dir, fileMap = new Map(), duplicates = new Map())
| 346 | } |
| 347 | |
| 348 | async scanForVideoFiles(dir, fileMap = new Map(), duplicates = new Map()) { |
| 349 | try { |
| 350 | const entries = await fs.readdir(dir, { withFileTypes: true }); |
| 351 | |
| 352 | for (const entry of entries) { |
| 353 | const fullPath = path.join(dir, entry.name); |
| 354 | |
| 355 | if (entry.isDirectory()) { |
| 356 | await this.scanForVideoFiles(fullPath, fileMap, duplicates); |
| 357 | continue; |
| 358 | } |
| 359 | |
| 360 | if (!entry.isFile()) { |
| 361 | continue; |
| 362 | } |
| 363 | |
| 364 | const ext = path.extname(entry.name).toLowerCase(); |
| 365 | if (!MEDIA_EXTENSIONS.includes(ext)) { |
| 366 | continue; |
| 367 | } |
| 368 | |
| 369 | // Match files ending with [<id>].<ext>; <id> is whatever yt-dlp wrote |
| 370 | // between the brackets at the end of the filename. |
| 371 | const match = entry.name.match(/\[([^[\]]+)\]\.[a-z0-9]+$/i); |
| 372 | if (!match) { |
| 373 | continue; |
| 374 | } |
| 375 | |
| 376 | const youtubeId = match[1]; |
| 377 | const isAudio = AUDIO_EXTENSIONS.includes(ext); |
| 378 | const stats = await fs.stat(fullPath); |
| 379 | |
| 380 | if (!fileMap.has(youtubeId)) { |
| 381 | fileMap.set(youtubeId, { |
| 382 | videoFilePath: null, |
| 383 | videoFileSize: null, |
| 384 | audioFilePath: null, |
| 385 | audioFileSize: null |
| 386 | }); |
| 387 | } |
| 388 | |
| 389 | const existing = fileMap.get(youtubeId); |
| 390 | const pathKey = isAudio ? 'audioFilePath' : 'videoFilePath'; |
| 391 | const sizeKey = isAudio ? 'audioFileSize' : 'videoFileSize'; |
| 392 | |
| 393 | if (existing[pathKey]) { |
| 394 | if (!duplicates.has(youtubeId)) { |
| 395 | duplicates.set(youtubeId, []); |
| 396 | } |
| 397 | duplicates.get(youtubeId).push(fullPath); |
| 398 | |
| 399 | if (stats.size > existing[sizeKey]) { |
| 400 | logger.warn( |
| 401 | { youtubeId, filePath: fullPath, size: stats.size, type: ext }, |
| 402 | 'Duplicate found: keeping larger file' |
| 403 | ); |
| 404 | existing[pathKey] = fullPath; |
| 405 | existing[sizeKey] = stats.size; |
no outgoing calls
no test coverage detected