* Recover completed videos from JobVideoDownload tracking table * Reads .info.json files and ensures videos are properly saved to DB * @param {string} jobId - The job ID to recover videos for * @returns {Promise } Number of videos successfully recovered
(jobId)
| 72 | * @returns {Promise<number>} Number of videos successfully recovered |
| 73 | */ |
| 74 | async recoverCompletedVideos(jobId) { |
| 75 | let recoveredCount = 0; |
| 76 | |
| 77 | try { |
| 78 | // Find all completed video downloads for this job |
| 79 | const completedDownloads = await JobVideoDownload.findAll({ |
| 80 | where: { |
| 81 | job_id: jobId, |
| 82 | status: 'completed' |
| 83 | } |
| 84 | }); |
| 85 | |
| 86 | if (completedDownloads.length === 0) { |
| 87 | logger.info({ jobId }, 'No completed videos to recover for job'); |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | logger.info({ jobId, count: completedDownloads.length }, 'Found completed videos to recover for job'); |
| 92 | |
| 93 | // Try to get the job instance for creating JobVideo relationships |
| 94 | let jobInstance = await Job.findOne({ where: { id: jobId } }); |
| 95 | |
| 96 | for (const download of completedDownloads) { |
| 97 | try { |
| 98 | const youtubeId = download.youtube_id; |
| 99 | const infoJsonPath = path.join(this.jobsDir, 'info', `${youtubeId}.info.json`); |
| 100 | |
| 101 | // Check if .info.json file exists |
| 102 | let infoExists = false; |
| 103 | try { |
| 104 | await fsPromises.access(infoJsonPath); |
| 105 | infoExists = true; |
| 106 | } catch (err) { |
| 107 | logger.warn({ youtubeId }, 'Info file not found for video, skipping recovery'); |
| 108 | continue; |
| 109 | } |
| 110 | |
| 111 | if (!infoExists) { |
| 112 | continue; |
| 113 | } |
| 114 | |
| 115 | // Read and parse the info.json file |
| 116 | const infoContent = await fsPromises.readFile(infoJsonPath, 'utf-8'); |
| 117 | const info = JSON.parse(infoContent); |
| 118 | |
| 119 | // Build video data object from info.json |
| 120 | const preferredChannelName = info.uploader || info.channel || info.uploader_id || info.channel_id || 'Unknown Channel'; |
| 121 | |
| 122 | const videoData = { |
| 123 | youtubeId: info.id, |
| 124 | youTubeChannelName: preferredChannelName, |
| 125 | youTubeVideoName: info.title, |
| 126 | duration: info.duration, |
| 127 | description: info.description, |
| 128 | originalDate: info.upload_date, |
| 129 | channel_id: info.channel_id, |
| 130 | media_type: info.media_type || 'video', |
| 131 | content_rating: info.content_rating || null, |
no test coverage detected