2024-07-09 12:37:51 +00:00
|
|
|
import Foundation
|
|
|
|
import GRDB
|
|
|
|
import Martin
|
|
|
|
import SwiftUI
|
|
|
|
|
2024-07-10 11:09:59 +00:00
|
|
|
enum AttachmentType: Int, Stateable, DatabaseValueConvertible {
|
|
|
|
case movie = 0
|
|
|
|
case image = 1
|
|
|
|
case audio = 2
|
|
|
|
case file = 3
|
2024-07-09 12:37:51 +00:00
|
|
|
}
|
|
|
|
|
2024-07-11 13:59:24 +00:00
|
|
|
struct Attachment: DBStorable {
|
|
|
|
static let databaseTableName = "attachments"
|
2024-07-10 11:09:59 +00:00
|
|
|
|
|
|
|
let id: String
|
2024-07-09 13:13:37 +00:00
|
|
|
let type: AttachmentType
|
2024-07-10 11:09:59 +00:00
|
|
|
let localPath: URL?
|
|
|
|
let remotePath: URL?
|
|
|
|
let localThumbnailPath: URL?
|
2024-07-11 13:59:24 +00:00
|
|
|
let messageId: String
|
2024-07-13 01:29:46 +00:00
|
|
|
var downloadFailed: Bool = false
|
2024-07-10 11:09:59 +00:00
|
|
|
|
2024-07-11 13:59:24 +00:00
|
|
|
static let message = belongsTo(Message.self)
|
|
|
|
var message: QueryInterfaceRequest<Message> {
|
|
|
|
request(for: Attachment.message)
|
|
|
|
}
|
2024-07-09 12:37:51 +00:00
|
|
|
}
|
2024-07-10 11:09:59 +00:00
|
|
|
|
|
|
|
extension Attachment: Equatable {}
|
2024-07-11 13:59:24 +00:00
|
|
|
|
|
|
|
extension String {
|
|
|
|
var attachmentType: AttachmentType {
|
|
|
|
let ext = (self as NSString).pathExtension.lowercased()
|
|
|
|
|
|
|
|
switch ext {
|
|
|
|
case "mov", "mp4", "avi":
|
|
|
|
return .movie
|
2024-07-13 01:29:46 +00:00
|
|
|
|
2024-07-11 13:59:24 +00:00
|
|
|
case "jpg", "png", "gif":
|
|
|
|
return .image
|
2024-07-13 01:29:46 +00:00
|
|
|
|
2024-07-11 13:59:24 +00:00
|
|
|
case "mp3", "wav", "m4a":
|
|
|
|
return .audio
|
2024-07-13 01:29:46 +00:00
|
|
|
|
2024-07-11 13:59:24 +00:00
|
|
|
case "txt", "doc", "pdf":
|
|
|
|
return .file
|
2024-07-13 01:29:46 +00:00
|
|
|
|
2024-07-11 13:59:24 +00:00
|
|
|
default:
|
2024-07-13 01:29:46 +00:00
|
|
|
return .file
|
2024-07-11 13:59:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|