Clean code for file type detection

This commit is contained in:
Christoph Hagen 2022-08-29 18:57:37 +02:00
parent 4c52232d24
commit 50519762a9
6 changed files with 47 additions and 54 deletions

View File

@ -22,7 +22,7 @@
E22E87AC289F1D3700E51191 /* Template.swift in Sources */ = {isa = PBXBuildFile; fileRef = E22E87AB289F1D3700E51191 /* Template.swift */; }; E22E87AC289F1D3700E51191 /* Template.swift in Sources */ = {isa = PBXBuildFile; fileRef = E22E87AB289F1D3700E51191 /* Template.swift */; };
E22E87AE289F1E0000E51191 /* String+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = E22E87AD289F1E0000E51191 /* String+Extensions.swift */; }; E22E87AE289F1E0000E51191 /* String+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = E22E87AD289F1E0000E51191 /* String+Extensions.swift */; };
E22E87B0289F221A00E51191 /* PrefilledTopBarTemplate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E22E87AF289F221A00E51191 /* PrefilledTopBarTemplate.swift */; }; E22E87B0289F221A00E51191 /* PrefilledTopBarTemplate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E22E87AF289F221A00E51191 /* PrefilledTopBarTemplate.swift */; };
E253C87728B767D50076B6D0 /* MediaType.swift in Sources */ = {isa = PBXBuildFile; fileRef = E253C87628B767D50076B6D0 /* MediaType.swift */; }; E253C87728B767D50076B6D0 /* ImageType.swift in Sources */ = {isa = PBXBuildFile; fileRef = E253C87628B767D50076B6D0 /* ImageType.swift */; };
E253C87A28B810090076B6D0 /* ImageOutput.swift in Sources */ = {isa = PBXBuildFile; fileRef = E253C87928B810090076B6D0 /* ImageOutput.swift */; }; E253C87A28B810090076B6D0 /* ImageOutput.swift in Sources */ = {isa = PBXBuildFile; fileRef = E253C87928B810090076B6D0 /* ImageOutput.swift */; };
E253C87C28B8BFB80076B6D0 /* FileSystem.swift in Sources */ = {isa = PBXBuildFile; fileRef = E253C87B28B8BFB80076B6D0 /* FileSystem.swift */; }; E253C87C28B8BFB80076B6D0 /* FileSystem.swift in Sources */ = {isa = PBXBuildFile; fileRef = E253C87B28B8BFB80076B6D0 /* FileSystem.swift */; };
E253C87F28B8FBB00076B6D0 /* Data+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = E253C87E28B8FBB00076B6D0 /* Data+Extensions.swift */; }; E253C87F28B8FBB00076B6D0 /* Data+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = E253C87E28B8FBB00076B6D0 /* Data+Extensions.swift */; };
@ -38,6 +38,7 @@
E2C5A5E328A037F900102A25 /* PageTemplate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2C5A5E228A037F900102A25 /* PageTemplate.swift */; }; E2C5A5E328A037F900102A25 /* PageTemplate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2C5A5E228A037F900102A25 /* PageTemplate.swift */; };
E2C5A5E528A03A6500102A25 /* BackNavigationTemplate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2C5A5E428A03A6500102A25 /* BackNavigationTemplate.swift */; }; E2C5A5E528A03A6500102A25 /* BackNavigationTemplate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2C5A5E428A03A6500102A25 /* BackNavigationTemplate.swift */; };
E2C5A5E928A0451C00102A25 /* LocalizedSiteTemplate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2C5A5E828A0451C00102A25 /* LocalizedSiteTemplate.swift */; }; E2C5A5E928A0451C00102A25 /* LocalizedSiteTemplate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2C5A5E828A0451C00102A25 /* LocalizedSiteTemplate.swift */; };
E2D4225128BD242200400E64 /* Configuration.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2D4225028BD242200400E64 /* Configuration.swift */; };
E2D55EDB28A2511D00B9453E /* OverviewSectionCleanTemplate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2D55EDA28A2511D00B9453E /* OverviewSectionCleanTemplate.swift */; }; E2D55EDB28A2511D00B9453E /* OverviewSectionCleanTemplate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2D55EDA28A2511D00B9453E /* OverviewSectionCleanTemplate.swift */; };
E2F8FA1E28A539C500632026 /* MarkdownProcessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2F8FA1D28A539C500632026 /* MarkdownProcessor.swift */; }; E2F8FA1E28A539C500632026 /* MarkdownProcessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2F8FA1D28A539C500632026 /* MarkdownProcessor.swift */; };
E2F8FA2028AB72D900632026 /* PlaceholderTemplate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2F8FA1F28AB72D900632026 /* PlaceholderTemplate.swift */; }; E2F8FA2028AB72D900632026 /* PlaceholderTemplate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2F8FA1F28AB72D900632026 /* PlaceholderTemplate.swift */; };
@ -83,7 +84,7 @@
E22E87AB289F1D3700E51191 /* Template.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Template.swift; sourceTree = "<group>"; }; E22E87AB289F1D3700E51191 /* Template.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Template.swift; sourceTree = "<group>"; };
E22E87AD289F1E0000E51191 /* String+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Extensions.swift"; sourceTree = "<group>"; }; E22E87AD289F1E0000E51191 /* String+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Extensions.swift"; sourceTree = "<group>"; };
E22E87AF289F221A00E51191 /* PrefilledTopBarTemplate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrefilledTopBarTemplate.swift; sourceTree = "<group>"; }; E22E87AF289F221A00E51191 /* PrefilledTopBarTemplate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrefilledTopBarTemplate.swift; sourceTree = "<group>"; };
E253C87628B767D50076B6D0 /* MediaType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MediaType.swift; sourceTree = "<group>"; }; E253C87628B767D50076B6D0 /* ImageType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageType.swift; sourceTree = "<group>"; };
E253C87928B810090076B6D0 /* ImageOutput.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageOutput.swift; sourceTree = "<group>"; }; E253C87928B810090076B6D0 /* ImageOutput.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageOutput.swift; sourceTree = "<group>"; };
E253C87B28B8BFB80076B6D0 /* FileSystem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileSystem.swift; sourceTree = "<group>"; }; E253C87B28B8BFB80076B6D0 /* FileSystem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileSystem.swift; sourceTree = "<group>"; };
E253C87E28B8FBB00076B6D0 /* Data+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Data+Extensions.swift"; sourceTree = "<group>"; }; E253C87E28B8FBB00076B6D0 /* Data+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Data+Extensions.swift"; sourceTree = "<group>"; };
@ -99,6 +100,7 @@
E2C5A5E228A037F900102A25 /* PageTemplate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PageTemplate.swift; sourceTree = "<group>"; }; E2C5A5E228A037F900102A25 /* PageTemplate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PageTemplate.swift; sourceTree = "<group>"; };
E2C5A5E428A03A6500102A25 /* BackNavigationTemplate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BackNavigationTemplate.swift; sourceTree = "<group>"; }; E2C5A5E428A03A6500102A25 /* BackNavigationTemplate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BackNavigationTemplate.swift; sourceTree = "<group>"; };
E2C5A5E828A0451C00102A25 /* LocalizedSiteTemplate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalizedSiteTemplate.swift; sourceTree = "<group>"; }; E2C5A5E828A0451C00102A25 /* LocalizedSiteTemplate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalizedSiteTemplate.swift; sourceTree = "<group>"; };
E2D4225028BD242200400E64 /* Configuration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Configuration.swift; sourceTree = "<group>"; };
E2D55EDA28A2511D00B9453E /* OverviewSectionCleanTemplate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OverviewSectionCleanTemplate.swift; sourceTree = "<group>"; }; E2D55EDA28A2511D00B9453E /* OverviewSectionCleanTemplate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OverviewSectionCleanTemplate.swift; sourceTree = "<group>"; };
E2F8FA1D28A539C500632026 /* MarkdownProcessor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MarkdownProcessor.swift; sourceTree = "<group>"; }; E2F8FA1D28A539C500632026 /* MarkdownProcessor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MarkdownProcessor.swift; sourceTree = "<group>"; };
E2F8FA1F28AB72D900632026 /* PlaceholderTemplate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlaceholderTemplate.swift; sourceTree = "<group>"; }; E2F8FA1F28AB72D900632026 /* PlaceholderTemplate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlaceholderTemplate.swift; sourceTree = "<group>"; };
@ -194,8 +196,9 @@
E2F8FA3928AE313A00632026 /* ValidationLog.swift */, E2F8FA3928AE313A00632026 /* ValidationLog.swift */,
E253C87928B810090076B6D0 /* ImageOutput.swift */, E253C87928B810090076B6D0 /* ImageOutput.swift */,
E253C87B28B8BFB80076B6D0 /* FileSystem.swift */, E253C87B28B8BFB80076B6D0 /* FileSystem.swift */,
E253C87628B767D50076B6D0 /* MediaType.swift */, E253C87628B767D50076B6D0 /* ImageType.swift */,
E2F8FA2728ACD84400632026 /* VideoType.swift */, E2F8FA2728ACD84400632026 /* VideoType.swift */,
E2D4225028BD242200400E64 /* Configuration.swift */,
); );
path = Files; path = Files;
sourceTree = "<group>"; sourceTree = "<group>";
@ -346,8 +349,9 @@
E22E87A4289F0C7000E51191 /* SiteGenerator.swift in Sources */, E22E87A4289F0C7000E51191 /* SiteGenerator.swift in Sources */,
E22E87AC289F1D3700E51191 /* Template.swift in Sources */, E22E87AC289F1D3700E51191 /* Template.swift in Sources */,
E22E87A0289F008200E51191 /* ThumbnailListGenerator.swift in Sources */, E22E87A0289F008200E51191 /* ThumbnailListGenerator.swift in Sources */,
E2D4225128BD242200400E64 /* Configuration.swift in Sources */,
E2F8FA3028AD450B00632026 /* PageState.swift in Sources */, E2F8FA3028AD450B00632026 /* PageState.swift in Sources */,
E253C87728B767D50076B6D0 /* MediaType.swift in Sources */, E253C87728B767D50076B6D0 /* ImageType.swift in Sources */,
E22E87B0289F221A00E51191 /* PrefilledTopBarTemplate.swift in Sources */, E22E87B0289F221A00E51191 /* PrefilledTopBarTemplate.swift in Sources */,
E22E87A8289F0E7B00E51191 /* PageGenerator.swift in Sources */, E22E87A8289F0E7B00E51191 /* PageGenerator.swift in Sources */,
E2C5A5E328A037F900102A25 /* PageTemplate.swift in Sources */, E2C5A5E328A037F900102A25 /* PageTemplate.swift in Sources */,

View File

@ -261,7 +261,8 @@ final class FileSystem {
// Ensure that image file is supported // Ensure that image file is supported
let ext = destinationUrl.pathExtension.lowercased() let ext = destinationUrl.pathExtension.lowercased()
guard MediaType.isProcessableImage(ext) else { guard ImageType(fileExtension: ext) != nil else {
// TODO: This should never be reached, since extensions are checked before
log.add(info: "Copying image", source: image.source) log.add(info: "Copying image", source: image.source)
do { do {
let sourceUrl = input.appendingPathComponent(image.source) let sourceUrl = input.appendingPathComponent(image.source)
@ -303,7 +304,7 @@ final class FileSystem {
} }
let destinationExtension = destinationUrl.pathExtension.lowercased() let destinationExtension = destinationUrl.pathExtension.lowercased()
guard let type = MediaType.supportedImage(destinationExtension) else { guard let type = ImageType(fileExtension: destinationExtension)?.fileType else {
log.add(error: "No image type for extension \(destinationExtension)", log.add(error: "No image type for extension \(destinationExtension)",
source: destination) source: destination)
return return

View File

@ -0,0 +1,27 @@
import Foundation
import AppKit
enum ImageType: CaseIterable {
case jpg
case png
init?(fileExtension: String) {
switch fileExtension {
case "jpg", "jpeg":
self = .jpg
case "png":
self = .png
default:
return nil
}
}
var fileType: NSBitmapImageRep.FileType {
switch self {
case .jpg:
return .jpeg
case .png:
return .png
}
}
}

View File

@ -1,39 +0,0 @@
import Foundation
import AppKit
private let supportedImageExtensions: [String : NSBitmapImageRep.FileType] = [
"jpg" : .jpeg,
"jpeg" : .jpeg,
"png" : .png,
]
private let supportedVideoExtensions: Set<String> = [
"mp4", "mov", "m4v"
]
enum MediaType {
case image
case video
case file
case svg
init(fileExtension: String) {
if supportedImageExtensions[fileExtension] != nil {
self = .image
} else if supportedVideoExtensions.contains(fileExtension) {
self = .video
} else if fileExtension == "svg" {
self = .svg
} else {
self = .file
}
}
static func isProcessableImage(_ fileExtension: String) -> Bool {
supportedImage(fileExtension) != nil
}
static func supportedImage(_ fileExtension: String) -> NSBitmapImageRep.FileType? {
supportedImageExtensions[fileExtension]
}
}

View File

@ -1,6 +1,6 @@
import Foundation import Foundation
enum VideoType: String { enum VideoType: String, CaseIterable {
case mp4 case mp4
case m4v case m4v

View File

@ -68,16 +68,16 @@ struct PageContentGenerator {
let title = fileAndTitle.contains(" ") ? fileAndTitle.dropBeforeFirst(" ").nonEmpty : nil let title = fileAndTitle.contains(" ") ? fileAndTitle.dropBeforeFirst(" ").nonEmpty : nil
let fileExtension = file.lastComponentAfter(".").lowercased() let fileExtension = file.lastComponentAfter(".").lowercased()
switch MediaType(fileExtension: fileExtension) { if let _ = ImageType(fileExtension: fileExtension) {
case .image:
return handleImage(page: page, file: file, rightTitle: title, leftTitle: alt) return handleImage(page: page, file: file, rightTitle: title, leftTitle: alt)
case .video:
return handleVideo(page: page, file: file, optionString: alt)
case .svg:
return handleSvg(page: page, file: file, area: alt)
case .file:
return handleFile(page: page, file: file, fileExtension: fileExtension)
} }
if let _ = VideoType(rawValue: fileExtension) {
return handleVideo(page: page, file: file, optionString: alt)
}
if fileExtension == "svg" {
return handleSvg(page: page, file: file, area: alt)
}
return handleFile(page: page, file: file, fileExtension: fileExtension)
} }
private func handleImage(page: Element, file: String, rightTitle: String?, leftTitle: String?) -> String { private func handleImage(page: Element, file: String, rightTitle: String?, leftTitle: String?) -> String {