htdocs/node/addvideos.js

44 lines
1.3 KiB
JavaScript
Raw Permalink Normal View History

2024-10-31 14:52:17 +01:00
const fs = require('fs/promises');
async function readFilesAndPopulateVideos(path) {
try {
const files = await fs.readdir(path);
// Use Promise.all to wait for all fs.stat operations to complete
const statsPromises = files.map(async (file) => {
const filePath = `${path}/${file}`;
const stats = await fs.stat(filePath);
return [file, stats.mtime];
});
const videos = await Promise.all(statsPromises);
// Sort the videos array by modification time (most recent first)
videos.sort((a, b) => b[1] - a[1]);
console.log('Videos:', videos);
return videos;
} catch (err) {
console.error('Error reading directory:', err);
throw err;
}
}
// Call the function with await
(async () => {
try {
2024-11-02 02:03:32 +01:00
const videos = await readFilesAndPopulateVideos('/var/www/html/video/videos');
2024-10-31 14:52:17 +01:00
const videosJSFileContent = `let videos = ${JSON.stringify(videos)}
if (typeof module !== 'undefined' && module.exports) {
module.exports = videos;
}`;
2024-11-02 02:03:32 +01:00
await fs.writeFile("/var/www/html/video/videos.js", videosJSFileContent);
2024-10-31 14:52:17 +01:00
console.log('File has been written successfully.');
} catch (err) {
console.error('Error:', err);
}
})();