-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultFileSystem.swift
More file actions
51 lines (39 loc) · 1.38 KB
/
DefaultFileSystem.swift
File metadata and controls
51 lines (39 loc) · 1.38 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
//
// DefaultFileSystem.swift
// nnex
//
// Created by Nikolai Nobadi on 12/9/25.
//
import Files
import Foundation
public struct DefaultFileSystem {
private let fileManager: FileManager
public init(fileManager: FileManager = .default) {
self.fileManager = fileManager
}
}
// MARK: - FileSystem
extension DefaultFileSystem: FileSystem {
public var homeDirectory: any Directory {
return FilesDirectoryAdapter(folder: Folder.home)
}
public var currentDirectory: any Directory {
return FilesDirectoryAdapter(folder: Folder.current)
}
public func directory(at path: String) throws -> any Directory {
return try FilesDirectoryAdapter(folder: Folder(path: path))
}
public func desktopDirectory() throws -> any Directory {
let desktopPath = fileManager.homeDirectoryForCurrentUser.appending(path: "Desktop").path()
return try directory(at: desktopPath)
}
public func readFile(at path: String) throws -> String {
return try String(contentsOfFile: path, encoding: .utf8)
}
public func writeFile(at path: String, contents: String) throws {
try contents.write(toFile: path, atomically: true, encoding: .utf8)
}
public func moveToTrash(at path: String) throws {
try fileManager.trashItem(at: .init(fileURLWithPath: path), resultingItemURL: nil)
}
}