136 lines
4.3 KiB
JavaScript
136 lines
4.3 KiB
JavaScript
import { promises as fs } from 'fs';
|
|
import path from 'path';
|
|
import http from 'http';
|
|
import { fileURLToPath } from 'url';
|
|
|
|
// Get current directory name in ES modules
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
|
|
async function generateSite() {
|
|
// Read the images data
|
|
const imagesData = JSON.parse(
|
|
await fs.readFile('images.json', 'utf-8')
|
|
);
|
|
|
|
console.log(imagesData);
|
|
|
|
// Create output directory if it doesn't exist
|
|
await fs.mkdir('public', { recursive: true });
|
|
|
|
// Copy the CSS file to public directory
|
|
await fs.copyFile('public/styles.css', 'public/styles.css').catch(err => {
|
|
console.error('Error copying CSS file:', err);
|
|
});
|
|
|
|
// Copy the favicon
|
|
await fs.copyFile('public/AnalogCameraS.png', 'public/favicon.png').catch(err => {
|
|
console.error('Error copying favicon:', err);
|
|
});
|
|
|
|
// Generate individual pages for each image
|
|
for (let i = 0; i < imagesData.length; i++) {
|
|
const image = imagesData[i];
|
|
const prevImage = imagesData[i > 0 ? i - 1 : imagesData.length - 1];
|
|
const nextImage = imagesData[(i + 1) % imagesData.length];
|
|
|
|
// Use image ID for filename
|
|
const fileName = `${image.id}.html`;
|
|
const prevFileName = `${prevImage.id}.html`;
|
|
const nextFileName = `${nextImage.id}.html`;
|
|
|
|
const html = `
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>${image.description || `Image ${i + 1}`}</title>
|
|
<link rel="stylesheet" href="/styles.css">
|
|
<link rel="icon" type="image/png" href="/favicon.png">
|
|
</head>
|
|
<body>
|
|
<div class="image-container">
|
|
${image?.localPath || 'No image URL available'
|
|
? `<img src="${image?.localPath || 'No image URL available'}" alt="${image.metadata.description || ''}">`
|
|
: '<p>Image not available</p>'
|
|
}
|
|
${image.description ? `<p class="description">${image.description}</p>` : ''}
|
|
<div class="navigation">
|
|
<a href="${prevFileName}">←</a>
|
|
<a href="${nextFileName}">→</a>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>`;
|
|
|
|
await fs.writeFile(`public/${fileName}`, html);
|
|
}
|
|
|
|
// Update index.html to redirect to first image's ID
|
|
const indexHtml = `
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="refresh" content="0; url=${imagesData[0].id}.html">
|
|
</head>
|
|
<body>
|
|
Redirecting...
|
|
</body>
|
|
</html>`;
|
|
|
|
await fs.writeFile('public/index.html', indexHtml);
|
|
|
|
console.log('Site generated successfully!');
|
|
}
|
|
|
|
async function serveStaticSite(port = 3000) {
|
|
const server = http.createServer(async (req, res) => {
|
|
try {
|
|
// Convert URL to filesystem path
|
|
let filePath;
|
|
if (req.url.startsWith('/images/')) {
|
|
// Serve directly from images folder
|
|
filePath = req.url.slice(1); // Remove leading slash
|
|
} else {
|
|
// Serve from public folder
|
|
filePath = path.join('public', req.url === '/' ? 'index.html' : req.url);
|
|
|
|
// Add .html extension if no extension exists
|
|
if (!path.extname(filePath)) {
|
|
filePath += '.html';
|
|
}
|
|
}
|
|
|
|
const content = await fs.readFile(filePath);
|
|
|
|
// Set content type based on file extension
|
|
const ext = path.extname(filePath).toLowerCase();
|
|
const contentType = {
|
|
'.html': 'text/html',
|
|
'.css': 'text/css',
|
|
'.jpg': 'image/jpeg',
|
|
'.jpeg': 'image/jpeg',
|
|
'.png': 'image/png',
|
|
'.gif': 'image/gif',
|
|
}[ext] || 'application/octet-stream';
|
|
|
|
res.writeHead(200, { 'Content-Type': contentType });
|
|
res.end(content);
|
|
} catch (error) {
|
|
console.error('Error serving file:', error);
|
|
res.writeHead(404);
|
|
res.end('Not found');
|
|
}
|
|
});
|
|
|
|
server.listen(port, () => {
|
|
console.log(`Server running at http://localhost:${port}/`);
|
|
});
|
|
}
|
|
|
|
// Generate the site and then serve it
|
|
generateSite()
|
|
.then(() => serveStaticSite())
|
|
.catch(console.error);
|
|
|