diff --git a/server/server.js b/server/server.js index b95c07d..ffe2d8c 100644 --- a/server/server.js +++ b/server/server.js @@ -649,6 +649,64 @@ async function downloadImage(url, targetPath) { } } +function guessPrimaryVideo(rootFolder) { + const safe = sanitizeRelative(rootFolder); + if (!safe) return null; + + const baseDir = path.join(DOWNLOAD_DIR, safe); + if (!fs.existsSync(baseDir)) return null; + + let bestRelPath = null; + let bestSize = 0; + + const stack = [""]; + while (stack.length) { + const currentRel = stack.pop(); + const currentDir = path.join(baseDir, currentRel); + let dirEntries = []; + try { + dirEntries = fs.readdirSync(currentDir, { withFileTypes: true }); + } catch (err) { + console.warn(`⚠️ Klasör okunamadı (${currentDir}): ${err.message}`); + continue; + } + + for (const entry of dirEntries) { + const name = entry.name; + if (name.startsWith(".")) continue; + if (name === INFO_FILENAME) continue; + + const relPath = path.join(currentRel, name); + const absPath = path.join(currentDir, name); + + if (entry.isDirectory()) { + stack.push(relPath); + continue; + } + + if (!entry.isFile()) continue; + + const ext = path.extname(name).toLowerCase(); + if (!VIDEO_EXTS.includes(ext)) continue; + + let size = 0; + try { + size = fs.statSync(absPath).size; + } catch (err) { + console.warn(`⚠️ Dosya boyutu alınamadı (${absPath}): ${err.message}`); + continue; + } + + if (size >= bestSize) { + bestSize = size; + bestRelPath = relPath; + } + } + } + + return bestRelPath ? bestRelPath.replace(/\\/g, "/") : null; +} + async function ensureMovieData( rootFolder, displayName,