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-19 15:24:16 +03:00
|
|
|
calendarComponent.updatePropertyWithValue('prodid', '-//CalMerge//Calendar Merger 1.0//EN');
|
2024-11-12 23:23:59 +03:00
|
|
|
calendarComponent.updatePropertyWithValue('version', '2.0');
|
2024-11-19 14:03:42 +03:00
|
|
|
calendarComponent.updatePropertyWithValue('calscale', 'GREGORIAN');
|
2024-11-11 19:34:53 +03:00
|
|
|
return calendarComponent;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add events to the calendar component
|
2024-11-19 13:21:22 +03:00
|
|
|
export function addEventsToCalendar(calendarComponent, results, overrideFlag = false) {
|
2024-11-19 19:03:06 +03:00
|
|
|
let defaultTimeZone = null; // To store the first found X-WR-TIMEZONE
|
|
|
|
|
2024-11-11 19:34:53 +03:00
|
|
|
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-19 19:03:06 +03:00
|
|
|
|
|
|
|
// Extract METHOD from the parsed data (if available)
|
|
|
|
const method = component.getFirstPropertyValue('method');
|
|
|
|
if (method) {
|
|
|
|
console.log(`Extracted METHOD: ${method}`);
|
|
|
|
// Only add the METHOD property once
|
|
|
|
if (!calendarComponent.getFirstPropertyValue('method')) {
|
|
|
|
calendarComponent.updatePropertyWithValue('method', method.toUpperCase());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Extract X-WR-TIMEZONE if available
|
|
|
|
const wrTimeZone = component.getFirstPropertyValue('x-wr-timezone');
|
|
|
|
if (wrTimeZone) {
|
|
|
|
console.log(`Extracted X-WR-TIMEZONE: ${wrTimeZone}`);
|
|
|
|
// Set it as the default if not already set
|
|
|
|
if (!defaultTimeZone) {
|
|
|
|
defaultTimeZone = wrTimeZone;
|
|
|
|
if (!calendarComponent.getFirstPropertyValue('x-wr-timezone')) {
|
|
|
|
calendarComponent.updatePropertyWithValue('x-wr-timezone', defaultTimeZone);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-19 15:59:23 +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-19 15:24:16 +03:00
|
|
|
// 1. DTSTART with time zone
|
|
|
|
if (vevent.startDate) {
|
|
|
|
const startTime = vevent.startDate.toString(); // Format start date properly
|
|
|
|
const dtstartProp = new ICAL.Property('dtstart', newEvent);
|
|
|
|
dtstartProp.setValue(startTime);
|
|
|
|
|
|
|
|
// Add TZID parameter if zone is present
|
|
|
|
if (vevent.startDate.zone) {
|
|
|
|
dtstartProp.setParameter('TZID', vevent.startDate.zone.tzid);
|
|
|
|
}
|
|
|
|
newEvent.addProperty(dtstartProp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. DTEND with time zone
|
2024-11-19 01:29:32 +03:00
|
|
|
if (vevent.endDate) {
|
|
|
|
const endTime = vevent.endDate.toString(); // Format end date properly
|
|
|
|
const dtendProp = new ICAL.Property('dtend', newEvent);
|
|
|
|
dtendProp.setValue(endTime);
|
|
|
|
|
|
|
|
// Add TZID parameter if zone is present
|
|
|
|
if (vevent.endDate.zone) {
|
|
|
|
dtendProp.setParameter('TZID', vevent.endDate.zone.tzid);
|
|
|
|
}
|
|
|
|
newEvent.addProperty(dtendProp);
|
|
|
|
}
|
2024-11-18 23:49:50 +03:00
|
|
|
|
2024-11-19 15:24:16 +03:00
|
|
|
// 3. Copy DTSTAMP
|
2024-11-18 23:49:50 +03:00
|
|
|
const dtstamp = event.getFirstPropertyValue('dtstamp');
|
|
|
|
if (dtstamp) newEvent.updatePropertyWithValue('dtstamp', dtstamp);
|
|
|
|
|
2024-11-19 15:24:16 +03:00
|
|
|
// 4. Copy UID
|
|
|
|
newEvent.updatePropertyWithValue('uid', vevent.uid);
|
2024-11-18 23:49:50 +03:00
|
|
|
|
2024-11-19 15:24:16 +03:00
|
|
|
// 5. Add LOCATION (conditionally included)
|
2024-11-19 13:21:22 +03:00
|
|
|
if (!overrideFlag && vevent.location) {
|
|
|
|
newEvent.updatePropertyWithValue('location', vevent.location);
|
|
|
|
} else if (overrideFlag && vevent.location) {
|
|
|
|
// Modify SUMMARY if override is set
|
|
|
|
const modifiedSummary = `${vevent.summary.trim()} (Location omitted)`;
|
|
|
|
newEvent.updatePropertyWithValue('summary', modifiedSummary);
|
|
|
|
} else {
|
|
|
|
newEvent.updatePropertyWithValue('summary', vevent.summary.trim());
|
|
|
|
}
|
2024-11-18 23:49:50 +03:00
|
|
|
|
2024-11-19 15:24:16 +03:00
|
|
|
// 6. Copy Recurrence Rules (RRULE) and Recurrence ID
|
2024-11-18 23:49:50 +03:00
|
|
|
const rrule = event.getFirstPropertyValue('rrule');
|
|
|
|
if (rrule) newEvent.updatePropertyWithValue('rrule', rrule);
|
|
|
|
|
|
|
|
const recurrenceId = event.getFirstPropertyValue('recurrence-id');
|
|
|
|
if (recurrenceId) newEvent.updatePropertyWithValue('recurrence-id', recurrenceId);
|
2024-11-19 15:24:16 +03:00
|
|
|
|
2024-11-19 14:03:42 +03:00
|
|
|
// 7. Copy SUMMARY
|
2024-11-19 01:29:32 +03:00
|
|
|
newEvent.updatePropertyWithValue('summary', vevent.summary.trim());
|
|
|
|
|
2024-11-19 15:24:16 +03:00
|
|
|
// 8. Add SEQUENCE (if available or default to 0)
|
|
|
|
const sequence = event.getFirstPropertyValue('sequence') || 0;
|
|
|
|
newEvent.updatePropertyWithValue('sequence', sequence);
|
|
|
|
|
2024-11-18 23:49:50 +03:00
|
|
|
// 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) {
|
2024-11-19 19:06:22 +03:00
|
|
|
const normalizedContent = content.replace(/\r?\n/g, '\r\n').trimEnd(); // Normalize to CRLF
|
2024-11-11 19:34:53 +03:00
|
|
|
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-19 14:17:42 +03:00
|
|
|
fs.writeFileSync(filePath, normalizedContent);
|
2024-11-11 19:34:53 +03:00
|
|
|
return filePath;
|
|
|
|
}
|