mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-22 23:39:59 +00:00
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.
45 lines
1.5 KiB
Swift
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
|
|
}
|
|
}
|
|
}
|