1
0
Fork 0
CalMerger/server.js

150 lines
5.3 KiB
JavaScript

import express from 'express';
import ICAL from 'ical.js';
import fs from 'fs';
import axios from 'axios';
import path from 'path';
import icalGenerator from 'ical-generator';
const app = express();
app.use(express.json());
const MERGED_CALENDARS_DIR = path.join(process.cwd(), 'calendar');
console.log(`Merged calendars directory: ${MERGED_CALENDARS_DIR} under ${process.cwd()}`);
// Ensure the merged calendars directory exists
fs.mkdirSync(MERGED_CALENDARS_DIR, { recursive: true });
// Serve static files
app.get('/script.js', (req, res) => res.sendFile('script.js', { root: '.' }));
app.get('/', (req, res) => res.sendFile('index.html', { root: '.' }));
// Utility to sanitize filenames
const sanitizeFilename = (filename) => filename.replace(/[<>:"/\\|?* ]/g, '_');
// Fetch calendar data from URL or file
const fetchCalendarData = async (calendar) => {
const isFilePath = !calendar.url.startsWith('http');
try {
if (isFilePath) {
return {
data: fs.readFileSync(path.resolve(calendar.url), 'utf-8'),
...calendar
};
} else {
const response = await axios.get(calendar.url);
return { data: response.data, ...calendar };
}
} catch (error) {
console.error(`Error retrieving calendar from ${calendar.url}:`, error);
throw error;
}
};
// Merge calendar events
const mergeCalendarEvents = (calendarInstance, results) => {
results.forEach((result) => {
const parsed = ICAL.parse(result.data);
const component = new ICAL.Component(parsed);
component.getAllSubcomponents('vevent').forEach((event) => {
const vevent = new ICAL.Event(event);
const start = vevent.startDate.toJSDate();
const end = vevent.endDate.toJSDate();
const summary = result.override ? result.prefix : `${result.prefix} ${vevent.summary}`;
const eventOptions = {
start: start,
end: end,
summary: summary,
allDay: vevent.startDate.isDate
};
calendarInstance.createEvent(eventOptions);
});
});
};
// Save calendar data to file
const saveCalendarFile = (filename, content) => {
const filePath = path.join(MERGED_CALENDARS_DIR, filename);
fs.writeFileSync(filePath, content);
return filePath;
};
// Merge calendars endpoint
app.post('/merge', async (req, res) => {
const { linkGroupName, calendars } = req.body;
// Validate the input
if (!linkGroupName || !Array.isArray(calendars) || calendars.length === 0) {
return res.status(400).json({ error: 'Invalid input. Please provide a linkGroupName and at least one calendar.' });
}
try {
// Sanitize the linkGroupName to create a valid filename
const sanitizedLinkGroupName = sanitizeFilename(linkGroupName);
const filename = `${sanitizedLinkGroupName}.ics`;
// Fetch calendar data
const results = await Promise.all(calendars.map(fetchCalendarData));
// Generate merged calendar
const calendarInstance = icalGenerator({ name: linkGroupName });
mergeCalendarEvents(calendarInstance, results);
// Save the calendar to a file
saveCalendarFile(filename, calendarInstance.toString());
// Save the user input and sanitizedLinkGroupName in a separate JSON file
saveCalendarFile(`${sanitizedLinkGroupName}.json`, JSON.stringify({ linkGroupName, calendars }, null, 2));
res.json({ url: `${req.protocol}://${req.get('host')}/calendar/${sanitizedLinkGroupName}` });
} catch (error) {
console.error('Error merging calendars:', error);
res.status(500).json({ error: 'Failed to merge calendars' });
}
});
// Refresh calendar if outdated
const refreshCalendarData = async (calendarName) => {
const jsonFilePath = path.join(MERGED_CALENDARS_DIR, `${calendarName}.json`);
// Read the JSON file to get the source URL and other details
const { calendars } = JSON.parse(fs.readFileSync(jsonFilePath, 'utf-8'));
const results = await Promise.all(calendars.map(fetchCalendarData));
const calendarInstance = icalGenerator({ name: calendarName });
mergeCalendarEvents(calendarInstance, results);
saveCalendarFile(`${calendarName}.ics`, calendarInstance.toString());
console.log('Calendar data refreshed.');
};
// Serve the merged calendar file and refresh if older than an hour
app.get('/calendar/:name', async (req, res) => {
const calendarName = req.params.name;
const icsFilePath = path.join(MERGED_CALENDARS_DIR, `${calendarName}.ics`);
try {
// Check if the .ics file exists
if (fs.existsSync(icsFilePath)) {
const stats = fs.statSync(icsFilePath);
const isOutdated = new Date() - new Date(stats.mtime) > 60 * 60 * 1000;
if (isOutdated) await refreshCalendarData(calendarName);
res.setHeader('Content-Type', 'text/calendar');
res.sendFile(icsFilePath);
} else {
res.status(404).json({ error: 'Calendar not found.' });
}
} catch (error) {
console.error('Error retrieving calendar data:', error);
res.status(500).json({ error: 'Failed to retrieve calendar data.' });
}
});
export default app;