Use video preview if available

This commit is contained in:
Maurice Parker 2020-05-05 21:08:51 -05:00
parent 00cb83559c
commit 6c698c5b94
2 changed files with 34 additions and 5 deletions

View File

@ -73,10 +73,11 @@ struct RedditLinkData: Codable {
return mediaEmbedContent
}
if isVideo ?? false {
guard let fallbackURL = media?.video?.fallbackURL else {
return nil
if url.hasSuffix(".gif") {
return "<figure><img src=\"\(url)\"></figure>"
}
if isVideo ?? false, let videoURL = media?.video?.fallbackURL {
var html = "<video "
if let previewImageURL = preview?.images?.first?.source?.url {
html += "poster=\"\(previewImageURL)\" "
@ -84,7 +85,19 @@ struct RedditLinkData: Codable {
if let width = media?.video?.width, let height = media?.video?.height {
html += "width=\"\(width)\" height=\"\(height)\" "
}
html += "src=\"\(fallbackURL)\"></video>"
html += "src=\"\(videoURL)\"></video>"
return html
}
if let videoPreviewURL = preview?.videoPreview?.url {
var html = "<video "
if let previewImageURL = preview?.images?.first?.source?.url {
html += "poster=\"\(previewImageURL)\" "
}
if let width = preview?.videoPreview?.width, let height = preview?.videoPreview?.height {
html += "width=\"\(width)\" height=\"\(height)\" "
}
html += "src=\"\(videoPreviewURL)\"></video>"
return html
}

View File

@ -11,9 +11,11 @@ import Foundation
struct RedditPreview: Codable {
let images: [RedditPreviewImage]?
let videoPreview: RedditVideoPreview?
enum CodingKeys: String, CodingKey {
case images = "images"
case videoPreview = "reddit_video_preview"
}
}
@ -41,3 +43,17 @@ struct RedditPreviewImageSource: Codable {
}
}
struct RedditVideoPreview: Codable {
let url: String?
let width: Int?
let height: Int?
enum CodingKeys: String, CodingKey {
case url = "fallback_url"
case width = "width"
case height = "height"
}
}