refactor: centralize calendar data retrieval with fetchCalendarData helper

This commit is contained in:
Ryan Mwangi 2024-11-07 14:20:56 +03:00
parent af74d809c4
commit 0c61cb0e7c
1 changed files with 19 additions and 0 deletions

View File

@ -21,6 +21,25 @@ app.get('/', (req, res) => res.sendFile('index.html', { root: '.' }));
// Utility to sanitize filenames // Utility to sanitize filenames
const sanitizeFilename = (filename) => filename.replace(/[<>:"/\\|?* ]/g, '_'); 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 calendars endpoint // Merge calendars endpoint
app.post('/merge', async (req, res) => { app.post('/merge', async (req, res) => {
const { linkGroupName, calendars } = req.body; const { linkGroupName, calendars } = req.body;