Skip to content
Closed
Changes from all commits
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
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 @@ -78,6 +79,7 @@ enum AssetUtil {
let (structuredThemeStore, assetKeys) = initializeCatalog(from: file)

var images: [String: CGImage] = [:]
var seenImageKeys: [String: String] = [:] // SHA256 -> imageId mapping
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem right to me, where is an equivalent mapping used in the emerge code?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had some misunderstanding , we do logic to set the isDuplicate flag, but we don't actually do de-dupping in the parsing. The main misunderstanding I had was around how we will no longer be doing app thinning automatically for users, so I think I'm going to close this PR for now


for key in assetKeys {
let keyList = unsafeBitCast(
Expand Down Expand Up @@ -128,8 +130,17 @@ enum AssetUtil {
let isVector = type == 9
let (width, height, unslicedImage) = resolveImageDimensions(rendition, isVector)
let assetType = determineAssetType(key)
let imageId = UUID().uuidString
images[imageId] = unslicedImage

let imageKey = generateImageKey(from: rendition, className: className, data: data)

let imageId: String
if let existingImageId = seenImageKeys[imageKey] {
continue
} else {
imageId = UUID().uuidString
seenImageKeys[imageKey] = imageId
images[imageId] = unslicedImage
}

let asset = AssetCatalogEntry(
imageId: imageId,
Expand Down Expand Up @@ -293,6 +304,26 @@ enum AssetUtil {

return (width, height, unslicedImage)
}

private static func generateImageKey(from rendition: NSObject, className: String, data: Data) -> String {
if let unslicedImage = rendition.perform(Selector(("unslicedImage"))) {
let image = unslicedImage.takeUnretainedValue() as! CGImage
if let imageData = image.dataProvider?.data as? Data {
return imageData.sha256()
}
} else if className == "_CUIThemePDFRendition" {
return data.sha256()
}

return data.sha256()
}
}

extension Data {
func sha256() -> String {
let digest = SHA256.hash(data: self)
return digest.compactMap { String(format: "%02x", $0) }.joined()
}
}

private extension NSObject {
Expand Down