Strip location data when any image in a file has it

Only the first image was checked for GPS before stripping, so a file
whose later images carried a location, but not its first, was sent
unchanged with that location. Every image is checked now.

GPS stored only in XMP was already caught, because ImageIO reports it
in the image's GPS properties.
This commit is contained in:
Mauro Romito
2026-10-20 20:47:24 +02:00
committed by Mauro
parent 3a4a7e7cf9
commit b3e4a79b2c
2 changed files with 39 additions and 3 deletions
@@ -374,13 +374,15 @@ nonisolated struct MediaUploadingPreprocessor {
throw .failedStrippingLocationData
}
guard let originalMetadata = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil),
(originalMetadata as NSDictionary).value(forKeyPath: "\(kCGImagePropertyGPSDictionary)") != nil else {
let count = CGImageSourceGetCount(imageSource)
let hasLocation = (0..<count).contains { index in
(CGImageSourceCopyPropertiesAtIndex(imageSource, index, nil) as? [CFString: Any])?[kCGImagePropertyGPSDictionary] != nil
}
guard hasLocation else {
MXLog.info("No GPS metadata found. Nothing to do.")
return
}
let count = CGImageSourceGetCount(imageSource)
let metadataKeysToRemove: [CFString: Any] = [kCGImagePropertyGPSDictionary: kCFNull as Any]
let data = NSMutableData()
@@ -376,6 +376,17 @@ final class MediaUploadingPreprocessorTests {
#expect(CGImageSourceCopyAuxiliaryDataInfoAtIndex(imageSource, 2, kCGImageAuxiliaryDataTypeHDRGainMap) == nil)
}
@Test
func multiImageProcessingStripsLocationFoundOnlyOnLaterImage() async throws {
let imageSource = try await processedImageSource(at: makeImageWithLocationOnSecondImage())
#expect(CGImageSourceGetCount(imageSource) == 2)
for index in 0..<CGImageSourceGetCount(imageSource) {
let metadata = try #require(CGImageSourceCopyPropertiesAtIndex(imageSource, index, nil) as NSDictionary?)
#expect(metadata.value(forKeyPath: "\(kCGImagePropertyGPSDictionary)") == nil)
}
}
@Test
func spatialImageProcessingKeepsStereoPair() async throws {
let imageSource = try await processedImageSource(at: makeSpatialImageWithLocation())
@@ -673,6 +684,29 @@ final class MediaUploadingPreprocessorTests {
return url
}
/// Generates a two image HEIC with GPS metadata on its second image only in a unique temporary directory.
private func makeImageWithLocationOnSecondImage() throws -> URL {
let directory = FileManager.default.temporaryDirectory.appendingPathComponent("\(Self.self)-\(UUID().uuidString)")
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
let url = directory.appendingPathComponent("second_image_location_test_image.heic")
let extent = CGRect(x: 0, y: 0, width: 64, height: 64)
let image = try #require(CIContext().createCGImage(CIImage(color: .gray).cropped(to: extent), from: extent))
let destination = try #require(CGImageDestinationCreateWithURL(url as NSURL, UTType.heic.identifier as CFString, 2, nil))
CGImageDestinationAddImage(destination, image, nil)
let location = [kCGImagePropertyGPSDictionary: [kCGImagePropertyGPSLatitude: 51.5, kCGImagePropertyGPSLongitude: 0.1]]
CGImageDestinationAddImage(destination, image, location as NSDictionary)
try #require(CGImageDestinationFinalize(destination))
let imageSource = try #require(CGImageSourceCreateWithURL(url as NSURL, nil))
let firstMetadata = try #require(CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as NSDictionary?)
let secondMetadata = try #require(CGImageSourceCopyPropertiesAtIndex(imageSource, 1, nil) as NSDictionary?)
try #require(firstMetadata.value(forKeyPath: "\(kCGImagePropertyGPSDictionary)") == nil)
try #require(secondMetadata.value(forKeyPath: "\(kCGImagePropertyGPSDictionary)") != nil)
return url
}
/// Generates a HEIC laid out like an iPhone spatial photo in a unique temporary directory: a monoscopic primary image, then the right and
/// left eyes, all in one stereo pair group. Every image has GPS metadata and an HDR gain map of its own size, and each eye has its camera.
private func makeSpatialImageWithLocation() throws -> URL {