@@ -22,13 +22,15 @@ final class ShareViewController: UIViewController {
2222 private struct ExtractedShareContent {
2323 var payload : SharedContentPayload
2424 var attachments : [ LoadedAttachment ]
25+ var attachmentError : ShareImageProcessor . ProcessError ?
2526 }
2627
2728 private let logger = Logger ( subsystem : " ai.openclawfoundation.app " , category : " ShareExtension " )
2829 private let composeView = ShareComposeView ( )
2930 private var didPrepareDraft = false
3031 private var isSending = false
3132 private var pendingAttachments : [ ShareAttachment ] = [ ]
33+ private var attachmentError : ShareImageProcessor . ProcessError ?
3234
3335 override func loadView ( ) {
3436 self . view = self . composeView
@@ -58,6 +60,7 @@ final class ShareViewController: UIViewController {
5860 let extracted = await self . extractSharedContent ( )
5961 let payload = extracted . payload
6062 self . pendingAttachments = extracted . attachments . map ( \. payload )
63+ self . attachmentError = extracted . attachmentError
6164 self . logger . info ( " share payload trace= \( traceId , privacy : . public ) " )
6265 self . logger . info (
6366 " share payload title= \( payload . title ? . count ?? 0 ) text= \( payload . text ? . count ?? 0 ) " )
@@ -66,11 +69,15 @@ final class ShareViewController: UIViewController {
6669 let message = ShareDraftComposer . compose ( from : payload )
6770 self . composeView . setDraft ( message )
6871 self . composeView . setAttachmentPreviews ( extracted . attachments . map ( \. preview ) )
69- self . composeView . apply ( . ready )
7072 self . composeView . focusDraft ( )
71- if message . trimmingCharacters ( in : . whitespacesAndNewlines ) . isEmpty {
73+ if let attachmentError = extracted . attachmentError {
74+ ShareGatewayRelaySettings . saveLastEvent ( " Share blocked: image processing failed. " )
75+ self . composeView . apply ( . blocked ( self . imageProcessingErrorMessage ( attachmentError ) ) )
76+ } else if message . trimmingCharacters ( in : . whitespacesAndNewlines ) . isEmpty {
77+ self . composeView . apply ( . ready )
7278 ShareGatewayRelaySettings . saveLastEvent ( " Share ready: waiting for message input. " )
7379 } else {
80+ self . composeView . apply ( . ready )
7481 ShareGatewayRelaySettings . saveLastEvent ( " Share ready: draft prepared. " )
7582 }
7683 }
@@ -81,6 +88,10 @@ final class ShareViewController: UIViewController {
8188 }
8289
8390 private func sendCurrentDraft ( ) async {
91+ if let attachmentError = self . attachmentError {
92+ self . composeView . apply ( . blocked ( self . imageProcessingErrorMessage ( attachmentError ) ) )
93+ return
94+ }
8495 let trimmed = self . composeView . draftText . trimmingCharacters ( in : . whitespacesAndNewlines )
8596 guard ! trimmed . isEmpty else {
8697 ShareGatewayRelaySettings . saveLastEvent ( " Share blocked: message is empty. " )
@@ -270,14 +281,16 @@ final class ShareViewController: UIViewController {
270281 guard let items = self . extensionContext ? . inputItems as? [ NSExtensionItem ] else {
271282 return ExtractedShareContent (
272283 payload : SharedContentPayload ( title : nil , url : nil , text : nil ) ,
273- attachments : [ ] )
284+ attachments : [ ] ,
285+ attachmentError : nil )
274286 }
275287
276288 var title : String ?
277289 var sharedURL : URL ?
278290 var sharedText : String ?
279291 var attributedContentText : String ?
280292 var attachments : [ LoadedAttachment ] = [ ]
293+ var attachmentError : ShareImageProcessor . ProcessError ?
281294 let maxImageAttachments = 3
282295
283296 for item in items {
@@ -297,11 +310,19 @@ final class ShareViewController: UIViewController {
297310 sharedText = await self . loadText ( from : provider )
298311 }
299312
300- if provider . hasItemConformingToTypeIdentifier ( UTType . image . identifier ) ,
301- attachments . count < maxImageAttachments ,
302- let attachment = await self . loadImageAttachment ( from : provider , index : attachments . count )
303- {
304- attachments . append ( attachment )
313+ if provider . hasItemConformingToTypeIdentifier ( UTType . image . identifier ) {
314+ if attachments . count < maxImageAttachments , attachmentError == nil {
315+ do {
316+ let attachment = try await self . loadImageAttachment (
317+ from : provider ,
318+ index : attachments . count )
319+ attachments . append ( attachment )
320+ } catch let error as ShareImageProcessor . ProcessError {
321+ attachmentError = error
322+ } catch {
323+ attachmentError = . encodeFailed
324+ }
325+ }
305326 }
306327 }
307328 }
@@ -314,20 +335,21 @@ final class ShareViewController: UIViewController {
314335 sharedURL : sharedURL )
315336 return ExtractedShareContent (
316337 payload : SharedContentPayload ( title : title ?? supplementalTitle , url : sharedURL , text : sharedText ) ,
317- attachments : attachments )
338+ attachments : attachments ,
339+ attachmentError : attachmentError )
318340 }
319341
320- private func loadImageAttachment ( from provider : NSItemProvider , index : Int ) async -> LoadedAttachment ? {
342+ private func loadImageAttachment ( from provider : NSItemProvider , index : Int ) async throws -> LoadedAttachment {
321343 let imageUTI = self . preferredImageTypeIdentifier ( from : provider ) ?? UTType . image . identifier
322344 guard let rawData = await self . loadDataValue ( from : provider , typeIdentifier : imageUTI ) else {
323- return nil
345+ throw ShareImageProcessor . ProcessError . invalidImage
324346 }
325347
326- let maxBytes = 5_000_000
327- guard let image = UIImage ( data : rawData ) ,
328- let data = self . normalizedJPEGData ( from : image , maxBytes : maxBytes )
329- else {
330- return nil
348+ let data = try await Task . detached ( priority : . userInitiated ) {
349+ try ShareImageProcessor . processForUpload ( data : rawData )
350+ } . value
351+ guard let image = UIImage ( data : data ) else {
352+ throw ShareImageProcessor . ProcessError . invalidImage
331353 }
332354
333355 return await LoadedAttachment (
@@ -339,6 +361,12 @@ final class ShareViewController: UIViewController {
339361 preview : self . boundedPreview ( from : image ) )
340362 }
341363
364+ private func imageProcessingErrorMessage ( _ : ShareImageProcessor . ProcessError ) -> String {
365+ NSLocalizedString (
366+ " The shared image could not be prepared. " ,
367+ comment : " Share extension image processing failure " )
368+ }
369+
342370 /// Previews are retained for the sheet's lifetime; keep them bounded so
343371 /// three full-resolution photos cannot blow the extension memory cap.
344372 /// Never falls back to the full-size image.
@@ -369,19 +397,6 @@ final class ShareViewController: UIViewController {
369397 return nil
370398 }
371399
372- private func normalizedJPEGData ( from image : UIImage , maxBytes : Int ) -> Data ? {
373- var quality : CGFloat = 0.9
374- while quality >= 0.4 {
375- if let data = image . jpegData ( compressionQuality : quality ) , data . count <= maxBytes {
376- return data
377- }
378- quality -= 0.1
379- }
380- guard let fallback = image . jpegData ( compressionQuality : 0.35 ) else { return nil }
381- if fallback . count <= maxBytes { return fallback }
382- return nil
383- }
384-
385400 private func loadURL ( from provider : NSItemProvider ) async -> URL ? {
386401 if provider . hasItemConformingToTypeIdentifier ( UTType . url . identifier ) {
387402 if let url = await self . loadURLValue (
0 commit comments