Add width, height, and medium properties to ParsedAttachment. Make JSON Feed mime_type optional.

This commit is contained in:
Brent Simmons committed 2026-10-22 21:05:06 -07:00
1 parent d9ec7df6f9
commit df20c6cc1c
8 files changed
+62 -9

No files matched your search

@@ -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)
}
@@ -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)
}
}
@@ -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
@@ -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
@@ -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
@@ -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)
}
}
@@ -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)
}
}
@@ -0,0 +1,22 @@
{
"version": "https://jsonfeed.org/version/1.1",
"title": "Attachments test feed",
"items": [
{
"id": "Episode 1",
"content_html": "<p>Show notes.</p>",
"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"
}
]
}
]
}