diff --git a/Modules/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift b/Modules/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift index 0f55c5d2b..2179a61d7 100644 --- a/Modules/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift +++ b/Modules/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift @@ -1153,14 +1153,14 @@ private extension ReaderAPIAccountDelegate { contentText: nil, markdown: nil, summary: entry.summary.content, - imageURL: nil, + imageURL: entry.imageURL, thumbnailURL: nil, bannerImageURL: nil, datePublished: entry.parseDatePublished(), dateModified: nil, authors: authors, tags: nil, - attachments: nil) + attachments: entry.attachments) } return Set(parsedItems) diff --git a/Modules/Account/Sources/Account/ReaderAPI/ReaderAPIEntry.swift b/Modules/Account/Sources/Account/ReaderAPI/ReaderAPIEntry.swift index c7d423653..96cd74920 100644 --- a/Modules/Account/Sources/Account/ReaderAPI/ReaderAPIEntry.swift +++ b/Modules/Account/Sources/Account/ReaderAPI/ReaderAPIEntry.swift @@ -59,6 +59,16 @@ struct ReaderAPIEntry: Codable, Sendable { let alternates: [ReaderAPIAlternateLocation]? let categories: [String] let origin: ReaderAPIEntryOrigin + let enclosures: [ReaderAPIEnclosure]? + + var attachments: Set? { + let attachments = enclosures?.compactMap { $0.parsedAttachment } ?? [] + return attachments.isEmpty ? nil : Set(attachments) + } + + var imageURL: String? { + enclosures?.first { $0.isImage }?.url + } enum CodingKeys: String, CodingKey { case articleID = "id" @@ -71,6 +81,7 @@ struct ReaderAPIEntry: Codable, Sendable { case crawledTimestamp = "crawlTimeMsec" case origin = "origin" case timestampUsec = "timestampUsec" + case enclosures = "enclosure" } func parseDatePublished() -> Date? { @@ -120,6 +131,67 @@ struct ReaderAPIAlternateLocation: Codable, Sendable { } } +/// `type` is a MIME type, or a bare `image`, `audio`, or `video` (FreshRSS). +struct ReaderAPIEnclosure: Codable, Sendable { + let url: String? + let type: String? + let length: Int? + + var parsedAttachment: ParsedAttachment? { + guard let url else { + return nil + } + return ParsedAttachment(url: url, mimeType: mimeType, title: nil, sizeInBytes: sizeInBytes, durationInSeconds: nil, width: nil, height: nil, medium: medium) + } + + var isImage: Bool { + medium == .image || mimeType?.lowercased().hasPrefix("image/") == true + } + + enum CodingKeys: String, CodingKey { + case url = "href" + case type + case length + } + + init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + url = try? container.decodeIfPresent(String.self, forKey: .url) + type = try? container.decodeIfPresent(String.self, forKey: .type) + if let length = try? container.decodeIfPresent(Int.self, forKey: .length) { + self.length = length + } else if let lengthString = try? container.decodeIfPresent(String.self, forKey: .length) { + self.length = Int(lengthString) + } else { + self.length = nil + } + } +} + +private extension ReaderAPIEnclosure { + + var sizeInBytes: Int? { + guard let length, length > 0 else { + return nil + } + return length + } + + var mimeType: String? { + guard let type, type.contains("/") else { + return nil + } + return type + } + + var medium: ParsedAttachment.Medium? { + guard mimeType == nil else { + return nil + } + return ParsedAttachment.Medium(mediaRSSValue: type) + } +} + struct ReaderAPIEntryOrigin: Codable, Sendable { let streamId: String? let title: String? diff --git a/Modules/Account/Tests/AccountTests/ReaderAPI/ReaderAPIEntryTests.swift b/Modules/Account/Tests/AccountTests/ReaderAPI/ReaderAPIEntryTests.swift index 19a98a44c..cf8aff5b4 100644 --- a/Modules/Account/Tests/AccountTests/ReaderAPI/ReaderAPIEntryTests.swift +++ b/Modules/Account/Tests/AccountTests/ReaderAPI/ReaderAPIEntryTests.swift @@ -50,6 +50,33 @@ struct ReaderAPIEntryTests { } } + @Test func enclosures() throws { + // From FreshRSS’s toGReader: a MIME type, or a bare medium. + let json = """ + { + "id": "tag:google.com,2005:reader/item/00058b10ce338909", + "summary": {}, + "categories": [], + "origin": {}, + "enclosure": [ + {"href": "https://example.com/episode.mp3", "type": "audio/mpeg", "length": 54947132}, + {"href": "https://example.com/cover.jpg", "type": "image"} + ] + } + """ + let entry = try JSONDecoder().decode(ReaderAPIEntry.self, from: Data(json.utf8)) + + #expect(entry.imageURL == "https://example.com/cover.jpg") + + let attachments = try #require(entry.attachments) + let audio = try #require(attachments.first { $0.url == "https://example.com/episode.mp3" }) + #expect(audio.mimeType == "audio/mpeg") + #expect(audio.sizeInBytes == 54947132) + let image = try #require(attachments.first { $0.url == "https://example.com/cover.jpg" }) + #expect(image.mimeType == nil) + #expect(image.medium == .image) + } + private func makeEntry(articleID: String) throws -> ReaderAPIEntry { let json = "{\"id\":\"\(articleID)\",\"summary\":{},\"categories\":[],\"origin\":{}}" return try JSONDecoder().decode(ReaderAPIEntry.self, from: Data(json.utf8)) diff --git a/Modules/RSParser/Sources/RSParser/Feeds/XML/MediaRSS.swift b/Modules/RSParser/Sources/RSParser/Feeds/XML/MediaRSS.swift index 2aaae70b6..0ab93b9e5 100644 --- a/Modules/RSParser/Sources/RSParser/Feeds/XML/MediaRSS.swift +++ b/Modules/RSParser/Sources/RSParser/Feeds/XML/MediaRSS.swift @@ -130,7 +130,7 @@ private struct MediaGroup { extension ParsedAttachment.Medium { /// Case-insensitive. Nil for values the Media RSS spec doesn’t define. - init?(mediaRSSValue: String?) { + public init?(mediaRSSValue: String?) { guard let mediaRSSValue else { return nil }