From 50519762a9352116754a782190399470403174de Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Mon, 29 Aug 2022 18:57:37 +0200 Subject: [PATCH] Clean code for file type detection --- WebsiteGenerator.xcodeproj/project.pbxproj | 12 ++++-- WebsiteGenerator/Files/FileSystem.swift | 5 ++- WebsiteGenerator/Files/ImageType.swift | 27 +++++++++++++ WebsiteGenerator/Files/MediaType.swift | 39 ------------------- WebsiteGenerator/Files/VideoType.swift | 2 +- .../Generators/MarkdownProcessor.swift | 16 ++++---- 6 files changed, 47 insertions(+), 54 deletions(-) create mode 100644 WebsiteGenerator/Files/ImageType.swift delete mode 100644 WebsiteGenerator/Files/MediaType.swift diff --git a/WebsiteGenerator.xcodeproj/project.pbxproj b/WebsiteGenerator.xcodeproj/project.pbxproj index a87d955..d6770de 100644 --- a/WebsiteGenerator.xcodeproj/project.pbxproj +++ b/WebsiteGenerator.xcodeproj/project.pbxproj @@ -22,7 +22,7 @@ E22E87AC289F1D3700E51191 /* Template.swift in Sources */ = {isa = PBXBuildFile; fileRef = E22E87AB289F1D3700E51191 /* Template.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 */; }; - 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 */; }; E253C87C28B8BFB80076B6D0 /* FileSystem.swift in Sources */ = {isa = PBXBuildFile; fileRef = E253C87B28B8BFB80076B6D0 /* FileSystem.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 */; }; E2C5A5E528A03A6500102A25 /* BackNavigationTemplate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2C5A5E428A03A6500102A25 /* BackNavigationTemplate.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 */; }; E2F8FA1E28A539C500632026 /* MarkdownProcessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2F8FA1D28A539C500632026 /* MarkdownProcessor.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 = ""; }; E22E87AD289F1E0000E51191 /* String+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Extensions.swift"; sourceTree = ""; }; E22E87AF289F221A00E51191 /* PrefilledTopBarTemplate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrefilledTopBarTemplate.swift; sourceTree = ""; }; - E253C87628B767D50076B6D0 /* MediaType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MediaType.swift; sourceTree = ""; }; + E253C87628B767D50076B6D0 /* ImageType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageType.swift; sourceTree = ""; }; E253C87928B810090076B6D0 /* ImageOutput.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageOutput.swift; sourceTree = ""; }; E253C87B28B8BFB80076B6D0 /* FileSystem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileSystem.swift; sourceTree = ""; }; E253C87E28B8FBB00076B6D0 /* Data+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Data+Extensions.swift"; sourceTree = ""; }; @@ -99,6 +100,7 @@ E2C5A5E228A037F900102A25 /* PageTemplate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PageTemplate.swift; sourceTree = ""; }; E2C5A5E428A03A6500102A25 /* BackNavigationTemplate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BackNavigationTemplate.swift; sourceTree = ""; }; E2C5A5E828A0451C00102A25 /* LocalizedSiteTemplate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalizedSiteTemplate.swift; sourceTree = ""; }; + E2D4225028BD242200400E64 /* Configuration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Configuration.swift; sourceTree = ""; }; E2D55EDA28A2511D00B9453E /* OverviewSectionCleanTemplate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OverviewSectionCleanTemplate.swift; sourceTree = ""; }; E2F8FA1D28A539C500632026 /* MarkdownProcessor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MarkdownProcessor.swift; sourceTree = ""; }; E2F8FA1F28AB72D900632026 /* PlaceholderTemplate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlaceholderTemplate.swift; sourceTree = ""; }; @@ -194,8 +196,9 @@ E2F8FA3928AE313A00632026 /* ValidationLog.swift */, E253C87928B810090076B6D0 /* ImageOutput.swift */, E253C87B28B8BFB80076B6D0 /* FileSystem.swift */, - E253C87628B767D50076B6D0 /* MediaType.swift */, + E253C87628B767D50076B6D0 /* ImageType.swift */, E2F8FA2728ACD84400632026 /* VideoType.swift */, + E2D4225028BD242200400E64 /* Configuration.swift */, ); path = Files; sourceTree = ""; @@ -346,8 +349,9 @@ E22E87A4289F0C7000E51191 /* SiteGenerator.swift in Sources */, E22E87AC289F1D3700E51191 /* Template.swift in Sources */, E22E87A0289F008200E51191 /* ThumbnailListGenerator.swift in Sources */, + E2D4225128BD242200400E64 /* Configuration.swift in Sources */, E2F8FA3028AD450B00632026 /* PageState.swift in Sources */, - E253C87728B767D50076B6D0 /* MediaType.swift in Sources */, + E253C87728B767D50076B6D0 /* ImageType.swift in Sources */, E22E87B0289F221A00E51191 /* PrefilledTopBarTemplate.swift in Sources */, E22E87A8289F0E7B00E51191 /* PageGenerator.swift in Sources */, E2C5A5E328A037F900102A25 /* PageTemplate.swift in Sources */, diff --git a/WebsiteGenerator/Files/FileSystem.swift b/WebsiteGenerator/Files/FileSystem.swift index 4f9a957..3b94cc3 100644 --- a/WebsiteGenerator/Files/FileSystem.swift +++ b/WebsiteGenerator/Files/FileSystem.swift @@ -261,7 +261,8 @@ final class FileSystem { // Ensure that image file is supported 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) do { let sourceUrl = input.appendingPathComponent(image.source) @@ -303,7 +304,7 @@ final class FileSystem { } 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)", source: destination) return diff --git a/WebsiteGenerator/Files/ImageType.swift b/WebsiteGenerator/Files/ImageType.swift new file mode 100644 index 0000000..f138f9b --- /dev/null +++ b/WebsiteGenerator/Files/ImageType.swift @@ -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 + } + } +} diff --git a/WebsiteGenerator/Files/MediaType.swift b/WebsiteGenerator/Files/MediaType.swift deleted file mode 100644 index ae306aa..0000000 --- a/WebsiteGenerator/Files/MediaType.swift +++ /dev/null @@ -1,39 +0,0 @@ -import Foundation -import AppKit - -private let supportedImageExtensions: [String : NSBitmapImageRep.FileType] = [ - "jpg" : .jpeg, - "jpeg" : .jpeg, - "png" : .png, -] - -private let supportedVideoExtensions: Set = [ - "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] - } -} diff --git a/WebsiteGenerator/Files/VideoType.swift b/WebsiteGenerator/Files/VideoType.swift index 3a85664..ab5105c 100644 --- a/WebsiteGenerator/Files/VideoType.swift +++ b/WebsiteGenerator/Files/VideoType.swift @@ -1,6 +1,6 @@ import Foundation -enum VideoType: String { +enum VideoType: String, CaseIterable { case mp4 case m4v diff --git a/WebsiteGenerator/Generators/MarkdownProcessor.swift b/WebsiteGenerator/Generators/MarkdownProcessor.swift index ce586f0..0a10406 100644 --- a/WebsiteGenerator/Generators/MarkdownProcessor.swift +++ b/WebsiteGenerator/Generators/MarkdownProcessor.swift @@ -68,16 +68,16 @@ struct PageContentGenerator { let title = fileAndTitle.contains(" ") ? fileAndTitle.dropBeforeFirst(" ").nonEmpty : nil let fileExtension = file.lastComponentAfter(".").lowercased() - switch MediaType(fileExtension: fileExtension) { - case .image: + if let _ = ImageType(fileExtension: fileExtension) { 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 {