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-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
|
|
|
|
case "jpg", "png", "gif":
|
|
|
|
return .image
|
|
|
|
case "mp3", "wav", "m4a":
|
|
|
|
return .audio
|
|
|
|
case "txt", "doc", "pdf":
|
|
|
|
return .file
|
|
|
|
default:
|
|
|
|
return .file // Default to .file if the extension is not recognized
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|