import fs from 'fs'; import path from 'path'; import { promisify } from 'util'; export async function cacheVideos() { const stat = promisify(fs.stat); const copyFile = promisify(fs.copyFile); const sourceFolder = './public/api/assets'; const videoOutputFolder = './.output/public/videos'; const videoCacheFolder = './public/videos'; // Cache folder for videos // Ensure the video output and cache folders exist if (!fs.existsSync(videoOutputFolder)) fs.mkdirSync(videoOutputFolder, { recursive: true }); if (!fs.existsSync(videoCacheFolder)) fs.mkdirSync(videoCacheFolder, { recursive: true }); const files = fs.readdirSync(sourceFolder); for (const file of files) { const filePath = `${sourceFolder}/${file}`; if (file.slice(-3) === "mp4") { // Handle videos (cache and copy to video output folder) const cachedVideoFile = path.join(videoCacheFolder, file); const videoDestination = path.join(videoOutputFolder, file); try { await stat(cachedVideoFile); // Check if video is already cached } catch (error) { if (error.code === 'ENOENT') { // Cache the video if it's not already cached await copyFile(filePath, cachedVideoFile); } else { throw error; } } try { await stat(videoDestination); // Check if video is already copied to output } catch (error) { if (error.code === 'ENOENT') { // Copy cached video to output folder await copyFile(cachedVideoFile, videoDestination); } else { throw error; } } } } console.log('Videos cached and copied successfully.'); }