2017-07-07 17:49:16 -07:00
|
|
|
//
|
|
|
|
// Attachment+Database.swift
|
2019-07-08 22:20:46 -07:00
|
|
|
// NetNewsWire
|
2017-07-07 17:49:16 -07:00
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 7/4/17.
|
|
|
|
// Copyright © 2017 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2018-07-23 18:29:08 -07:00
|
|
|
import Articles
|
2017-07-16 19:36:38 -07:00
|
|
|
import RSDatabase
|
2017-09-04 17:10:02 -07:00
|
|
|
import RSParser
|
2017-07-07 17:49:16 -07:00
|
|
|
|
|
|
|
extension Attachment {
|
2017-07-16 19:36:38 -07:00
|
|
|
|
2017-09-16 13:10:03 -07:00
|
|
|
init?(row: FMResultSet) {
|
2017-08-20 22:43:46 -07:00
|
|
|
guard let url = row.string(forColumn: DatabaseKey.url) else {
|
|
|
|
return nil
|
|
|
|
}
|
2017-07-16 19:36:38 -07:00
|
|
|
|
2017-09-16 13:10:03 -07:00
|
|
|
let attachmentID = row.string(forColumn: DatabaseKey.attachmentID)
|
2017-07-16 19:36:38 -07:00
|
|
|
let mimeType = row.string(forColumn: DatabaseKey.mimeType)
|
|
|
|
let title = row.string(forColumn: DatabaseKey.title)
|
2019-07-08 22:20:46 -07:00
|
|
|
let sizeInBytes = row.optionalIntForColumn(DatabaseKey.sizeInBytes)
|
|
|
|
let durationInSeconds = row.optionalIntForColumn(DatabaseKey.durationInSeconds)
|
2017-07-16 19:36:38 -07:00
|
|
|
|
2017-08-20 22:43:46 -07:00
|
|
|
self.init(attachmentID: attachmentID, url: url, mimeType: mimeType, title: title, sizeInBytes: sizeInBytes, durationInSeconds: durationInSeconds)
|
2017-07-07 17:49:16 -07:00
|
|
|
}
|
2017-07-16 19:36:38 -07:00
|
|
|
|
2017-09-04 17:10:02 -07:00
|
|
|
init?(parsedAttachment: ParsedAttachment) {
|
2017-09-10 11:36:28 -07:00
|
|
|
self.init(attachmentID: nil, url: parsedAttachment.url, mimeType: parsedAttachment.mimeType, title: parsedAttachment.title, sizeInBytes: parsedAttachment.sizeInBytes, durationInSeconds: parsedAttachment.durationInSeconds)
|
2017-09-04 17:10:02 -07:00
|
|
|
}
|
|
|
|
|
2017-09-10 11:36:28 -07:00
|
|
|
static func attachmentsWithParsedAttachments(_ parsedAttachments: Set<ParsedAttachment>?) -> Set<Attachment>? {
|
2017-09-04 17:10:02 -07:00
|
|
|
guard let parsedAttachments = parsedAttachments else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-27 21:50:48 -05:00
|
|
|
let attachments = parsedAttachments.compactMap{ Attachment(parsedAttachment: $0) }
|
2017-09-04 17:10:02 -07:00
|
|
|
return attachments.isEmpty ? nil : Set(attachments)
|
|
|
|
}
|
2017-08-20 22:43:46 -07:00
|
|
|
}
|
|
|
|
|
2017-07-16 19:36:38 -07:00
|
|
|
|
2017-08-20 22:43:46 -07:00
|
|
|
extension Attachment: DatabaseObject {
|
|
|
|
|
|
|
|
public var databaseID: String {
|
2018-02-14 13:14:25 -08:00
|
|
|
return attachmentID
|
2017-07-07 17:49:16 -07:00
|
|
|
}
|
2017-09-13 13:29:52 -07:00
|
|
|
|
2019-03-02 16:17:06 -08:00
|
|
|
public func databaseDictionary() -> DatabaseDictionary? {
|
|
|
|
var d: DatabaseDictionary = [DatabaseKey.attachmentID: attachmentID, DatabaseKey.url: url]
|
2017-09-13 13:29:52 -07:00
|
|
|
if let mimeType = mimeType {
|
|
|
|
d[DatabaseKey.mimeType] = mimeType
|
|
|
|
}
|
|
|
|
if let title = title {
|
|
|
|
d[DatabaseKey.title] = title
|
|
|
|
}
|
|
|
|
if let sizeInBytes = sizeInBytes {
|
|
|
|
d[DatabaseKey.sizeInBytes] = NSNumber(value: sizeInBytes)
|
|
|
|
}
|
|
|
|
if let durationInSeconds = durationInSeconds {
|
|
|
|
d[DatabaseKey.durationInSeconds] = NSNumber(value: durationInSeconds)
|
|
|
|
}
|
2019-03-02 16:17:06 -08:00
|
|
|
return d
|
2017-09-13 13:29:52 -07:00
|
|
|
}
|
|
|
|
|
2017-07-07 17:49:16 -07:00
|
|
|
}
|
2017-09-12 21:19:45 -07:00
|
|
|
|
2019-07-08 22:20:46 -07:00
|
|
|
private extension FMResultSet {
|
|
|
|
|
|
|
|
func optionalIntForColumn(_ columnName: String) -> Int? {
|
|
|
|
let intValue = long(forColumn: columnName)
|
|
|
|
if intValue < 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return intValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-12 21:19:45 -07:00
|
|
|
extension Set where Element == Attachment {
|
|
|
|
|
2019-03-02 16:17:06 -08:00
|
|
|
func databaseDictionaries() -> [DatabaseDictionary] {
|
2018-01-27 21:50:48 -05:00
|
|
|
return self.compactMap { $0.databaseDictionary() }
|
2017-09-12 21:19:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func databaseObjects() -> [DatabaseObject] {
|
2018-01-27 21:50:48 -05:00
|
|
|
return self.compactMap { $0 as DatabaseObject }
|
2017-09-12 21:19:45 -07:00
|
|
|
}
|
|
|
|
}
|