improvements on the ui
This commit is contained in:
@@ -59,6 +59,27 @@ public struct ImageStore: Sendable {
|
||||
return relativePath(target)
|
||||
}
|
||||
|
||||
/// Deletes the image files in the image folder whose `images/<name>` path is not in `used`.
|
||||
///
|
||||
/// Only regular files directly in the folder go; a symbolic link, a folder or a hidden file stays, and a file
|
||||
/// that cannot be deleted is skipped.
|
||||
/// - Returns: the paths of the deleted files, sorted.
|
||||
@discardableResult
|
||||
public func removeUnused(keeping used: Set<String>) -> [String] {
|
||||
let manager = FileManager.default
|
||||
let keys: Set<URLResourceKey> = [.isRegularFileKey, .isSymbolicLinkKey]
|
||||
guard (try? manager.attributesOfItem(atPath: folder.path)[.type] as? FileAttributeType) == .typeDirectory,
|
||||
let urls = try? manager.contentsOfDirectory(at: folder, includingPropertiesForKeys: Array(keys), options: .skipsHiddenFiles)
|
||||
else { return [] }
|
||||
var removed: [String] = []
|
||||
for url in urls where Self.isImageFile(url) && !used.contains(relativePath(url)) {
|
||||
guard let values = try? url.resourceValues(forKeys: keys), values.isRegularFile == true, values.isSymbolicLink != true,
|
||||
(try? manager.removeItem(at: url)) != nil else { continue }
|
||||
removed.append(relativePath(url))
|
||||
}
|
||||
return removed.sorted()
|
||||
}
|
||||
|
||||
/// The name for an image made now: `2026-10-16-085512-a3f0`.
|
||||
public func name(ext: String) -> String {
|
||||
var calendar = Calendar(identifier: .gregorian)
|
||||
|
||||
Reference in New Issue
Block a user