forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndexedFileRecord.swift
More file actions
119 lines (105 loc) · 3.68 KB
/
Copy pathIndexedFileRecord.swift
File metadata and controls
119 lines (105 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import Foundation
@preconcurrency import GRDB
// MARK: - File Type Category
enum FileTypeCategory: String, Codable, CaseIterable {
case document
case code
case image
case video
case audio
case spreadsheet
case presentation
case archive
case data
case other
static func from(extension ext: String?) -> FileTypeCategory {
guard let ext = ext?.lowercased() else { return .other }
switch ext {
case "pdf", "doc", "docx", "txt", "rtf", "md", "pages", "odt":
return .document
case "swift", "py", "js", "ts", "tsx", "jsx", "go", "rs", "java", "cpp", "c", "h", "rb", "php", "kt", "scala", "sh",
"bash", "zsh", "r", "m", "mm", "lua", "pl", "ex", "exs", "hs", "clj", "dart", "vue", "svelte":
return .code
case "png", "jpg", "jpeg", "gif", "svg", "psd", "ai", "sketch", "webp", "ico", "tiff", "bmp", "heic", "raw":
return .image
case "mp4", "mov", "avi", "mkv", "webm", "flv", "wmv", "m4v":
return .video
case "mp3", "wav", "aac", "m4a", "flac", "ogg", "wma", "aiff":
return .audio
case "xlsx", "xls", "csv", "numbers", "tsv", "ods":
return .spreadsheet
case "pptx", "ppt", "key", "odp":
return .presentation
case "zip", "tar", "gz", "dmg", "rar", "7z", "bz2", "xz", "pkg", "iso":
return .archive
case "json", "xml", "yaml", "yml", "sql", "db", "sqlite", "plist", "toml", "ini", "cfg", "conf":
return .data
default:
return .other
}
}
}
// MARK: - Indexed File Record
struct IndexedFileRecord: Codable, FetchableRecord, PersistableRecord, Identifiable {
var id: Int64?
var path: String // Relative to home directory (~/...)
var filename: String
var fileExtension: String?
var fileType: String // FileTypeCategory raw value
var sizeBytes: Int64
var folder: String // Top-level scanned folder: Downloads, Documents, Desktop
var depth: Int // 0 = root of scanned folder
var createdAt: Date? // File creation date
var modifiedAt: Date? // File modification date
var indexedAt: Date // When we indexed it
static let databaseTableName = "indexed_files"
// MARK: - Column mapping
enum Columns {
static let id = Column(CodingKeys.id)
static let path = Column(CodingKeys.path)
static let filename = Column(CodingKeys.filename)
static let fileExtension = Column(CodingKeys.fileExtension)
static let fileType = Column(CodingKeys.fileType)
static let sizeBytes = Column(CodingKeys.sizeBytes)
static let folder = Column(CodingKeys.folder)
static let depth = Column(CodingKeys.depth)
static let createdAt = Column(CodingKeys.createdAt)
static let modifiedAt = Column(CodingKeys.modifiedAt)
static let indexedAt = Column(CodingKeys.indexedAt)
}
// MARK: - Initialization
init(
id: Int64? = nil,
path: String,
filename: String,
fileExtension: String? = nil,
fileType: String,
sizeBytes: Int64,
folder: String,
depth: Int,
createdAt: Date? = nil,
modifiedAt: Date? = nil,
indexedAt: Date = Date()
) {
self.id = id
self.path = path
self.filename = filename
self.fileExtension = fileExtension
self.fileType = fileType
self.sizeBytes = sizeBytes
self.folder = folder
self.depth = depth
self.createdAt = createdAt
self.modifiedAt = modifiedAt
self.indexedAt = indexedAt
}
// MARK: - Persistence Callbacks
mutating func didInsert(_ inserted: InsertionSuccess) {
id = inserted.rowID
}
}
// MARK: - TableDocumented
extension IndexedFileRecord: TableDocumented {
static var tableDescription: String { ChatPrompts.tableAnnotations["indexed_files"]! }
static var columnDescriptions: [String: String] { ChatPrompts.columnAnnotations["indexed_files"] ?? [:] }
}