mirror of https://github.com/alibaba/MNN.git
87 lines
3.1 KiB
Swift
87 lines
3.1 KiB
Swift
//
|
|
// ChatHistoryFileManager.swift
|
|
// MNNLLMiOS
|
|
//
|
|
// Created by 游薪渝(揽清) on 2025/1/20.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
class ChatHistoryFileManager {
|
|
static let shared = ChatHistoryFileManager()
|
|
|
|
private init() {}
|
|
|
|
// Base directory for chat histories
|
|
private var baseDirectory: URL {
|
|
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
|
|
return documentsDirectory.appendingPathComponent("ChatHistories")
|
|
}
|
|
|
|
// Create a directory for a specific historyId if it doesn't already exist
|
|
func createHistoryDirectory(for historyId: String) {
|
|
let historyDirectory = baseDirectory.appendingPathComponent(historyId)
|
|
|
|
if !FileManager.default.fileExists(atPath: historyDirectory.path) {
|
|
do {
|
|
try FileManager.default.createDirectory(at: historyDirectory, withIntermediateDirectories: true, attributes: nil)
|
|
print("Created directory for historyId \(historyId)")
|
|
} catch {
|
|
print("Failed to create directory for historyId \(historyId): \(error)")
|
|
}
|
|
} else {
|
|
print("Directory for historyId \(historyId) already exists.")
|
|
}
|
|
}
|
|
|
|
// Generic method to copy a file from URL to the history directory
|
|
func copyFile(from url: URL, for historyId: String) -> URL? {
|
|
let historyDirectory = baseDirectory.appendingPathComponent(historyId)
|
|
|
|
// Ensure the history directory exists
|
|
createHistoryDirectory(for: historyId)
|
|
|
|
// Extract the file name from the URL
|
|
let fileName = url.lastPathComponent
|
|
let destinationURL = historyDirectory.appendingPathComponent(fileName)
|
|
|
|
// Check if the file already exists at the destination
|
|
if FileManager.default.fileExists(atPath: destinationURL.path) {
|
|
print("File already exists at \(destinationURL), returning original URL.")
|
|
return destinationURL
|
|
}
|
|
|
|
do {
|
|
try FileManager.default.copyItem(at: url, to: destinationURL)
|
|
print("File copied to \(destinationURL)")
|
|
return destinationURL
|
|
} catch {
|
|
print("Failed to copy file: \(error)")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Delete the history directory
|
|
func deleteHistoryDirectory(for historyId: String) {
|
|
let historyDirectory = baseDirectory.appendingPathComponent(historyId)
|
|
|
|
do {
|
|
try FileManager.default.removeItem(at: historyDirectory)
|
|
print("Deleted history directory for historyId \(historyId)")
|
|
} catch {
|
|
print("Failed to delete history directory: \(error)")
|
|
}
|
|
}
|
|
|
|
// Fetch all history directories
|
|
func fetchAllHistoryDirectories() -> [String] {
|
|
do {
|
|
let directories = try FileManager.default.contentsOfDirectory(at: baseDirectory, includingPropertiesForKeys: nil)
|
|
return directories.map { $0.lastPathComponent }
|
|
} catch {
|
|
print("Failed to fetch history directories: \(error)")
|
|
return []
|
|
}
|
|
}
|
|
}
|