@@ -7,7 +7,7 @@ use std::{
77use async_zip:: { Compression , ZipEntryBuilder , base:: write:: ZipFileWriter } ;
88use axum:: extract:: multipart:: Field ;
99use futures:: TryStreamExt ;
10- use headless_chrome:: protocol:: cdp:: Page :: { self , Viewport } ;
10+ use headless_chrome:: protocol:: cdp:: Page :: { self } ;
1111use image:: { EncodableLayout , ImageFormat } ;
1212use mime_guess:: mime:: IMAGE_PNG ;
1313use mongodb:: bson:: { Document , doc} ;
@@ -81,40 +81,24 @@ impl FileService {
8181 temp_file_path : & PathBuf ,
8282 ) -> Result < Option < String > , ServiceError > {
8383 let ( extension, thumb) = {
84- async fn chrome_proc ( temp_file_path : & Path ) -> Result < Vec < u8 > , ServiceError > {
84+ fn chrome_proc ( temp_file_path : & Path ) -> Result < Vec < u8 > , ServiceError > {
8585 // first try with chromium as it seems faster
8686 let tab = get_chromium_tab ( ) . map_err ( ServiceError :: new) ?;
8787 let file_url = format ! ( "file://{}" , temp_file_path. display( ) ) ;
8888 tab. navigate_to ( & file_url) . map_err ( ServiceError :: new) ?;
8989 tab. wait_until_navigated ( ) . map_err ( ServiceError :: new) ?;
9090
91- let first_page_height: f64 = tab
92- . evaluate ( "document.querySelector('body').scrollHeight" , false )
93- . map_err ( |e| ServiceError :: new ( e. to_string ( ) ) ) ?
94- . value
95- . ok_or_else ( || ServiceError :: new ( "missing height viewport" ) ) ?
96- . as_f64 ( )
97- . ok_or_else ( || ServiceError :: new ( "could not parse height viewport" ) ) ?;
98- let first_page_width: f64 = tab
99- . evaluate ( "document.querySelector('body').scrollWidth" , false )
100- . map_err ( |e| ServiceError :: new ( e. to_string ( ) ) ) ?
101- . value
102- . ok_or_else ( || ServiceError :: new ( "missing width viewport" ) ) ?
103- . as_f64 ( )
104- . ok_or_else ( || ServiceError :: new ( "could not parse width viewport" ) ) ?;
105-
106- // Capture screenshot with the specific viewport
91+ let viewport = tab
92+ . wait_for_element ( "body" )
93+ . map_err ( ServiceError :: new) ?
94+ . get_box_model ( )
95+ . map_err ( ServiceError :: new) ?
96+ . margin_viewport ( ) ; // Capture screenshot with the specific viewport
10797 let png_data = tab
10898 . capture_screenshot (
10999 Page :: CaptureScreenshotFormatOption :: Png ,
110100 None ,
111- Some ( Page :: Viewport {
112- x : 0. ,
113- y : 0. ,
114- width : first_page_width,
115- height : first_page_height,
116- scale : 1.0 ,
117- } ) ,
101+ Some ( viewport) ,
118102 true ,
119103 )
120104 . map_err ( ServiceError :: new) ?;
@@ -131,7 +115,7 @@ impl FileService {
131115 . map_err ( ServiceError :: new)
132116 . map ( |im| ( upl. content_type . clone ( ) , im) )
133117 } else {
134- match chrome_proc ( temp_file_path) . await . map ( |bytes| {
118+ match tokio :: task :: block_in_place ( || chrome_proc ( temp_file_path) ) . map ( |bytes| {
135119 image:: load_from_memory ( & bytes)
136120 . map_err ( ServiceError :: new)
137121 . map ( |im| ( Some ( IMAGE_PNG . to_string ( ) ) , im) )
0 commit comments