feat: Implement image fetching and storage system
Some checks failed
Deploy to BeePC / deploy (push) Has been cancelled
Some checks failed
Deploy to BeePC / deploy (push) Has been cancelled
- Add image-fetcher module for downloading and saving images from various sources. - Create storage module for managing image files, including downloading, verifying integrity, and cleaning up orphaned files. - Develop gallery HTML page for displaying images with sorting and filtering options. - Set up RESTful API routes for image management, including fetching, adding tags, and deleting images. - Introduce setup script for initializing the database and configuring image sources. - Implement a batch process for verifying image integrity and cleaning up old images. - Add setup batch script for easy installation and configuration of the image storage system.
This commit is contained in:
87
server.js
87
server.js
@@ -1,30 +1,103 @@
|
||||
const express = require('express');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const database = require('./lib/database');
|
||||
const imageFetcher = require('./lib/image-fetcher');
|
||||
const imagesRouter = require('./routes/images');
|
||||
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3001;
|
||||
const DOMAIN = process.env.DOMAIN || 'homebase.sketchferret.com';
|
||||
|
||||
// Serve static files from public folder
|
||||
// Middleware
|
||||
app.use(express.json());
|
||||
app.use(express.static(path.join(__dirname, 'public')));
|
||||
|
||||
// API endpoint
|
||||
// Initialize database
|
||||
database.initializeDatabase().catch(err => {
|
||||
console.error('Failed to initialize database:', err);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
// API Routes
|
||||
app.use('/api', imagesRouter);
|
||||
|
||||
// Status endpoint
|
||||
app.get('/api/status', (req, res) => {
|
||||
res.json({
|
||||
message: 'HomeBase is running!',
|
||||
domain: DOMAIN,
|
||||
timestamp: new Date().toISOString(),
|
||||
version: '1.0.0'
|
||||
version: '1.0.0',
|
||||
features: {
|
||||
imageStorage: true,
|
||||
tagging: true,
|
||||
corruptionDetection: true
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Health check
|
||||
app.get('/health', (req, res) => {
|
||||
res.json({ status: 'healthy' });
|
||||
});
|
||||
|
||||
app.listen(PORT, '0.0.0.0', () => {
|
||||
console.log(`Server running on port ${PORT}`);
|
||||
console.log(`Domain: ${DOMAIN}`);
|
||||
console.log(`Visit: http://${DOMAIN}`);
|
||||
// Start server
|
||||
const server = app.listen(PORT, '0.0.0.0', () => {
|
||||
console.log(`\n🏠 HomeBase Server Started`);
|
||||
console.log(` Port: ${PORT}`);
|
||||
console.log(` Domain: ${DOMAIN}`);
|
||||
console.log(` URL: http://${DOMAIN}\n`);
|
||||
|
||||
// Load configuration from image-sources.json
|
||||
const configPath = path.join(__dirname, 'image-sources.json');
|
||||
let config = { sources: [], fetchInterval: 2.5 };
|
||||
|
||||
try {
|
||||
if (fs.existsSync(configPath)) {
|
||||
const fileContent = fs.readFileSync(configPath, 'utf8');
|
||||
config = JSON.parse(fileContent);
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn('⚠️ Could not read image-sources.json:', err.message);
|
||||
}
|
||||
|
||||
const imageSources = config.sources || [];
|
||||
const fetchInterval = config.fetchInterval || 2.5;
|
||||
|
||||
// Start image fetcher if sources are configured
|
||||
if (imageSources.length > 0) {
|
||||
const enabledSources = imageSources.filter(s => s.enabled);
|
||||
if (enabledSources.length > 0) {
|
||||
imageFetcher.startFetcher(enabledSources, fetchInterval);
|
||||
console.log(`📸 Image fetcher: Enabled (${fetchInterval} minute interval)\n`);
|
||||
} else {
|
||||
console.log('📸 Image fetcher: Disabled (no enabled sources in config)');
|
||||
console.log(' Edit image-sources.json and set "enabled": true to enable\n');
|
||||
}
|
||||
} else {
|
||||
console.log('📸 Image fetcher: No sources configured');
|
||||
console.log(' Edit image-sources.json to add image sources\n');
|
||||
}
|
||||
});
|
||||
|
||||
// Graceful shutdown
|
||||
process.on('SIGTERM', () => {
|
||||
console.log('\n📴 Shutting down gracefully...');
|
||||
imageFetcher.stopFetcher();
|
||||
server.close(async () => {
|
||||
await database.closeDatabase();
|
||||
process.exit(0);
|
||||
});
|
||||
});
|
||||
|
||||
process.on('SIGINT', () => {
|
||||
console.log('\n📴 Shutting down gracefully...');
|
||||
imageFetcher.stopFetcher();
|
||||
server.close(async () => {
|
||||
await database.closeDatabase();
|
||||
process.exit(0);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user