2024-11-14 13:46:27 +01:00
|
|
|
import ICAL from './lib/ical.timezones.js';
|
2024-11-11 19:34:53 +03:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
|
|
|
import axios from 'axios';
|
|
|
|
|
|
|
|
export const MERGED_CALENDARS_DIR = path.join(process.cwd(), 'calendar');
|
|
|
|
|
|
|
|
// Ensure the merged calendars directory exists
|
|
|
|
fs.mkdirSync(MERGED_CALENDARS_DIR, { recursive: true });
|
|
|
|
|
|
|
|
// Utility to sanitize filenames
|
|
|
|
export const sanitizeFilename = (filename) => filename.replace(/[<>:"/\\|?* ]/g, '_');
|
|
|
|
|
|
|
|
// Fetch calendar data from URL or file
|
|
|
|
export async function fetchCalendarData(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) {
|
|
|
|
throw new Error(`Error retrieving calendar from ${calendar.url}: ${error.message}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a top-level VCALENDAR component
|
|
|
|
export function createCalendarComponent(name) {
|
|
|
|
const calendarComponent = new ICAL.Component(['vcalendar', [], []]);
|
|
|
|
calendarComponent.updatePropertyWithValue('name', name);
|
2024-11-12 23:23:59 +03:00
|
|
|
calendarComponent.updatePropertyWithValue('version', '2.0');
|
2024-11-11 19:34:53 +03:00
|
|
|
return calendarComponent;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add events to the calendar component
|
|
|
|
export function addEventsToCalendar(calendarComponent, results) {
|
|
|
|
results.forEach((result) => {
|
2024-11-12 22:39:39 +03:00
|
|
|
try {
|
|
|
|
const parsed = ICAL.parse(result.data);
|
|
|
|
const component = new ICAL.Component(parsed);
|
2024-11-11 19:34:53 +03:00
|
|
|
|
2024-11-18 23:40:41 +03:00
|
|
|
// Extract and add VTIMEZONE components
|
|
|
|
const timezones = component.getAllSubcomponents('vtimezone');
|
|
|
|
timezones.forEach((timezone) => {
|
|
|
|
const tzid = timezone.getFirstPropertyValue('tzid');
|
|
|
|
if (!calendarComponent.getFirstSubcomponent((comp) => comp.name === 'vtimezone' && comp.getFirstPropertyValue('tzid') === tzid)) {
|
|
|
|
calendarComponent.addSubcomponent(timezone);
|
|
|
|
}
|
|
|
|
});
|
2024-11-18 23:49:50 +03:00
|
|
|
// Process VEVENT components
|
|
|
|
component.getAllSubcomponents('vevent').forEach((event) => {
|
|
|
|
const vevent = new ICAL.Event(event);
|
|
|
|
const newEvent = new ICAL.Component('vevent');
|
2024-11-18 23:40:41 +03:00
|
|
|
|
2024-11-18 23:49:50 +03:00
|
|
|
// Copy UID
|
|
|
|
newEvent.updatePropertyWithValue('uid', vevent.uid);
|
|
|
|
|
|
|
|
// Copy DTSTAMP
|
|
|
|
const dtstamp = event.getFirstPropertyValue('dtstamp');
|
|
|
|
if (dtstamp) newEvent.updatePropertyWithValue('dtstamp', dtstamp);
|
|
|
|
|
|
|
|
// Copy SUMMARY
|
|
|
|
newEvent.updatePropertyWithValue('summary', vevent.summary.trim());
|
|
|
|
|
|
|
|
// Copy DTSTART and DTEND
|
|
|
|
newEvent.updatePropertyWithValue('dtstart', vevent.startDate);
|
|
|
|
newEvent.updatePropertyWithValue('dtend', vevent.endDate);
|
|
|
|
|
|
|
|
// Copy Recurrence Rules (RRULE) and Recurrence ID
|
|
|
|
const rrule = event.getFirstPropertyValue('rrule');
|
|
|
|
if (rrule) newEvent.updatePropertyWithValue('rrule', rrule);
|
|
|
|
|
|
|
|
const recurrenceId = event.getFirstPropertyValue('recurrence-id');
|
|
|
|
if (recurrenceId) newEvent.updatePropertyWithValue('recurrence-id', recurrenceId);
|
|
|
|
|
|
|
|
// Add the VEVENT to the calendar
|
|
|
|
calendarComponent.addSubcomponent(newEvent);
|
|
|
|
});
|
|
|
|
|
|
|
|
console.log(`Processed VEVENT components for calendar: ${result.name}`);
|
|
|
|
} catch (error) {
|
2024-11-12 22:39:39 +03:00
|
|
|
console.error('Error processing calendar data:', error.message);
|
|
|
|
}
|
2024-11-11 19:34:53 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save calendar data to file
|
|
|
|
export function saveCalendarFile(filename, content) {
|
|
|
|
const filePath = path.join(MERGED_CALENDARS_DIR, filename);
|
2024-11-18 23:40:41 +03:00
|
|
|
// console.log(`Saving calendar data to file: ${filePath}`);
|
2024-11-11 19:34:53 +03:00
|
|
|
fs.writeFileSync(filePath, content);
|
|
|
|
return filePath;
|
|
|
|
}
|