Skip to content

Commit 6eafa14

Browse files
committed
Improve article attachements parser
1 parent 5896877 commit 6eafa14

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

Modules/Sources/ParsingClient/Parsers/ArticleParser.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public struct ArticleParser {
4949
imageUrl: URL(string: (fields[10] as! String))!,
5050
title: (fields[11] as! String).convertHtmlCodes(),
5151
description: (fields[12] as! String).convertHtmlCodes(),
52-
attachments: AttachmentParser.parseArticleAttachment(from: fields[13] as! [[Any]]),
52+
attachments: try AttachmentParser.parseArticleAttachment(from: fields[13] as! [[Any]]),
5353
tags: extractTags(from: fields[14] as! [[Any]]),
5454
comments: extractComments(from: fields[15] as! [[Any]]),
5555
poll: extractPoll(from: pollFields)

Modules/Sources/ParsingClient/Parsers/AttachmentParser.swift

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,29 @@ struct AttachmentParser {
2020
4. "description" - description
2121
5. "https..." - (optional) full image url
2222
*/
23-
static func parseArticleAttachment(from array: [[Any]]) -> [Attachment] {
24-
return array.map { fields in
23+
static func parseArticleAttachment(from array: [[Any]]) throws -> [Attachment] {
24+
return try array.map { fields in
25+
guard let id = fields[safe: 0] as? Int,
26+
let url = fields[safe: 1] as? String,
27+
let width = fields[safe: 2] as? Int,
28+
let height = fields[safe: 3] as? Int,
29+
let description = fields[4] as? String else {
30+
throw ParsingError.failedToCastFields
31+
}
32+
33+
let fullUrl = fields[safe: 5] as? String
34+
2535
return Attachment(
26-
id: fields[0] as! Int,
36+
id: id,
2737
type: .image,
2838
name: "",
2939
size: 0,
3040
metadata: .init(
31-
width: fields[2] as! Int,
32-
height: fields[3] as! Int,
33-
url: URL(string: fields[1] as! String)!,
34-
fullUrl: URL(string: fields[5] as! String)
41+
width: width,
42+
height: height,
43+
url: URL(string: url)!,
44+
fullUrl: URL(string: fullUrl ?? ""),
45+
description: description
3546
),
3647
downloadCount: nil
3748
)

0 commit comments

Comments
 (0)