Handle media source validation more gracefully. (#3571)

* Handle media source validation more gracefully.

* Fix unit tests.
This commit is contained in:
Doug
2024-11-29 12:42:27 +00:00
committed by GitHub
parent c8627cfd64
commit 49a94e0bdb
13 changed files with 95 additions and 90 deletions
@@ -195,9 +195,7 @@ class RoomTimelineController: RoomTimelineControllerProtocol {
}
}
if let avatarURL {
let mediaSource = MediaSourceProxy(url: avatarURL, mimeType: nil)
if let avatarURL, let mediaSource = try? MediaSourceProxy(url: avatarURL, mimeType: nil) {
if case let .success(avatarData) = await mediaProvider.loadThumbnailForSource(source: mediaSource, size: .init(width: 100, height: 100)) {
sendMessageIntent.setImage(INImage(imageData: avatarData), forParameterNamed: \.speakableGroupName)
} else {
@@ -243,11 +243,15 @@ struct VideoInfoProxy: Hashable {
}
static var mockVideo: VideoInfoProxy {
.init(source: .init(url: .mockMXCVideo, mimeType: nil),
duration: 100,
size: .init(width: 1920, height: 1080),
aspectRatio: 1.78,
mimeType: nil)
guard let mediaSource = try? MediaSourceProxy(url: .mockMXCVideo, mimeType: nil) else {
fatalError("Invalid mock media source URL")
}
return .init(source: mediaSource,
duration: 100,
size: .init(width: 1920, height: 1080),
aspectRatio: 1.78,
mimeType: nil)
}
}
@@ -269,10 +273,6 @@ struct ImageInfoProxy: Hashable {
self.init(source: .init(source: source, mimeType: mimeType), width: width, height: height, mimeType: mimeType)
}
init(url: URL, width: UInt64?, height: UInt64?, mimeType: String?) {
self.init(source: .init(url: url, mimeType: mimeType), width: width, height: height, mimeType: mimeType)
}
init(source: MediaSourceProxy, width: UInt64?, height: UInt64?, mimeType: String?) {
self.source = source
@@ -292,17 +292,25 @@ struct ImageInfoProxy: Hashable {
}
static var mockImage: ImageInfoProxy {
.init(source: .init(url: .mockMXCImage, mimeType: "image/png"),
size: .init(width: 100, height: 100),
aspectRatio: 1,
mimeType: "image/png")
guard let mediaSource = try? MediaSourceProxy(url: .mockMXCImage, mimeType: "image/png") else {
fatalError("Invalid mock media source URL")
}
return .init(source: mediaSource,
size: .init(width: 100, height: 100),
aspectRatio: 1,
mimeType: "image/png")
}
static var mockThumbnail: ImageInfoProxy {
.init(source: .init(url: .mockMXCImage, mimeType: nil),
size: nil,
aspectRatio: nil,
mimeType: nil)
guard let mediaSource = try? MediaSourceProxy(url: .mockMXCImage, mimeType: "image/png") else {
fatalError("Invalid mock media source URL")
}
return .init(source: mediaSource,
size: nil,
aspectRatio: nil,
mimeType: nil)
}
}
@@ -33,12 +33,7 @@ struct RoomTimelineItemFactory: RoomTimelineItemFactoryProtocol {
case .redactedMessage:
return buildRedactedTimelineItem(eventItemProxy, isOutgoing)
case .sticker(let body, let imageInfo, let mediaSource):
guard let url = URL(string: mediaSource.url()) else {
MXLog.error("Invalid sticker url string: \(mediaSource.url())")
return buildUnsupportedTimelineItem(eventItemProxy, "m.sticker", "Invalid Sticker URL", isOutgoing)
}
return buildStickerTimelineItem(eventItemProxy, body, imageInfo, url, isOutgoing)
return buildStickerTimelineItem(eventItemProxy, body, imageInfo, mediaSource, isOutgoing)
case .failedToParseMessageLike(let eventType, let error):
return buildUnsupportedTimelineItem(eventItemProxy, eventType, error, isOutgoing)
case .failedToParseState(let eventType, _, let error):
@@ -119,9 +114,9 @@ struct RoomTimelineItemFactory: RoomTimelineItemFactoryProtocol {
private func buildStickerTimelineItem(_ eventItemProxy: EventTimelineItemProxy,
_ body: String,
_ info: MatrixRustSDK.ImageInfo,
_ imageURL: URL,
_ mediaSource: MediaSource,
_ isOutgoing: Bool) -> RoomTimelineItemProtocol {
let imageInfo = ImageInfoProxy(url: imageURL, width: info.width, height: info.height, mimeType: info.mimetype)
let imageInfo = ImageInfoProxy(source: mediaSource, width: info.width, height: info.height, mimeType: info.mimetype)
return StickerRoomTimelineItem(id: eventItemProxy.id,
body: body,