Skip to content

Commit

Permalink
fixed: ms-ics-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
scolastico committed Apr 25, 2024
1 parent 35a303f commit 72c7cba
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 22 deletions.
18 changes: 8 additions & 10 deletions src/ms-ics-fix/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ Key problem here is, that they are exporting ICS files, which are not RFC compli

```yml
# ...
TZID:UTC # Server indicates its using UTC
TZID:UTC # Server indicates its using UTC
# ...
TZID:Central Europe Standard Time # Suddenly it indicates the correct timezone
# ...
BEGIN:VEVENT
# ...
DTSTAMP:20240425T132325Z # Server using a local timezone
# ... and not using the UTC-OFFSET value
# ... as it should to indicate the timezone
DTSTAMP:20240101T130000Z # Server using UTC, tho it said it uses CEST
# ...
END:VEVENT
# ...
```
Expand All @@ -58,18 +59,17 @@ To fix this, this tool will work like an ICS proxy, which will fix the timezone
|-----------------------------|---------|---------------|---------------------------------------------------------|
| `ICS_ON_DEMAND` | boolean | `false` | Whether to enable the on demand mode. |
| `ICS_{counter}_URL` | string | `null` | The URL of the ICS file to serve. |
| `ICS_{counter}_TZID` | string | `null` | The timezone of the ICS file. |
| `ICS_{counter}_PATH` | string? | `null` | The path to serve the ICS file. |

## On Demand Mode

If `ICS_ON_DEMAND` is set to `true`, you can make a GET request to `/` with the query parameter `url` and `tzid`.
If `ICS_ON_DEMAND` is set to `true`, you can make a GET request to `/` with the query parameter `url`.

```http
/?url=https%3A%2F%2Fexample.com%2Fcalendar.ics&tzid=Europe%2FBerlin
/?url=https%3A%2F%2Fexample.com%2Fcalendar.ics
```

This will return the ICS file from `https://example.com/calendar.ics` with the timezone `Europe/Berlin`.
This will return the ICS file from `https://example.com/calendar.ics`.

**Don't forget to URL encode the query parameters.**

Expand All @@ -84,10 +84,8 @@ services:
- "3000:3000"
environment:
ICS_0_URL: "https://example.com/calendar1.ics"
ICS_0_TZID: "Europe/Berlin"
ICS_0_PATH: "your-new-filename-which-will-be-served.ics"
ICS_1_URL: "https://example.com/calendar2.ics"
ICS_1_TZID: "Europe/Berlin"
# ...
```

Expand Down
15 changes: 3 additions & 12 deletions src/ms-ics-fix/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ let counter = 0
while (process.env[`ICS_${counter}_URL`]) {
config.ics.push({
url: process.env[`ICS_${counter}_URL`],
tz: process.env[`ICS_${counter}_TZID`],
path: process.env[`ICS_${counter}_PATH`],
})
counter++
Expand All @@ -24,7 +23,7 @@ config.ics = (() => {
const n = []
const s = new Set()
for (const ics of config.ics) {
if (!ics.url || !ics.tz) continue // Skip invalid ICS
if (!ics.url) continue // Skip invalid ICS
if (s.has(ics.path)) ics.path = null // Reset path if duplicated
let c = -1
if (!ics.path) do { // Generate path if not provided
Expand All @@ -38,17 +37,9 @@ config.ics = (() => {
})();

// The little magic, microsoft is too lazy to implement...
async function doIcsFix(url, tz) {
async function doIcsFix(url) {
const ics = await fetch(url).then(res => res.text())
return ics.replace(/^TZID:.*$/gm, `TZID:UTC`).split('\n').map(line => {
// Translate DTSTART and DTEND from the specified timezone to UTC
if (!line.startsWith('DTSTART') && !line.startsWith('DTEND')) return line
const date = line.split(':')[1]
const dt = new Date(date)
const dtUTC = new Date(dt.toLocaleString('en-US', { timeZone: tz }))
return `${line.split(':')[0]}:${dtUTC.toISOString().replace(/[-:]/g, '').replace(/\.\d+/, '')}`
return line
}).join('\n')
return ics.replace(/^TZID:.*$/gm, `TZID:UTC`)
}

app.get('*', (req, res) => {
Expand Down

0 comments on commit 72c7cba

Please sign in to comment.