diff --git a/Modules/Account/Sources/Account/Feedly/FeedlyModel.swift b/Modules/Account/Sources/Account/Feedly/FeedlyModel.swift index c7b072071..9ffe33b64 100644 --- a/Modules/Account/Sources/Account/Feedly/FeedlyModel.swift +++ b/Modules/Account/Sources/Account/Feedly/FeedlyModel.swift @@ -269,7 +269,7 @@ struct FeedlyEntryParser { guard let enclosure = entry.enclosure, !enclosure.isEmpty else { return nil } - let attachments = enclosure.compactMap { ParsedAttachment(url: $0.href, mimeType: $0.type, title: nil, sizeInBytes: nil, durationInSeconds: nil) } + let attachments = enclosure.compactMap { ParsedAttachment(url: $0.href, mimeType: $0.type, title: nil, sizeInBytes: nil, durationInSeconds: nil, width: nil, height: nil, medium: nil) } return attachments.isEmpty ? nil : Set(attachments) } diff --git a/Modules/RSParser/Sources/RSParser/Feeds/JSON/JSONFeedParser.swift b/Modules/RSParser/Sources/RSParser/Feeds/JSON/JSONFeedParser.swift index 7e04924b1..7d7f2a419 100644 --- a/Modules/RSParser/Sources/RSParser/Feeds/JSON/JSONFeedParser.swift +++ b/Modules/RSParser/Sources/RSParser/Feeds/JSON/JSONFeedParser.swift @@ -234,14 +234,12 @@ private extension JSONFeedParser { guard let url = attachmentObject[Key.url] as? String else { return nil } - guard let mimeType = attachmentObject[Key.mimeType] as? String else { - return nil - } + let mimeType = attachmentObject[Key.mimeType] as? String let title = attachmentObject[Key.title] as? String let sizeInBytes = attachmentObject[Key.sizeInBytes] as? Int let durationInSeconds = attachmentObject[Key.durationInSeconds] as? Int - return ParsedAttachment(url: url, mimeType: mimeType, title: title, sizeInBytes: sizeInBytes, durationInSeconds: durationInSeconds) + return ParsedAttachment(url: url, mimeType: mimeType, title: title, sizeInBytes: sizeInBytes, durationInSeconds: durationInSeconds, width: nil, height: nil, medium: nil) } } diff --git a/Modules/RSParser/Sources/RSParser/Feeds/JSON/RSSInJSONParser.swift b/Modules/RSParser/Sources/RSParser/Feeds/JSON/RSSInJSONParser.swift index 19b995ee4..392dca5cc 100644 --- a/Modules/RSParser/Sources/RSParser/Feeds/JSON/RSSInJSONParser.swift +++ b/Modules/RSParser/Sources/RSParser/Feeds/JSON/RSSInJSONParser.swift @@ -174,7 +174,7 @@ private extension RSSInJSONParser { } let type = enclosureObject["type"] as? String - if let attachment = ParsedAttachment(url: attachmentURL, mimeType: type, title: nil, sizeInBytes: attachmentSize, durationInSeconds: nil) { + if let attachment = ParsedAttachment(url: attachmentURL, mimeType: type, title: nil, sizeInBytes: attachmentSize, durationInSeconds: nil, width: nil, height: nil, medium: nil) { return Set([attachment]) } return nil diff --git a/Modules/RSParser/Sources/RSParser/Feeds/ParsedAttachment.swift b/Modules/RSParser/Sources/RSParser/Feeds/ParsedAttachment.swift index f1f0eda33..fd45d3a6e 100644 --- a/Modules/RSParser/Sources/RSParser/Feeds/ParsedAttachment.swift +++ b/Modules/RSParser/Sources/RSParser/Feeds/ParsedAttachment.swift @@ -9,13 +9,25 @@ import Foundation public struct ParsedAttachment: Hashable, Sendable { + /// Media RSS `medium` attribute. + public enum Medium: String, Sendable { + case image + case audio + case video + case document + case executable + } + public let url: String public let mimeType: String? public let title: String? public let sizeInBytes: Int? public let durationInSeconds: Int? + public let width: Int? + public let height: Int? + public let medium: Medium? - public init?(url: String, mimeType: String?, title: String?, sizeInBytes: Int?, durationInSeconds: Int?) { + public init?(url: String, mimeType: String?, title: String?, sizeInBytes: Int?, durationInSeconds: Int?, width: Int?, height: Int?, medium: Medium?) { if url.isEmpty { return nil } @@ -25,6 +37,9 @@ public struct ParsedAttachment: Hashable, Sendable { self.title = title self.sizeInBytes = sizeInBytes self.durationInSeconds = durationInSeconds + self.width = width + self.height = height + self.medium = medium } // MARK: - Hashable diff --git a/Modules/RSParser/Sources/RSParser/Feeds/XML/AtomParser.swift b/Modules/RSParser/Sources/RSParser/Feeds/XML/AtomParser.swift index a032123cf..66340e0d4 100644 --- a/Modules/RSParser/Sources/RSParser/Feeds/XML/AtomParser.swift +++ b/Modules/RSParser/Sources/RSParser/Feeds/XML/AtomParser.swift @@ -315,7 +315,7 @@ private final class AtomDelegate: XMLSAXParserDelegate { if rel == "enclosure" { let length = Int(attributes["length"] ?? "") ?? 0 let sizeInBytes = length > 0 ? length : nil - if let attachment = ParsedAttachment(url: resolved, mimeType: attributes["type"], title: attributes["title"], sizeInBytes: sizeInBytes, durationInSeconds: nil) { + if let attachment = ParsedAttachment(url: resolved, mimeType: attributes["type"], title: attributes["title"], sizeInBytes: sizeInBytes, durationInSeconds: nil, width: nil, height: nil, medium: nil) { article.attachments.insert(attachment) } return diff --git a/Modules/RSParser/Sources/RSParser/Feeds/XML/RSSParser.swift b/Modules/RSParser/Sources/RSParser/Feeds/XML/RSSParser.swift index ea936c01b..14ea65e8a 100644 --- a/Modules/RSParser/Sources/RSParser/Feeds/XML/RSSParser.swift +++ b/Modules/RSParser/Sources/RSParser/Feeds/XML/RSSParser.swift @@ -293,7 +293,7 @@ private final class RSSDelegate: XMLSAXParserDelegate { let length = Int(currentAttributes["length"] ?? "") ?? 0 let mimeType = currentAttributes["type"] let sizeInBytes = length > 0 ? length : nil - if let attachment = ParsedAttachment(url: url, mimeType: mimeType, title: nil, sizeInBytes: sizeInBytes, durationInSeconds: nil) { + if let attachment = ParsedAttachment(url: url, mimeType: mimeType, title: nil, sizeInBytes: sizeInBytes, durationInSeconds: nil, width: nil, height: nil, medium: nil) { article.attachments.insert(attachment) } } diff --git a/Modules/RSParser/Tests/RSParserTests/Feeds/JSON/JSONFeedParserTests.swift b/Modules/RSParser/Tests/RSParserTests/Feeds/JSON/JSONFeedParserTests.swift index 8187d32d1..de6ee6c49 100644 --- a/Modules/RSParser/Tests/RSParserTests/Feeds/JSON/JSONFeedParserTests.swift +++ b/Modules/RSParser/Tests/RSParserTests/Feeds/JSON/JSONFeedParserTests.swift @@ -98,4 +98,22 @@ import RSParser let bothAuthorsItem = try #require(parsedFeed.items.first { $0.uniqueID == "Item with both" }) #expect(bothAuthorsItem.authors == itemAuthors) } + + @Test func attachments() throws { + let d = parserData("attachments", "json", "https://example.com/") + let parsedFeed = try #require(try FeedParser.parse(d)) + let item = try #require(parsedFeed.items.first) + let attachments = try #require(item.attachments) + #expect(attachments.count == 2) + + let audio = try #require(attachments.first { $0.url == "https://example.com/episode1.mp3" }) + #expect(audio.mimeType == "audio/mpeg") + #expect(audio.title == "Episode 1") + #expect(audio.sizeInBytes == 12345678) + #expect(audio.durationInSeconds == 3600) + + // mime_type is optional in the JSON Feed spec. + let image = try #require(attachments.first { $0.url == "https://example.com/episode1.jpg" }) + #expect(image.mimeType == nil) + } } diff --git a/Modules/RSParser/Tests/RSParserTests/Resources/attachments.json b/Modules/RSParser/Tests/RSParserTests/Resources/attachments.json new file mode 100644 index 000000000..b31f98cb3 --- /dev/null +++ b/Modules/RSParser/Tests/RSParserTests/Resources/attachments.json @@ -0,0 +1,22 @@ +{ + "version": "https://jsonfeed.org/version/1.1", + "title": "Attachments test feed", + "items": [ + { + "id": "Episode 1", + "content_html": "
Show notes.
", + "attachments": [ + { + "url": "https://example.com/episode1.mp3", + "mime_type": "audio/mpeg", + "title": "Episode 1", + "size_in_bytes": 12345678, + "duration_in_seconds": 3600 + }, + { + "url": "https://example.com/episode1.jpg" + } + ] + } + ] +}