@@ -586,20 +586,33 @@ async def _precache_image_url(
586586 og: The Open Graph dictionary. This is modified with image information.
587587 """
588588 # If there's no image or it is blank, there's nothing to do.
589- if "og:image" not in og or not og ["og:image" ]:
589+ if "og:image" not in og :
590+ return
591+
592+ # Remove the raw image URL, this will be replaced with an MXC URL, if successful.
593+ image_url = og .pop ("og:image" )
594+ if not image_url :
590595 return
591596
592597 # The image URL from the HTML might be relative to the previewed page,
593598 # convert it to an URL which can be requested directly.
594- image_url = og ["og:image" ]
595599 url_parts = urlparse (image_url )
596600 if url_parts .scheme != "data" :
597601 image_url = urljoin (media_info .uri , image_url )
598602
599603 # FIXME: it might be cleaner to use the same flow as the main /preview_url
600604 # request itself and benefit from the same caching etc. But for now we
601605 # just rely on the caching on the master request to speed things up.
602- image_info = await self ._handle_url (image_url , user , allow_data_urls = True )
606+ try :
607+ image_info = await self ._handle_url (image_url , user , allow_data_urls = True )
608+ except Exception as e :
609+ # Pre-caching the image failed, don't block the entire URL preview.
610+ logger .warning (
611+ "Pre-caching image failed during URL preview: %s errored with %s" ,
612+ image_url ,
613+ e ,
614+ )
615+ return
603616
604617 if _is_media (image_info .media_type ):
605618 # TODO: make sure we don't choke on white-on-transparent images
@@ -611,13 +624,11 @@ async def _precache_image_url(
611624 og ["og:image:width" ] = dims ["width" ]
612625 og ["og:image:height" ] = dims ["height" ]
613626 else :
614- logger .warning ("Couldn't get dims for %s" , og [ "og:image" ] )
627+ logger .warning ("Couldn't get dims for %s" , image_url )
615628
616629 og ["og:image" ] = f"mxc://{ self .server_name } /{ image_info .filesystem_id } "
617630 og ["og:image:type" ] = image_info .media_type
618631 og ["matrix:image:size" ] = image_info .media_length
619- else :
620- del og ["og:image" ]
621632
622633 async def _handle_oembed_response (
623634 self , url : str , media_info : MediaInfo , expiration_ms : int
0 commit comments