Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions ImmichFrame.Core/Services/IcalCalendarService.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
using System.Net.Http.Headers;
using System.Text;
using Ical.Net;
using ImmichFrame.Core.Interfaces;
using ImmichFrame.WebApi.Helpers;
using Microsoft.Extensions.Logging;

public class IcalCalendarService : ICalendarService
{
private readonly IServerSettings _serverSettings;
private readonly ILogger<IcalCalendarService> _logger;
private readonly ApiCache<List<IAppointment>> _appointmentCache = new(TimeSpan.FromMinutes(15));

public IcalCalendarService(IServerSettings serverSettings)
public IcalCalendarService(IServerSettings serverSettings, ILogger<IcalCalendarService> logger)
{
_logger = logger;
_serverSettings = serverSettings;
}

Expand All @@ -18,7 +23,9 @@ public async Task<List<IAppointment>> GetAppointments()
{
var appointments = new List<IAppointment>();

var icals = await GetCalendars(_serverSettings.Webcalendars);
List<(string? auth, string url)> cals = _serverSettings.Webcalendars.Select(x => x.Contains(';') ? (x.Split(';')[0], x.Split(';')[1]) : (null, x.ToString())).ToList();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest that you use the standard auth formatting in URLs: https://<user>:<pass>@host/path, rather that semicolon delimited.


var icals = await GetCalendars(cals);

foreach (var ical in icals)
{
Expand All @@ -31,29 +38,38 @@ public async Task<List<IAppointment>> GetAppointments()
});
}

public async Task<List<string>> GetCalendars(IEnumerable<string> calendars)
public async Task<List<string>> GetCalendars(IEnumerable<(string? auth, string url)> calendars)
{
var icals = new List<string>();

foreach (var webcal in calendars)
using (HttpClient client = new HttpClient())
{
string httpUrl = webcal.Replace("webcal://", "https://");

using (HttpClient client = new HttpClient())
foreach (var calendar in calendars)
{
_logger.LogDebug($"Loading calendar: {calendar.auth ?? "no auth"} - {calendar.url}");
client.DefaultRequestHeaders.Authorization = null;

string httpUrl = calendar.url.Replace("webcal://", "https://");

if (!string.IsNullOrEmpty(calendar.auth))
{
var byteArray = Encoding.ASCII.GetBytes($"{calendar.auth}");
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
}

HttpResponseMessage response = await client.GetAsync(httpUrl);
if (response.IsSuccessStatusCode)
{
icals.Add(await response.Content.ReadAsStringAsync());
}
else
{
throw new Exception("Failed to load calendar data");
_logger.LogError($"Failed to load calendar data from '{httpUrl}' (Status: {response.StatusCode})");
}
}
}

return icals;
}

}
9 changes: 1 addition & 8 deletions ImmichFrame.WebApi/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
{}
Loading