@@ -566,6 +566,62 @@ def _cov_media_src(node: dict[str, Any]) -> str | None:
566566 return None
567567
568568
569+ def _cov_fetch_media_bytes (src : str ) -> bytes | None :
570+ from app .services .assets import _fetch_bytes
571+
572+ try :
573+ data , _ctype = _fetch_bytes (src )
574+ return data if data and len (data ) >= 8 else None
575+ except Exception :
576+ return None
577+
578+
579+ def _cov_raster_image (node : dict [str , Any ]) -> bytes | None :
580+ """Host image tiles on storage — avoids CORS/auth issues with raw src URLs."""
581+ src = _cov_media_src (node )
582+ if not src :
583+ return None
584+ raw = _cov_fetch_media_bytes (src )
585+ if not raw :
586+ return None
587+ try :
588+ from io import BytesIO
589+
590+ from PIL import Image
591+ except Exception :
592+ return None
593+ attrs = node .get ("attrs" ) if isinstance (node .get ("attrs" ), dict ) else {}
594+ try :
595+ with Image .open (BytesIO (raw )) as im :
596+ im = im .convert ("RGBA" )
597+ crop_x = _cov_num (attrs .get ("cropX" ), - 1 )
598+ crop_y = _cov_num (attrs .get ("cropY" ), - 1 )
599+ crop_w = _cov_num (attrs .get ("cropW" ), - 1 )
600+ crop_h = _cov_num (attrs .get ("cropH" ), - 1 )
601+ if crop_w > 0 and crop_h > 0 :
602+ iw , ih = im .size
603+ left = max (0 , min (iw - 1 , int (round (crop_x * iw ))))
604+ top = max (0 , min (ih - 1 , int (round (crop_y * ih ))))
605+ right = max (left + 1 , min (iw , int (round ((crop_x + crop_w ) * iw ))))
606+ bottom = max (top + 1 , min (ih , int (round ((crop_y + crop_h ) * ih ))))
607+ im = im .crop ((left , top , right , bottom ))
608+ w , h = im .size
609+ if w < 1 or h < 1 :
610+ return None
611+ scale = min (1.0 , float (_COVER_EDGE ) / max (w , h ))
612+ out_w = max (32 , int (round (w * scale )))
613+ out_h = max (32 , int (round (h * scale )))
614+ if scale < 1.0 :
615+ im = im .resize ((out_w , out_h ), Image .Resampling .LANCZOS )
616+ rgb = Image .new ("RGB" , im .size , (255 , 255 , 255 ))
617+ rgb .paste (im , mask = im .split ()[3 ] if im .mode == "RGBA" else None )
618+ buf = BytesIO ()
619+ rgb .save (buf , format = "WEBP" , quality = 82 , method = 4 )
620+ return buf .getvalue ()
621+ except Exception :
622+ return None
623+
624+
569625def _cov_put_tile_bytes (
570626 user_id : str ,
571627 project_id : str ,
@@ -605,6 +661,9 @@ def _cov_node_cover_digest(node: dict[str, Any], blob: bytes | None) -> str:
605661 for key in ("fill-color" , "fill" , "shapeType" , "border-color" , "border-width" ):
606662 if key in attrs :
607663 parts .append (f"{ key } ={ attrs [key ]} " )
664+ for key in ("src" , "uploadKey" , "cropX" , "cropY" , "cropW" , "cropH" , "opacity" ):
665+ if key in attrs :
666+ parts .append (f"{ key } ={ attrs [key ]} " )
608667 if node .get ("fill" ):
609668 parts .append (f"fill={ node .get ('fill' )} " )
610669 if blob :
@@ -719,7 +778,18 @@ def _cov_tile_for_node(
719778 existing = existing_entries or []
720779 suffix = f"-{ index } " if index else ""
721780 key = str (node .get ("key" ) or "" )
722- if key in ("image" , "video" , "lottie" , "audio" ):
781+ if key == "image" :
782+ blob = _cov_raster_image (node )
783+ if blob :
784+ digest = _cov_node_cover_digest (node , blob )
785+ thumb_key = f"projects/{ user_id } /{ project_id } /thumb-{ digest } { suffix } .webp"
786+ if thumb_key in existing :
787+ return thumb_key
788+ return _cov_put_tile_bytes (
789+ user_id , project_id , blob , index = index , digest = digest
790+ )
791+ return None
792+ if key in ("video" , "lottie" , "audio" ):
723793 src = _cov_media_src (node )
724794 return src
725795 blob : bytes | None = None
@@ -1568,6 +1638,7 @@ def extract_project_covers(
15681638 )
15691639 prev = (existing .thumbnail_key or "" ).strip ()
15701640 nxt = (next_key or "" ).strip ()
1641+ force_ts = document is not None
15711642 if nxt != prev or bool (next_custom ) != bool (custom ):
15721643 thumb_key = next_key
15731644 custom = bool (next_custom )
@@ -1578,6 +1649,14 @@ def extract_project_covers(
15781649 thumbnail_custom = bool (next_custom ),
15791650 updated_at = now ,
15801651 )
1652+ elif force_ts and nxt :
1653+ crud .update_project_covers_by_id (
1654+ session = session ,
1655+ project_id = pid ,
1656+ thumbnail_key = thumb_key ,
1657+ thumbnail_custom = bool (custom ),
1658+ updated_at = now ,
1659+ )
15811660
15821661 return {
15831662 "id" : pid ,
0 commit comments