Consolidate images and files
This commit is contained in:
@ -15,14 +15,14 @@ private struct ImageJob {
|
||||
|
||||
let quality: CGFloat
|
||||
|
||||
let type: ImageType
|
||||
let type: ImageFileType
|
||||
}
|
||||
|
||||
final class ImageGenerator {
|
||||
|
||||
private let storage: Storage
|
||||
|
||||
private let inputImageFolder: URL
|
||||
//private let inputImageFolder: URL
|
||||
|
||||
private let relativeImageOutputPath: String
|
||||
|
||||
@ -30,9 +30,8 @@ final class ImageGenerator {
|
||||
|
||||
private var jobs: [ImageJob] = []
|
||||
|
||||
init(storage: Storage, inputImageFolder: URL, relativeImageOutputPath: String) {
|
||||
init(storage: Storage, relativeImageOutputPath: String) {
|
||||
self.storage = storage
|
||||
self.inputImageFolder = inputImageFolder
|
||||
self.relativeImageOutputPath = relativeImageOutputPath
|
||||
self.generatedImages = storage.loadListOfGeneratedImages()
|
||||
}
|
||||
@ -64,14 +63,14 @@ final class ImageGenerator {
|
||||
storage.save(listOfGeneratedImages: generatedImages)
|
||||
}
|
||||
|
||||
private func versionFileName(image: String, type: ImageType, width: CGFloat, height: CGFloat) -> String {
|
||||
private func versionFileName(image: String, type: ImageFileType, width: CGFloat, height: CGFloat) -> String {
|
||||
let fileName = image.fileNameAndExtension.fileName
|
||||
let prefix = "\(fileName)@\(Int(width))x\(Int(height))"
|
||||
return "\(prefix).\(type.fileExtension)"
|
||||
}
|
||||
|
||||
func generateImageSet(for image: String, maxWidth: CGFloat, maxHeight: CGFloat, altText: String) -> FeedEntryData.Image {
|
||||
let type = ImageType(fileExtension: image.fileExtension!)!
|
||||
let type = ImageFileType(fileExtension: image.fileExtension!)!
|
||||
|
||||
let width2x = maxWidth * 2
|
||||
let height2x = maxHeight * 2
|
||||
@ -92,7 +91,7 @@ final class ImageGenerator {
|
||||
altText: altText)
|
||||
}
|
||||
|
||||
func generateVersion(for image: String, type: ImageType, maximumWidth: CGFloat, maximumHeight: CGFloat) -> String {
|
||||
func generateVersion(for image: String, type: ImageFileType, maximumWidth: CGFloat, maximumHeight: CGFloat) -> String {
|
||||
let version = versionFileName(image: image, type: type, width: maximumWidth, height: maximumHeight)
|
||||
let fullPath = "/" + relativeImageOutputPath + "/" + version
|
||||
if exists(version) {
|
||||
@ -142,18 +141,12 @@ final class ImageGenerator {
|
||||
if hasPreviouslyGenerated(version: job.version, for: job.image), exists(job.version) {
|
||||
return true
|
||||
}
|
||||
let inputPath = inputImageFolder.appendingPathComponent(job.image)
|
||||
#warning("TODO: Read through security scope")
|
||||
guard inputPath.exists else {
|
||||
print("Missing image \(inputPath.path())")
|
||||
return false
|
||||
}
|
||||
|
||||
let data: Data
|
||||
do {
|
||||
data = try Data(contentsOf: inputPath)
|
||||
data = try storage.fileData(for: job.image)
|
||||
} catch {
|
||||
print("Failed to load image \(inputPath.path()): \(error)")
|
||||
print("Failed to load image \(job.image): \(error)")
|
||||
return false
|
||||
}
|
||||
|
||||
@ -231,7 +224,7 @@ final class ImageGenerator {
|
||||
|
||||
// MARK: Avif images
|
||||
|
||||
private func create(image: NSBitmapImageRep, type: ImageType, quality: CGFloat) -> Data? {
|
||||
private func create(image: NSBitmapImageRep, type: ImageFileType, quality: CGFloat) -> Data? {
|
||||
switch type {
|
||||
case .jpg:
|
||||
return image.representation(using: .jpeg, properties: [.compressionFactor: NSNumber(value: 0.6)])
|
||||
@ -243,6 +236,10 @@ final class ImageGenerator {
|
||||
return createWebp(image: image, quality: 0.8)
|
||||
case .gif:
|
||||
return image.representation(using: .gif, properties: [.compressionFactor: NSNumber(value: quality)])
|
||||
case .svg:
|
||||
return nil
|
||||
case .tiff:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,7 +2,7 @@ import Foundation
|
||||
import Ink
|
||||
import Splash
|
||||
|
||||
typealias VideoSource = (url: String, type: VideoType)
|
||||
typealias VideoSource = (url: String, type: VideoFileType)
|
||||
|
||||
final class PageContentParser {
|
||||
|
||||
|
@ -50,7 +50,6 @@ final class WebsiteGenerator {
|
||||
self.localizedSettings = content.settings.localized(in: language)
|
||||
self.imageGenerator = ImageGenerator(
|
||||
storage: content.storage,
|
||||
inputImageFolder: content.storage.filesFolder,
|
||||
relativeImageOutputPath: "images")
|
||||
}
|
||||
|
||||
@ -96,7 +95,7 @@ final class WebsiteGenerator {
|
||||
navigationItems: navigationItems)
|
||||
}
|
||||
|
||||
private func createImageSet(for image: ImageResource) -> FeedEntryData.Image {
|
||||
private func createImageSet(for image: FileResource) -> FeedEntryData.Image {
|
||||
imageGenerator.generateImageSet(
|
||||
for: image.id,
|
||||
maxWidth: mainContentMaximumWidth,
|
||||
|
Reference in New Issue
Block a user