624 lines
21 KiB
Swift
624 lines
21 KiB
Swift
import Foundation
|
|
|
|
enum SecurityScopeBookmark: String {
|
|
|
|
case outputPath = "outputPathBookmark"
|
|
|
|
case contentPath = "contentPathBookmark"
|
|
}
|
|
|
|
enum StorageAccessError: Error {
|
|
|
|
case noBookmarkData
|
|
|
|
case bookmarkDataCorrupted(Error)
|
|
|
|
case folderAccessFailed(URL)
|
|
|
|
case stringConversionFailed
|
|
|
|
case fileNotFound(String)
|
|
|
|
}
|
|
|
|
extension StorageAccessError: CustomStringConvertible {
|
|
|
|
var description: String {
|
|
switch self {
|
|
case .noBookmarkData:
|
|
return "No bookmark data to access resources in folder"
|
|
case .bookmarkDataCorrupted(let error):
|
|
return "Failed to resolve bookmark: \(error)"
|
|
case .folderAccessFailed(let url):
|
|
return "Failed to access folder: \(url.path())"
|
|
case .stringConversionFailed:
|
|
return "Failed to convert string to data"
|
|
case .fileNotFound(let path):
|
|
return "File not found: \(path)"
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
A class that handles the storage of the website data.
|
|
|
|
BaseFolder
|
|
- pages: Contains the markdown files of the localized pages, file name is the url
|
|
- images: Contains the raw images
|
|
- files: Contains additional files
|
|
- videos: Contains raw video files
|
|
- posts: Contains the markdown files for localized posts, file name is the post id
|
|
-
|
|
*/
|
|
final class Storage {
|
|
|
|
private(set) var baseFolder: URL
|
|
|
|
private let encoder = JSONEncoder()
|
|
|
|
private let decoder = JSONDecoder()
|
|
|
|
private let fm = FileManager.default
|
|
|
|
/**
|
|
Create the storage.
|
|
*/
|
|
init(baseFolder: URL) {
|
|
self.baseFolder = baseFolder
|
|
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
|
|
}
|
|
|
|
// MARK: Helper
|
|
|
|
private func subFolder(_ name: String) -> URL {
|
|
baseFolder.appending(path: name, directoryHint: .isDirectory)
|
|
}
|
|
|
|
private func files(in folder: URL) throws -> [URL] {
|
|
do {
|
|
return try fm.contentsOfDirectory(at: folder, includingPropertiesForKeys: [.isDirectoryKey])
|
|
.filter { !$0.hasDirectoryPath }
|
|
} catch {
|
|
print("Failed to get files in folder \(folder.path): \(error)")
|
|
throw error
|
|
}
|
|
}
|
|
|
|
private func fileNames(in folder: URL) throws -> [String] {
|
|
try fm.contentsOfDirectory(atPath: folder.path())
|
|
.filter { !$0.hasPrefix(".") }
|
|
.sorted()
|
|
}
|
|
|
|
private func files(in folder: URL, type: String) throws -> [URL] {
|
|
try files(in: folder).filter { $0.pathExtension == type }
|
|
}
|
|
|
|
// MARK: Folders
|
|
|
|
func update(baseFolder: URL) throws {
|
|
self.baseFolder = baseFolder
|
|
try createFolderStructure()
|
|
}
|
|
|
|
private func create(folder: URL) throws {
|
|
guard !FileManager.default.fileExists(atPath: folder.path) else {
|
|
return
|
|
}
|
|
try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
|
|
}
|
|
|
|
func createFolderStructure() throws {
|
|
try operate(in: .contentPath) { contentPath in
|
|
try create(folder: pagesFolder(in: contentPath))
|
|
try create(folder: filesFolder(in: contentPath))
|
|
try create(folder: postsFolder(in: contentPath))
|
|
try create(folder: tagsFolder(in: contentPath))
|
|
}
|
|
}
|
|
|
|
// MARK: Pages
|
|
|
|
private let pagesFolderName = "pages"
|
|
|
|
/// The folder path where the markdown and metadata files of the pages are stored (by their id/url component)
|
|
private func pagesFolder(in folder: URL) -> URL {
|
|
folder.appending(path: pagesFolderName, directoryHint: .isDirectory)
|
|
}
|
|
|
|
private func pageContentFileName(_ id: String, _ language: ContentLanguage) -> String {
|
|
"\(id)-\(language.rawValue).md"
|
|
}
|
|
|
|
private func pageContentPath(page pageId: String, language: ContentLanguage) -> String {
|
|
pagesFolderName + "/" + pageContentFileName(pageId, language)
|
|
}
|
|
|
|
private func pageMetadataPath(page pageId: String) -> String {
|
|
pagesFolderName + "/" + pageId + ".json"
|
|
}
|
|
|
|
private func pageFileName(_ id: String) -> String {
|
|
id + ".json"
|
|
}
|
|
|
|
private func pageContentUrl(page pageId: String, language: ContentLanguage, in folder: URL) -> URL {
|
|
let fileName = pageContentFileName(pageId, language)
|
|
return pagesFolder(in: folder).appending(path: fileName, directoryHint: .notDirectory)
|
|
}
|
|
|
|
private func pageMetadataUrl(page pageId: String, in folder: URL) -> URL {
|
|
let fileName = pageFileName(pageId)
|
|
return pagesFolder(in: folder).appending(path: fileName, directoryHint: .notDirectory)
|
|
}
|
|
|
|
func save(pageContent: String, for pageId: String, language: ContentLanguage) throws {
|
|
let path = pageContentPath(page: pageId, language: language)
|
|
try writeIfChanged(content: pageContent, to: path)
|
|
}
|
|
|
|
func save(pageMetadata: PageFile, for pageId: String) throws {
|
|
let path = pageMetadataPath(page: pageId)
|
|
try writeIfChanged(pageMetadata, to: path)
|
|
}
|
|
|
|
func loadAllPages() throws -> [String : PageFile] {
|
|
try decodeAllFromJson(in: pagesFolderName)
|
|
}
|
|
|
|
func pageContent(for pageId: String, language: ContentLanguage) throws -> String {
|
|
let path = pageContentPath(page: pageId, language: language)
|
|
return try readString(at: path, defaultValue: "")
|
|
}
|
|
|
|
/**
|
|
Delete all files associated with pages that are not in the given set
|
|
- Note: This function requires a security scope for the content path
|
|
*/
|
|
func deletePageFiles(notIn pages: [String]) throws {
|
|
var files = Set(pages.map(pageFileName))
|
|
for language in ContentLanguage.allCases {
|
|
files.formUnion(pages.map { pageContentFileName($0, language) })
|
|
}
|
|
try deleteFiles(in: pagesFolderName, notIn: files)
|
|
}
|
|
|
|
func move(page pageId: String, to newFile: String) -> Bool {
|
|
do {
|
|
try operate(in: .contentPath) { contentPath in
|
|
// Move the metadata file
|
|
let source = pageMetadataUrl(page: pageId, in: contentPath)
|
|
let destination = pageMetadataUrl(page: newFile, in: contentPath)
|
|
try fm.moveItem(at: source, to: destination)
|
|
|
|
// Move the existing content files
|
|
for language in ContentLanguage.allCases {
|
|
let source = pageContentUrl(page: pageId, language: language, in: contentPath)
|
|
guard source.exists else { continue }
|
|
let destination = pageContentUrl(page: newFile, language: language, in: contentPath)
|
|
try fm.moveItem(at: source, to: destination)
|
|
}
|
|
}
|
|
return true
|
|
} catch {
|
|
print("Failed to move page file \(pageId) to \(newFile): \(error)")
|
|
return false
|
|
}
|
|
}
|
|
|
|
// MARK: Posts
|
|
|
|
private let postsFolderName = "posts"
|
|
|
|
private func postFileName(_ postId: String) -> String {
|
|
postId + ".json"
|
|
}
|
|
|
|
/// The folder path where the markdown files of the posts are stored (by their unique id/url component)
|
|
private func postsFolder(in folder: URL) -> URL {
|
|
folder.appending(path: postsFolderName, directoryHint: .isDirectory)
|
|
}
|
|
|
|
private func postFileUrl(post postId: String, in folder: URL) -> URL {
|
|
let path = postFilePath(post: postId)
|
|
return folder.appending(path: path, directoryHint: .notDirectory)
|
|
}
|
|
|
|
private func postFilePath(post postId: String) -> String {
|
|
postsFolderName + "/" + postFileName(postId)
|
|
}
|
|
|
|
func save(post: PostFile, for postId: String) throws {
|
|
let path = postFilePath(post: postId)
|
|
try writeIfChanged(post, to: path)
|
|
}
|
|
|
|
func loadAllPosts() throws -> [String : PostFile] {
|
|
try decodeAllFromJson(in: postsFolderName)
|
|
}
|
|
|
|
private func postContent(for postId: String) throws -> PostFile {
|
|
let path = postFilePath(post: postId)
|
|
return try read(at: path)
|
|
}
|
|
|
|
/**
|
|
Delete all files associated with posts that are not in the given set
|
|
- Note: This function requires a security scope for the content path
|
|
*/
|
|
func deletePostFiles(notIn posts: [String]) throws {
|
|
let files = Set(posts.map(postFileName))
|
|
try deleteFiles(in: postsFolderName, notIn: files)
|
|
}
|
|
|
|
func move(post postId: String, to newFile: String) throws {
|
|
try operate(in: .contentPath) { contentPath in
|
|
let source = postFileUrl(post: postId, in: contentPath)
|
|
let destination = postFileUrl(post: newFile, in: contentPath)
|
|
try fm.moveItem(at: source, to: destination)
|
|
}
|
|
}
|
|
|
|
// MARK: Tags
|
|
|
|
private let tagsFolderName = "tags"
|
|
|
|
private func tagFileName(tagId: String) -> String {
|
|
tagId + ".json"
|
|
}
|
|
|
|
/// The folder path where the source images are stored (by their unique name)
|
|
private func tagsFolder(in folder: URL) -> URL {
|
|
folder.appending(path: tagsFolderName)
|
|
}
|
|
|
|
private func relativeTagFilePath(tagId: String) -> String {
|
|
tagsFolderName + "/" + tagFileName(tagId: tagId)
|
|
}
|
|
|
|
func save(tagMetadata: TagFile, for tagId: String) throws {
|
|
let path = relativeTagFilePath(tagId: tagId)
|
|
try writeIfChanged(tagMetadata, to: path)
|
|
}
|
|
|
|
func loadAllTags() throws -> [String : TagFile] {
|
|
try decodeAllFromJson(in: tagsFolderName)
|
|
}
|
|
|
|
/**
|
|
Delete all files associated with tags that are not in the given set
|
|
- Note: This function requires a security scope for the content path
|
|
*/
|
|
func deleteTagFiles(notIn tags: [String]) throws {
|
|
let files = Set(tags.map { $0 + ".json" })
|
|
try deleteFiles(in: tagsFolderName, notIn: files)
|
|
}
|
|
|
|
// MARK: File descriptions
|
|
|
|
private let fileDescriptionFilename = "file-descriptions.json"
|
|
|
|
func loadFileDescriptions() throws -> [FileDescriptions] {
|
|
try read(at: fileDescriptionFilename, defaultValue: [])
|
|
}
|
|
|
|
func save(fileDescriptions: [FileDescriptions]) throws {
|
|
try writeIfChanged(fileDescriptions, to: fileDescriptionFilename)
|
|
}
|
|
|
|
// MARK: Files
|
|
|
|
private let filesFolderName = "files"
|
|
|
|
private func filePath(file fileId: String) -> String {
|
|
filesFolderName + "/" + fileId
|
|
}
|
|
|
|
/// The folder path where other files are stored (by their unique name)
|
|
private func filesFolder(in folder: URL) -> URL {
|
|
folder.appending(path: filesFolderName, directoryHint: .isDirectory)
|
|
}
|
|
|
|
private func fileUrl(file: String, in folder: URL) -> URL {
|
|
filesFolder(in: folder).appending(path: file, directoryHint: .notDirectory)
|
|
}
|
|
|
|
/**
|
|
Copy an external file to the content folder
|
|
*/
|
|
func copyFile(at url: URL, fileId: String) throws {
|
|
try operate(in: .contentPath) { contentPath in
|
|
let destination = fileUrl(file: fileId, in: contentPath)
|
|
try fm.copyItem(at: url, to: destination)
|
|
}
|
|
}
|
|
|
|
func move(file fileId: String, to newFile: String) throws {
|
|
try operate(in: .contentPath) { contentPath in
|
|
let source = fileUrl(file: fileId, in: contentPath)
|
|
let destination = fileUrl(file: newFile, in: contentPath)
|
|
try fm.moveItem(at: source, to: destination)
|
|
}
|
|
}
|
|
|
|
func copy(file fileId: String, to relativeOutputPath: String) throws {
|
|
let path = filePath(file: fileId)
|
|
try withScopedContent(file: path) { input in
|
|
try operate(in: .outputPath) { outputPath in
|
|
let output = outputPath.appending(path: relativeOutputPath, directoryHint: .notDirectory)
|
|
if output.exists {
|
|
return
|
|
}
|
|
try output.ensureParentFolderExistence()
|
|
|
|
try FileManager.default.copyItem(at: input, to: output)
|
|
}
|
|
}
|
|
}
|
|
|
|
func loadAllFiles() throws -> [String] {
|
|
try self.existingFiles(in: filesFolderName)
|
|
.map { $0.lastPathComponent }
|
|
}
|
|
|
|
/**
|
|
Delete all file resources that are not in the given set
|
|
- Note: This function requires a security scope for the content path
|
|
*/
|
|
func deleteFileResources(notIn fileSet: [String]) throws {
|
|
try deleteFiles(in: filesFolderName, notIn: Set(fileSet))
|
|
}
|
|
|
|
func fileContent(for fileId: String) throws -> String {
|
|
let path = filePath(file: fileId)
|
|
return try readString(at: path)
|
|
}
|
|
|
|
func fileData(for fileId: String) throws -> Data {
|
|
let path = filePath(file: fileId)
|
|
return try readExistingFile(at: path)
|
|
}
|
|
|
|
// MARK: Website data
|
|
|
|
private let settingsDataFileName: String = "settings.json"
|
|
|
|
func loadSettings() throws -> SettingsFile {
|
|
try read(at: settingsDataFileName, defaultValue: .default)
|
|
}
|
|
|
|
func save(settings: SettingsFile) throws {
|
|
try writeIfChanged(settings, to: settingsDataFileName)
|
|
}
|
|
|
|
// MARK: Image generation data
|
|
|
|
private let generatedImagesListName = "generated-images.json"
|
|
|
|
func loadListOfGeneratedImages() throws -> [String : [String]] {
|
|
try read(at: generatedImagesListName, defaultValue: [:])
|
|
}
|
|
|
|
func save(listOfGeneratedImages: [String : [String]]) throws {
|
|
try writeIfChanged(listOfGeneratedImages, to: generatedImagesListName)
|
|
}
|
|
|
|
// MARK: Output files
|
|
|
|
func write(content: String, to relativeOutputPath: String) throws {
|
|
try writeIfChanged(content: content, to: relativeOutputPath, in: .outputPath)
|
|
}
|
|
|
|
// MARK: Folder access
|
|
|
|
func save(folderUrl url: URL, in bookmark: SecurityScopeBookmark) {
|
|
do {
|
|
let bookmarkData = try url.bookmarkData(options: .withSecurityScope, includingResourceValuesForKeys: nil, relativeTo: nil)
|
|
UserDefaults.standard.set(bookmarkData, forKey: bookmark.rawValue)
|
|
} catch {
|
|
print("Failed to create security-scoped bookmark: \(error)")
|
|
}
|
|
}
|
|
|
|
func write(in scope: SecurityScopeBookmark, operation: (URL) -> Bool) -> Bool {
|
|
do {
|
|
return try operate(in: scope, operation: operation)
|
|
} catch {
|
|
print(error)
|
|
return false
|
|
}
|
|
}
|
|
|
|
private func withScopedContent<T>(file relativePath: String, in scope: SecurityScopeBookmark = .contentPath, _ operation: (URL) throws -> T) throws -> T {
|
|
try withScopedContent(relativePath, in: scope, directoryHint: .notDirectory, operation)
|
|
}
|
|
|
|
private func withScopedContent<T>(folder relativePath: String, in scope: SecurityScopeBookmark = .contentPath, _ operation: (URL) throws -> T) throws -> T {
|
|
try withScopedContent(relativePath, in: scope, directoryHint: .isDirectory, operation)
|
|
}
|
|
|
|
private func withScopedContent<T>(_ relativePath: String, in scope: SecurityScopeBookmark, directoryHint: URL.DirectoryHint, _ operation: (URL) throws -> T) throws -> T {
|
|
try operate(in: scope) {
|
|
let url = $0.appending(path: relativePath, directoryHint: directoryHint)
|
|
return try operation(url)
|
|
}
|
|
}
|
|
|
|
func operate<T>(in scope: SecurityScopeBookmark, operation: (URL) throws -> T) throws -> T {
|
|
guard let bookmarkData = UserDefaults.standard.data(forKey: scope.rawValue) else {
|
|
throw StorageAccessError.noBookmarkData
|
|
}
|
|
var isStale = false
|
|
let folderUrl: URL
|
|
do {
|
|
// Resolve the bookmark to get the folder URL
|
|
folderUrl = try URL(resolvingBookmarkData: bookmarkData, options: .withSecurityScope, relativeTo: nil, bookmarkDataIsStale: &isStale)
|
|
} catch {
|
|
throw StorageAccessError.bookmarkDataCorrupted(error)
|
|
}
|
|
|
|
if isStale {
|
|
print("Bookmark is stale, consider saving a new bookmark.")
|
|
}
|
|
|
|
// Start accessing the security-scoped resource
|
|
guard folderUrl.startAccessingSecurityScopedResource() else {
|
|
throw StorageAccessError.folderAccessFailed(folderUrl)
|
|
}
|
|
defer { folderUrl.stopAccessingSecurityScopedResource() }
|
|
return try operation(folderUrl)
|
|
}
|
|
|
|
// MARK: Writing files
|
|
|
|
/**
|
|
Delete files in a subPath of the content folder which are not in the given set of files
|
|
- Note: This function requires a security scope for the content path
|
|
*/
|
|
private func deleteFiles(in folder: String, notIn fileSet: Set<String>) throws {
|
|
try withScopedContent(folder: folder) { folderUrl in
|
|
let filesToDelete = try files(in: folderUrl)
|
|
.filter { !fileSet.contains($0.lastPathComponent) }
|
|
|
|
for file in filesToDelete {
|
|
try fm.removeItem(at: file)
|
|
print("Deleted \(file.path())")
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
Write the data of an encodable value to a relative path in the content folder
|
|
- Note: This function requires a security scope for the content path
|
|
*/
|
|
private func writeIfChanged<T>(_ value: T, to relativePath: String) throws where T: Encodable {
|
|
let data = try encoder.encode(value)
|
|
try writeIfChanged(data: data, to: relativePath)
|
|
}
|
|
|
|
/**
|
|
Write the data of a string to a relative path in the content folder
|
|
- Note: This function requires a security scope for the content path
|
|
*/
|
|
private func writeIfChanged(content: String, to relativePath: String, in scope: SecurityScopeBookmark = .contentPath) throws {
|
|
guard let data = content.data(using: .utf8) else {
|
|
print("Failed to convert string to data for file at \(relativePath)")
|
|
throw StorageAccessError.stringConversionFailed
|
|
}
|
|
try writeIfChanged(data: data, to: relativePath, in: scope)
|
|
}
|
|
|
|
/**
|
|
Write the data to a relative path in the content folder
|
|
- Note: This function requires a security scope for the content path
|
|
*/
|
|
private func writeIfChanged(data: Data, to relativePath: String, in scope: SecurityScopeBookmark = .contentPath) throws {
|
|
try withScopedContent(file: relativePath, in: scope) { url in
|
|
if fm.fileExists(atPath: url.path()) {
|
|
// Check if content is the same, to prevent unnecessary writes
|
|
do {
|
|
let oldData = try Data(contentsOf: url)
|
|
if data == oldData {
|
|
// File is the same, don't write
|
|
return
|
|
}
|
|
} catch {
|
|
print("Failed to read file \(url.path()) for equality check: \(error)")
|
|
// No check possible, write file
|
|
}
|
|
} else {
|
|
print("Writing new file \(url.path())")
|
|
try url.ensureParentFolderExistence()
|
|
}
|
|
try data.write(to: url)
|
|
print("Saved file \(url.path())")
|
|
}
|
|
}
|
|
|
|
/**
|
|
|
|
- Note: This function requires a security scope for the content path
|
|
*/
|
|
private func read<T>(at relativePath: String, defaultValue: T? = nil) throws -> T where T: Decodable {
|
|
guard let data = try readData(at: relativePath) else {
|
|
guard let defaultValue else {
|
|
throw StorageAccessError.fileNotFound(relativePath)
|
|
}
|
|
return defaultValue
|
|
}
|
|
return try decoder.decode(T.self, from: data)
|
|
}
|
|
|
|
/**
|
|
|
|
- Note: This function requires a security scope for the content path
|
|
*/
|
|
private func readString(at relativePath: String, defaultValue: String? = nil) throws -> String {
|
|
try withScopedContent(file: relativePath) { url in
|
|
guard url.exists else {
|
|
guard let defaultValue else {
|
|
throw StorageAccessError.fileNotFound(relativePath)
|
|
}
|
|
return defaultValue
|
|
}
|
|
return try String(contentsOf: url, encoding: .utf8)
|
|
}
|
|
}
|
|
|
|
private func readExistingFile(at relativePath: String) throws -> Data {
|
|
guard let data = try readData(at: relativePath) else {
|
|
throw StorageAccessError.fileNotFound(relativePath)
|
|
}
|
|
return data
|
|
}
|
|
|
|
/**
|
|
|
|
- Note: This function requires a security scope for the content path
|
|
*/
|
|
private func readData(at relativePath: String) throws -> Data? {
|
|
try withScopedContent(file: relativePath) { url in
|
|
guard url.exists else {
|
|
return nil
|
|
}
|
|
return try Data(contentsOf: url)
|
|
}
|
|
}
|
|
|
|
private func getFiles(in folder: URL) throws -> [URL] {
|
|
try fm.contentsOfDirectory(at: folder, includingPropertiesForKeys: [.isDirectoryKey])
|
|
.filter { !$0.hasDirectoryPath }
|
|
}
|
|
|
|
private func existingFiles(in folder: String) throws -> [URL] {
|
|
try withScopedContent(folder: folder, getFiles)
|
|
}
|
|
|
|
/**
|
|
|
|
- Note: This function requires a security scope for the content path
|
|
*/
|
|
private func decodeAllFromJson<T>(in folder: String) throws -> [String : T] where T: Decodable {
|
|
try withScopedContent(folder: folder) { folderUrl in
|
|
try getFiles(in: folderUrl)
|
|
.filter { $0.pathExtension.lowercased() == "json" }
|
|
.reduce(into: [:]) { items, url in
|
|
let id = url.deletingPathExtension().lastPathComponent
|
|
let data = try Data(contentsOf: url)
|
|
items[id] = try decoder.decode(T.self, from: data)
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
|
|
- Note: This function requires a security scope for the content path
|
|
*/
|
|
private func copy(file: URL, to relativePath: String) throws {
|
|
try withScopedContent(file: relativePath) { destination in
|
|
try destination.ensureParentFolderExistence()
|
|
try fm.copyItem(at: file, to: destination)
|
|
}
|
|
}
|
|
}
|