From b3e4a79b2c59fe48184a362f0a4aa7c77e875193 Mon Sep 17 00:00:00 2001 From: Mauro Romito Date: Tue, 20 Oct 2026 20:47:24 +0200 Subject: [PATCH] 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. --- .../Media/MediaUploadingPreprocessor.swift | 8 +++-- .../MediaUploadingPreprocessorTests.swift | 34 +++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/ElementX/Sources/Services/Media/MediaUploadingPreprocessor.swift b/ElementX/Sources/Services/Media/MediaUploadingPreprocessor.swift index d94eccd3d..eac1a83e2 100644 --- a/ElementX/Sources/Services/Media/MediaUploadingPreprocessor.swift +++ b/ElementX/Sources/Services/Media/MediaUploadingPreprocessor.swift @@ -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.. 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 {