Add local file previews to chat and split the Toolbox layout
This commit is contained in:
@@ -395,3 +395,30 @@ enum GiteaRepositoryLookup {
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
struct ChatFilePreview: Identifiable, Sendable {
|
||||
let id = UUID()
|
||||
let name: String
|
||||
let text: String
|
||||
|
||||
static func read(_ url: URL) throws -> Self {
|
||||
let scoped = url.startAccessingSecurityScopedResource()
|
||||
defer { if scoped { url.stopAccessingSecurityScopedResource() } }
|
||||
let text: String
|
||||
if try url.resourceValues(forKeys: [.isDirectoryKey]).isDirectory == true {
|
||||
let entries = try FileManager.default.contentsOfDirectory(
|
||||
at: url, includingPropertiesForKeys: [.isDirectoryKey], options: [.skipsHiddenFiles]
|
||||
).sorted { $0.lastPathComponent.localizedStandardCompare($1.lastPathComponent) == .orderedAscending }
|
||||
let names = entries.prefix(75).map { $0.lastPathComponent + ($0.hasDirectoryPath ? "/" : "") }
|
||||
text = "Folder: \(url.lastPathComponent)\n" +
|
||||
(names.isEmpty ? "(Empty)" : names.joined(separator: "\n")) +
|
||||
(entries.count > 75 ? "\n[Listing limited to 75 entries]" : "")
|
||||
} else if url.pathExtension.lowercased() == "pdf" {
|
||||
text = try LibraryImport.chunks(at: url).prefix(5).joined(separator: "\n\n")
|
||||
} else {
|
||||
text = try LocalFilePreview.read(url, limit: 7500)
|
||||
}
|
||||
let bounded = text.count > 8000 ? String(text.prefix(7960)) + "\n[Preview truncated]" : text
|
||||
return Self(name: url.lastPathComponent, text: bounded)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user