Files
openclaw/apps/shared/OpenClawKit/Sources/OpenClawKit/ChatImageProcessor.swift
Val Alexander faa443a452 fix(chat/ios): downscale image attachments before send
Resize iOS chat PhotosPicker image attachments through the shared JPEG transcoder before staging/sending. Cap long edge and payload bytes, strip source metadata, preserve previews from processed data, and add focused processor/view-model regression tests.\n\nFixes #68524.\nSupersedes #73710.
2026-05-13 21:44:05 -05:00

45 lines
1.5 KiB
Swift

import Foundation
/// Chat-specific image upload policy built on the shared JPEG transcoder.
public enum ChatImageProcessor {
public static let maxLongEdgePx = 1600
public static let jpegQuality = 0.8
public static let maxPayloadBytes = 3_500_000
public enum ProcessError: Error, LocalizedError, Sendable {
case notAnImage
case decodeFailed
case encodeFailed
public var errorDescription: String? {
switch self {
case .notAnImage:
"The data is not a recognizable image."
case .decodeFailed:
"The image could not be decoded."
case .encodeFailed:
"The image could not be resized to fit the chat upload limit."
}
}
}
public static func processForUpload(data: Data) throws -> Data {
do {
let result = try JPEGTranscoder.transcodeToJPEG(
imageData: data,
maxLongEdgePx: self.maxLongEdgePx,
quality: self.jpegQuality,
maxBytes: self.maxPayloadBytes)
return result.data
} catch JPEGTranscodeError.decodeFailed {
throw ProcessError.notAnImage
} catch JPEGTranscodeError.propertiesMissing {
throw ProcessError.decodeFailed
} catch JPEGTranscodeError.sizeLimitExceeded {
throw ProcessError.encodeFailed
} catch {
throw ProcessError.encodeFailed
}
}
}