feat(smart url): add feature to try alternate url versions with and without .ics suffix

This commit is contained in:
ryan 2025-04-15 17:06:02 +03:00
parent 48548dadc8
commit 30622c611a

View file

@ -15,18 +15,33 @@ export const sanitizeFilename = (filename) => filename.replace(/[<>:"/\\|?* ]/g,
// Fetch calendar data from URL or file // Fetch calendar data from URL or file
export async function fetchCalendarData(calendar) { export async function fetchCalendarData(calendar) {
const isFilePath = !calendar.url.startsWith('http'); const isFilePath = !calendar.url.startsWith('http');
if (isFilePath) {
// logger.debug(`Reading calendar from file: ${calendar.url}`);
return { data: fs.readFileSync(path.resolve(calendar.url), 'utf-8'), ...calendar };
}
try { try {
if (isFilePath) { // First try the original URL
// logger.debug(`Reading calendar from file: ${calendar.url}`); const initialResponse = await axios.get(calendar.url);
return { data: fs.readFileSync(path.resolve(calendar.url), 'utf-8'), ...calendar }; return { data: initialResponse.data, ...calendar };
} else { } catch (initialError) {
// logger.debug(`Fetching calendar from URL: ${calendar.url}`); logger.debug(`Initial fetch failed, trying extension adjustment for: ${calendar.url}`);
const response = await axios.get(calendar.url);
return { data: response.data, ...calendar }; // Determine alternate URL version
const altUrl = calendar.url.endsWith('.ics')
? calendar.url.slice(0, -4) // Remove .ics
: calendar.url + '.ics'; // Add .ics
try {
// Try the alternate version
const altResponse = await axios.get(altUrl);
logger.debug(`Success with adjusted URL: ${altUrl}`);
return { data: altResponse.data, ...calendar };
} catch (altError) {
logger.error(`Both URL versions failed:
Original: ${calendar.url}
Adjusted: ${altUrl}`);
throw new Error(`Calendar fetch failed for both URL versions`);
} }
} catch (error) {
logger.error(`Error retrieving calendar from ${calendar.url}: ${error.message}`);
throw new Error(`Error retrieving calendar from ${calendar.url}: ${error.message}`);
} }
} }