Improve storage, paths

This commit is contained in:
Christoph Hagen
2024-12-16 21:01:38 +01:00
parent b22b76fd32
commit 849585acc7
19 changed files with 393 additions and 268 deletions

View File

@@ -4,6 +4,11 @@ import Combine
final class Content: ObservableObject {
let storage = Storage()
@Published
var storageIsInitialized = false
@Published
var settings: Settings
@@ -28,48 +33,23 @@ final class Content: ObservableObject {
@Published
var isGeneratingWebsite = false
@AppStorage("contentPath")
private var storedContentPath: String = ""
@Published
var contentPath: String = "" {
didSet {
storedContentPath = contentPath
}
}
let storage: Storage
private var cancellables = Set<AnyCancellable>()
init(settings: Settings,
posts: [Post],
pages: [Page],
tags: [Tag],
files: [FileResource],
tagOverview: TagOverviewPage?,
storedContentPath: String) {
tagOverview: TagOverviewPage?) {
self.settings = settings
self.posts = posts
self.pages = pages
self.tags = tags
self.files = files
self.tagOverview = tagOverview
self.storedContentPath = storedContentPath
self.contentPath = storedContentPath
self.storage = Storage(baseFolder: URL(filePath: storedContentPath))
do {
try storage.createFolderStructure()
} catch {
print(error)
return
}
observeContentPath()
initialize()
}
init() {
self.storage = Storage(baseFolder: URL(filePath: ""))
self.settings = .mock
self.posts = []
self.pages = []
@@ -77,29 +57,24 @@ final class Content: ObservableObject {
self.files = []
self.tagOverview = nil
contentPath = storedContentPath
do {
try storage.createFolderStructure()
} catch {
print(error)
initialize()
}
private func initialize() {
guard storage.check(contentPath: settings.paths.contentDirectoryPath) == .nominal else {
storageIsInitialized = false
return
}
try? storage.update(baseFolder: URL(filePath: contentPath))
observeContentPath()
}
storage.check(outputPath: settings.paths.outputDirectoryPath)
private func observeContentPath() {
$contentPath.sink { newValue in
let url = URL(filePath: newValue)
do {
try self.storage.update(baseFolder: url)
try self.loadFromDisk()
} catch {
print("Failed to switch content path: \(error)")
}
do {
try storage.createFolderStructure()
storageIsInitialized = true
} catch {
print("Failed to initialize storage: \(error)")
storageIsInitialized = false
}
.store(in: &cancellables)
}
var images: [FileResource] {