Skip to content

Commit 297c351

Browse files
authored
Fixes notification image downloading (#1067)
* Fixes notification image downloading This is currently broken when using a remote connection to download notification images Signed-off-by: Dan Cunningham <dan@digitaldan.com> * Rework structure and waitForConnection logic Signed-off-by: Dan Cunningham <dan@digitaldan.com> --------- Signed-off-by: Dan Cunningham <dan@digitaldan.com>
1 parent 53c9cd0 commit 297c351

1 file changed

Lines changed: 20 additions & 36 deletions

File tree

NotificationService/NotificationService.swift

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ enum NotificationServiceError: Error {
4343
}
4444

4545
actor NotificationServiceHandler {
46-
static let networkTimeout: TimeInterval = 5
46+
static let networkTimeout: TimeInterval = 20 // don't go too long or we risk being terminated by the system
4747
var contentHandler: ((UNNotificationContent) -> Void)?
4848
var bestAttemptContent: UNMutableNotificationContent?
4949
var cancellables = Set<AnyCancellable>()
@@ -107,16 +107,20 @@ actor NotificationServiceHandler {
107107

108108
// check if there is an attachment to put on the notification
109109
// this should be last as we need to wait for media
110-
// TODO: we should support relative paths and try the user's openHAB (local,remote) for content
111110
if let attachmentURLString = userInfo["media-attachment-url"] as? String {
112-
// HERE we switch to async usage
113111
Task {
114112
do {
113+
guard let activeConfig = await networkTracker().waitForActiveConnection()?.configuration else {
114+
Logger.notificationService.error("No active connection available")
115+
throw NotificationServiceError.noActiveConnection
116+
}
117+
115118
let unNotificationAttachment = if attachmentURLString.starts(with: "item:") {
116119
try await downloadAndAttachItemImage(itemURI: attachmentURLString)
117120
} else {
118-
try await downloadAndAttachMedia(url: attachmentURLString)
121+
try await downloadAndAttachMedia(url: attachmentURLString, config: activeConfig)
119122
}
123+
120124
if let unNotificationAttachment {
121125
bestAttemptContent.attachments = [unNotificationAttachment]
122126
} else {
@@ -127,7 +131,6 @@ actor NotificationServiceHandler {
127131
}
128132
contentHandler(bestAttemptContent)
129133
}
130-
131134
} else {
132135
contentHandler(bestAttemptContent)
133136
}
@@ -163,47 +166,28 @@ actor NotificationServiceHandler {
163166
return nil
164167
}
165168

166-
private func downloadForAttachment(attachmentURLString: String) -> (URL?, String?) {
167-
var returnValues: (URL?, String?)
168-
Task {
169-
do {
170-
returnValues = if attachmentURLString.starts(with: "item:") {
171-
try await downloadItemImage(itemURI: attachmentURLString)
172-
} else {
173-
try await downloadMedia(url: attachmentURLString)
174-
}
175-
176-
} catch {
177-
Logger.notificationService.error("Error fetching data: \(error.localizedDescription)")
178-
}
179-
}
180-
return returnValues
181-
}
182-
183-
private func downloadAndAttachMedia(url: String) async throws -> UNNotificationAttachment? {
184-
let (localURL, mimeType) = try await downloadMedia(url: url)
169+
private func downloadAndAttachMedia(url: String, config: ConnectionConfiguration) async throws -> UNNotificationAttachment? {
170+
let (localURL, mimeType) = try await downloadMedia(url: url, config: config)
185171
guard let localURL else { return nil }
186172
return await attachFile(localURL: localURL, mimeType: mimeType)
187173
}
188174

189-
private func downloadMedia(url: String) async throws -> (URL?, String?) {
190-
guard let fullURL = await resolveFullURL(from: url) else { return (nil, nil) }
191-
192-
guard let activeConfig = await networkTracker().waitForActiveConnection()?.configuration else { return (nil, nil) }
193-
194-
let client = HTTPClient(connectionConfiguration: activeConfig)
195-
175+
private func downloadMedia(url: String, config: ConnectionConfiguration) async throws -> (URL?, String?) {
176+
// Resolve URL using the provided config
177+
guard let fullURL = resolveFullURL(from: url, baseURL: config.url) else {
178+
return (nil, nil)
179+
}
180+
let client = HTTPClient(connectionConfiguration: config)
196181
let (localURL, urlResponse) = try await client.downloadFile(url: fullURL)
197182
return (localURL, urlResponse.mimeType)
198183
}
199184

200-
// 🔹 Extracted helper function to determine full URL
201-
private func resolveFullURL(from url: String) async -> URL? {
185+
// Helper function to determine full URL
186+
private func resolveFullURL(from url: String, baseURL: String) -> URL? {
202187
if url.starts(with: "/") {
203-
guard let activeConfig = await networkTracker().waitForActiveConnection()?.configuration else { return nil }
204-
return URL(string: activeConfig.url)?.appendingPathComponent(url)
188+
URL(string: baseURL)?.appendingPathComponent(url)
205189
} else {
206-
return URL(string: url)
190+
URL(string: url)
207191
}
208192
}
209193

0 commit comments

Comments
 (0)