Explicit asset property

This commit is contained in:
Christoph Hagen 2025-02-17 13:38:48 +01:00
parent 0cc0f76254
commit 964c644922
6 changed files with 42 additions and 31 deletions

View File

@ -43,6 +43,10 @@ final class FileResource: Item, LocalizedItem {
@Published
var modifiedDate: Date
/// Identify this file as an asset to be placed in the `asset` folder on generation
@Published
var isAsset: Bool
/// The dimensions of the image
@Published
var imageDimensions: CGSize? = nil
@ -63,7 +67,8 @@ final class FileResource: Item, LocalizedItem {
generatedImageVersions: Set<String> = [],
customOutputPath: String? = nil,
addedDate: Date = .now,
modifiedDate: Date = .now) {
modifiedDate: Date = .now,
isAsset: Bool = false) {
self.type = FileType(fileExtension: id.fileExtension)
self.isExternallyStored = isExternallyStored
self.german = german
@ -74,6 +79,7 @@ final class FileResource: Item, LocalizedItem {
self.customOutputPath = customOutputPath
self.addedDate = addedDate
self.modifiedDate = modifiedDate
self.isAsset = isAsset
super.init(content: content, id: id)
}
@ -91,6 +97,7 @@ final class FileResource: Item, LocalizedItem {
self.customOutputPath = nil
self.addedDate = Date.now
self.modifiedDate = Date.now
self.isAsset = false
super.init(content: content, id: resourceImage)
}
@ -306,6 +313,9 @@ final class FileResource: Item, LocalizedItem {
}
private var pathPrefix: String {
if isAsset {
return content.settings.paths.assetsOutputFolderPath
}
if type.isImage {
return content.settings.paths.imagesOutputFolderPath
}
@ -315,9 +325,6 @@ final class FileResource: Item, LocalizedItem {
if type.isAudio {
return content.settings.paths.audioOutputFolderPath
}
if type.isAsset {
return content.settings.paths.assetsOutputFolderPath
}
return content.settings.paths.filesOutputFolderPath
}
@ -365,7 +372,8 @@ extension FileResource: StorageItem {
generatedImageVersions: Set(data.generatedImages ?? []),
customOutputPath: data.customOutputPath,
addedDate: data.addedDate,
modifiedDate: data.modifiedDate)
modifiedDate: data.modifiedDate,
isAsset: data.isAsset ?? false)
savedData = data
}
@ -378,7 +386,8 @@ extension FileResource: StorageItem {
version: version,
sourceUrl: sourceUrl,
addedDate: addedDate,
modifiedDate: modifiedDate)
modifiedDate: modifiedDate,
isAsset: isAsset ? true : nil)
}
/// This struct holds metadata about a file resource that is stored in the content folder.
@ -391,6 +400,7 @@ extension FileResource: StorageItem {
let sourceUrl: String?
let addedDate: Date
let modifiedDate: Date
let isAsset: Bool?
}
func saveToDisk(_ data: Data) -> Bool {

View File

@ -8,7 +8,6 @@ enum FileTypeCategory: String, CaseIterable {
case text
case video
case resource
case asset
case audio
var text: String {
@ -18,7 +17,6 @@ enum FileTypeCategory: String, CaseIterable {
case .model: return "Models"
case .text: return "Text"
case .video: return "Videos"
case .asset: return "Assets"
case .resource: return "Other"
case .audio: return "Audio"
}
@ -32,7 +30,6 @@ enum FileTypeCategory: String, CaseIterable {
case .text: .docText
case .video: .video
case .resource: .docZipper
case .asset: .network
case .audio: .speakerWave2CircleFill
}
}
@ -67,6 +64,8 @@ enum FileType: String {
case tiff
case ico
// MARK: Code
case html
@ -77,15 +76,9 @@ enum FileType: String {
case sh
// MARK: Assets
case css
case js
case ttf
case ico
case css
// MARK: Text
@ -131,6 +124,8 @@ enum FileType: String {
case noExtension
case ttf
case zip
case cddx
@ -163,21 +158,19 @@ enum FileType: String {
var category: FileTypeCategory {
switch self {
case .jpg, .png, .avif, .webp, .gif, .svg, .tiff:
case .jpg, .png, .avif, .webp, .gif, .svg, .tiff, .ico:
return .image
case .mp4, .m4v, .webm:
return .video
case .mp3, .aac, .m4b, .m4a:
return .audio
case .js, .css, .ttf, .ico:
return .asset
case .json, .conf, .yaml, .txt:
return .text
case .html, .cpp, .swift, .sh:
case .html, .cpp, .swift, .sh, .js, .css:
return .code
case .stl, .f3d, .step, .glb, .f3z:
return .model
case .zip, .cddx, .pdf, .key, .psd:
case .zip, .cddx, .pdf, .key, .psd, .ttf:
return .resource
case .noExtension, .unknown:
return .resource
@ -213,8 +206,11 @@ enum FileType: String {
category == .audio
}
var isAsset: Bool {
category == .asset
var isLikelyAssetType: Bool {
switch self {
case .js, .css, .ttf, .ico: true
default: false
}
}
var isTextFile: Bool {

View File

@ -39,7 +39,7 @@ struct FileContentView: View {
.font(.title)
}
.foregroundStyle(.secondary)
case .text, .code, .asset:
case .text, .code:
TextFileContentView(file: file)
.id(file.id)
case .video:

View File

@ -99,6 +99,11 @@ struct FileDetailView: View {
text: $file.customOutputPath,
footer: "A custom path where the file is stored in the output folder")
BoolPropertyView(
title: "Asset",
value: $file.isAsset,
footer: "Indicate that this file should be treated as an asset")
if let imageDimensions = file.imageDimensions {
GenericPropertyView(title: "Image dimensions") {
Text("\(Int(imageDimensions.width)) x \(Int(imageDimensions.height)) (\(file.aspectRatio))")

View File

@ -25,13 +25,13 @@ struct AudioSettingsDetailView: View {
title: "Audio Player CSS File",
footer: "The CSS file to provide the style for the audio player",
selectedFile: $audioPlayer.audioPlayerCssFile,
allowedType: .asset)
allowedType: .code)
FilePropertyView(
title: "Audio Player JavaScript File",
footer: "The CSS file to provide the functionality for the audio player",
selectedFile: $audioPlayer.audioPlayerJsFile,
allowedType: .asset)
allowedType: .code)
LocalizedAudioSettingsDetailView(settings: audioPlayer.localized(in: language))
.id(language)

View File

@ -30,32 +30,32 @@ struct PageSettingsDetailView: View {
title: "Default CSS File",
footer: "The CSS file containing the styling of all pages",
selectedFile: $pageSettings.defaultCssFile,
allowedType: .asset)
allowedType: .code)
FilePropertyView(
title: "Code Highlighting File",
footer: "The JavaScript file to provide syntax highlighting of code blocks",
selectedFile: $pageSettings.codeHighlightingJsFile,
allowedType: .asset)
allowedType: .code)
FilePropertyView(
title: "3D Model Viewer File",
footer: "The JavaScript file to provide the functionality for the 3D model viewer",
selectedFile: $pageSettings.modelViewerJsFile,
allowedType: .asset)
allowedType: .code)
FilePropertyView(
title: "Image Comparison CSS File",
footer: "The CSS file to provide image comparisons",
selectedFile: $pageSettings.imageCompareCssFile,
allowedType: .asset)
allowedType: .code)
FilePropertyView(
title: "Image Comparison JaveScript File",
footer: "The JavaScript file to provide image comparisons",
selectedFile: $pageSettings.imageCompareJsFile,
allowedType: .asset)
allowedType: .code)
LocalizedPageSettingsView(settings: pageSettings.localized(in: language))
.id(language)