Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
change approach to use contentHash
  • Loading branch information
NicoHinderling committed Dec 11, 2025
commit 7acd3736dd1c97808fd343a8301da6e9a5fceb54
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import CoreGraphics
import CryptoKit
import Foundation
import ImageIO
import UniformTypeIdentifiers
Expand Down Expand Up @@ -40,6 +41,7 @@ struct AssetCatalogEntry: Encodable {
let type: AssetType?
let idiom: String?
let colorspace: String?
let contentHash: String?
}

enum Error: Swift.Error {
Expand Down Expand Up @@ -112,7 +114,7 @@ enum AssetUtil {

let (structuredThemeStore, assetKeys) = initializeCatalog(from: file)

var images: [String: (cgImage: CGImage, format: String)] = [:]
var cgImages: [String: (cgImage: CGImage, format: String)] = [:]

// First pass: Build map of multisize sets and cache renditions for performance
var multisizeSets: [MultisizeSetInfo] = []
Expand Down Expand Up @@ -216,17 +218,22 @@ enum AssetUtil {
var width: Int?
var height: Int?
var unslicedImage: CGImage?
var contentHash: String? = nil

if isMultisizeImageSet {
continue
} else {
// Get image dimensions from regular rendition
(width, height, unslicedImage) = resolveImageDimensions(rendition, isVector)

// Skip SVGs, but save images even if they don't have an extension (default to png)
if fileExtension != "svg", let unslicedImage = unslicedImage {
// Compute content hash for PDFs/SVGs without saving to disk
if fileExtension == "pdf" || fileExtension == "svg" {
contentHash = data.sha256Hash()
}
// Save images that can be converted to CGImage (excluding PDFs/SVGs)
else if let unslicedImage = unslicedImage {
let format = fileExtension.isEmpty ? "png" : fileExtension
images[imageId] = (cgImage: unslicedImage, format: format)
cgImages[imageId] = (cgImage: unslicedImage, format: format)
}
}

Expand All @@ -251,7 +258,8 @@ enum AssetUtil {
filename: renditionTypeName,
type: assetType,
idiom: idiomToString(idiomValue),
colorspace: colorSpaceIDToString(colorSpaceID)
colorspace: colorSpaceIDToString(colorSpaceID),
contentHash: contentHash
)
assets.append(asset)
}
Expand All @@ -266,7 +274,8 @@ enum AssetUtil {
filename: nil,
type: nil,
idiom: nil,
colorspace: nil
colorspace: nil,
contentHash: nil
))

let data = try! JSONEncoder().encode(assets)
Expand All @@ -275,7 +284,7 @@ enum AssetUtil {
.appendingPathComponent("Assets")
.appendingPathExtension("json")
try! data.write(to: url, options: [])
for (id, imageInfo) in images {
for (id, imageInfo) in cgImages {
let format = imageInfo.format
let cgImage = imageInfo.cgImage
let fileURL = folder.appendingPathComponent(id).appendingPathExtension(format)
Expand Down Expand Up @@ -460,6 +469,17 @@ enum AssetUtil {
}
}

private extension Data {
func sha256Hash() -> String {
if #available(macOS 10.15, *) {
let digest = SHA256.hash(data: self)
return digest.map { String(format: "%02x", $0) }.joined()
}
// Fallback for older macOS (shouldn't happen with version 13+ requirement)
return ""
}
}

private extension NSObject {
func getUInt(forKey key: String) -> UInt? {
if let result = perform(Selector(key)) {
Expand Down