External files, improve page generation

This commit is contained in:
Christoph Hagen
2024-12-10 15:21:28 +01:00
parent 8183bc4903
commit efc9234917
50 changed files with 1069 additions and 424 deletions

View File

@ -18,6 +18,10 @@ extension Content {
return prefix + page.localized(in: language).urlString
}
func page(_ pageId: String) -> Page? {
pages.first { $0.id == pageId }
}
func pageLink(pageId: String, language: ContentLanguage) -> String? {
guard let page = pages.first(where: { $0.id == pageId }) else {
// TODO: Note missing link
@ -31,18 +35,42 @@ extension Content {
guard let file = file(id: fileId) else {
return nil
}
switch file.type {
case .image: return pathToImage(file)
case .video: return pathToVideo(file)
default: return pathToFile(file)
}
}
func pathToFile(_ file: FileResource) -> String {
#warning("Add files path to settings")
return "/files/\(file.id)"
}
func image(_ imageId: String) -> FileResource? {
files.first { $0.id == imageId }
func pathToImage(_ image: FileResource) -> String {
return "/images/\(image.id)"
}
func imageLink(imageId: String) {
func image(_ imageId: String) -> FileResource? {
files.first { $0.id == imageId && $0.type.isImage }
}
func video(_ videoId: String) -> FileResource? {
files.first { $0.id == videoId && $0.type.isVideo }
}
func pathToVideo(_ videoId: String) -> String? {
guard let video = video(videoId) else {
return nil
}
return pathToVideo(video)
}
func pathToVideo(_ video: FileResource) -> String {
"/videos/\(video.id)"
}
func file(id: String) -> FileResource? {
files.first { $0.id == id }
}

View File

@ -25,6 +25,7 @@ extension Content {
private func convert(_ page: LocalizedPageFile, images: [String : FileResource]) -> LocalizedPage {
LocalizedPage(
content: self,
urlString: page.url,
title: page.title,
lastModified: page.lastModifiedDate,
@ -57,12 +58,24 @@ extension Content {
let pagesData = try storage.loadAllPages()
let postsData = try storage.loadAllPosts()
let fileList = try storage.loadAllFiles()
let externalFiles = try storage.loadExternalFileList()
let files: [String : FileResource] = fileList.reduce(into: [:]) { files, fileId in
var files: [String : FileResource] = fileList.reduce(into: [:]) { files, fileId in
let descriptions = imageDescriptions[fileId]
files[fileId] = FileResource(
content: self,
id: fileId,
isExternallyStored: false,
en: descriptions?.english ?? "",
de: descriptions?.german ?? "")
}
for fileId in externalFiles {
let descriptions = imageDescriptions[fileId]
files[fileId] = FileResource(
content: self,
id: fileId,
isExternallyStored: true,
en: descriptions?.english ?? "",
de: descriptions?.german ?? "")
}
@ -115,7 +128,8 @@ extension Content {
let pages = PageSettings(
pageUrlPrefix: settings.pages.pageUrlPrefix,
contentWidth: settings.pages.contentWidth)
contentWidth: settings.pages.contentWidth,
largeImageWidth: settings.pages.largeImageWidth)
return Settings(
outputDirectoryPath: settings.outputDirectoryPath,

View File

@ -29,6 +29,9 @@ extension Content {
try storage.save(fileDescriptions: fileDescriptions)
let externalFileList = files.filter { $0.isExternallyStored }.map { $0.id }
try storage.save(externalFileList: externalFileList)
do {
try storage.deletePostFiles(notIn: posts.map { $0.id })
try storage.deletePageFiles(notIn: pages.map { $0.id })
@ -116,7 +119,7 @@ private extension LocalizedTag {
name: name,
subtitle: subtitle,
description: description,
thumbnail: thumbnail?.id,
thumbnail: linkPreviewImage?.id,
originalURL: originalUrl)
}
}
@ -154,7 +157,8 @@ private extension PageSettings {
var file: PageSettingsFile {
.init(pageUrlPrefix: pageUrlPrefix,
contentWidth: contentWidth)
contentWidth: contentWidth,
largeImageWidth: largeImageWidth)
}
}

View File

@ -11,6 +11,9 @@ final class FileResource: ObservableObject {
@Published
var id: String
@Published
var isExternallyStored: Bool
@Published
var germanDescription: String
@ -20,12 +23,13 @@ final class FileResource: ObservableObject {
@Published
var size: CGSize = .zero
init(content: Content, id: String, en: String, de: String) {
init(content: Content, id: String, isExternallyStored: Bool, en: String, de: String) {
self.content = content
self.id = id
self.type = FileType(fileExtension: id.fileExtension)
self.englishDescription = en
self.germanDescription = de
self.isExternallyStored = isExternallyStored
}
/**
@ -37,6 +41,7 @@ final class FileResource: ObservableObject {
self.id = resourceImage
self.englishDescription = "A test image included in the bundle"
self.germanDescription = "Ein Testbild aus dem Bundle"
self.isExternallyStored = true
}
func getDescription(for language: ContentLanguage) -> String {

View File

@ -8,6 +8,8 @@ import SwiftUI
*/
final class LocalizedPage: ObservableObject {
unowned let content: Content
/**
The string to use when creating the url for the page.
@ -64,7 +66,8 @@ final class LocalizedPage: ObservableObject {
@Published
var linkPreviewDescription: String?
init(urlString: String,
init(content: Content,
urlString: String,
title: String,
lastModified: Date? = nil,
originalUrl: String? = nil,
@ -74,6 +77,7 @@ final class LocalizedPage: ObservableObject {
linkPreviewImage: FileResource? = nil,
linkPreviewTitle: String? = nil,
linkPreviewDescription: String? = nil) {
self.content = content
self.urlString = urlString
self.title = title
self.lastModified = lastModified

View File

@ -17,7 +17,7 @@ final class LocalizedTag: ObservableObject {
/// The image id of the thumbnail
@Published
var thumbnail: FileResource?
var linkPreviewImage: FileResource?
/// The original url in the previous site layout
let originalUrl: String?
@ -32,7 +32,7 @@ final class LocalizedTag: ObservableObject {
self.name = name
self.subtitle = subtitle
self.description = description
self.thumbnail = thumbnail
self.linkPreviewImage = thumbnail
self.originalUrl = originalUrl
}
}

View File

@ -98,6 +98,13 @@ extension Page: Hashable {
}
}
extension Page: Comparable {
static func < (lhs: Page, rhs: Page) -> Bool {
lhs.id < rhs.id
}
}
extension Page: DateItem {
}

View File

@ -10,8 +10,12 @@ final class PageSettings: ObservableObject {
@Published
var contentWidth: Int
init(pageUrlPrefix: String, contentWidth: Int) {
@Published
var largeImageWidth: Int
init(pageUrlPrefix: String, contentWidth: Int, largeImageWidth: Int) {
self.pageUrlPrefix = pageUrlPrefix
self.contentWidth = contentWidth
self.largeImageWidth = largeImageWidth
}
}