11use crate :: {
2- db:: { self , main:: area:: schema:: Area } ,
2+ db:: { self , image :: ImagePool , main:: area:: schema:: Area } ,
33 Result ,
44} ;
55use base64:: prelude:: * ;
@@ -41,7 +41,7 @@ impl From<Area> for Res {
4141 }
4242}
4343
44- pub async fn run ( params : Params , pool : & Pool ) -> Result < Res > {
44+ pub async fn run ( params : Params , pool : & Pool , image_pool : & ImagePool ) -> Result < Res > {
4545 let area = db:: main:: area:: queries:: select_by_id_or_alias ( & params. id , pool) . await ?;
4646 let file_name = format ! ( "{}.{}" , area. id, params. icon_ext) ;
4747 let bytes = BASE64_STANDARD . decode ( params. icon_base64 ) ?;
@@ -54,6 +54,49 @@ pub async fn run(params: Params, pool: &Pool) -> Result<Res> {
5454 ) ) ?;
5555 file. write_all ( & bytes) ?;
5656 file. flush ( ) ?;
57+
58+ let dims = actix_web:: web:: block ( {
59+ let bytes = bytes. clone ( ) ;
60+ move || super :: generate_area_icons:: decode_dimensions ( & bytes)
61+ } )
62+ . await ?;
63+
64+ if let Some ( ( width, height) ) = dims {
65+ let bytes_len = bytes. len ( ) as i64 ;
66+ let existing =
67+ db:: image:: area:: queries:: select_by_area_id_and_type ( area. id , "square" , image_pool)
68+ . await ?;
69+
70+ if let Some ( existing) = & existing {
71+ if existing. image_data == bytes {
72+ // already in sync, skip DB write
73+ } else {
74+ db:: image:: area:: queries:: delete ( existing. id , image_pool) . await ?;
75+ db:: image:: area:: queries:: insert (
76+ area. id ,
77+ "square" ,
78+ bytes,
79+ width as i64 ,
80+ height as i64 ,
81+ bytes_len,
82+ image_pool,
83+ )
84+ . await ?;
85+ }
86+ } else {
87+ db:: image:: area:: queries:: insert (
88+ area. id ,
89+ "square" ,
90+ bytes,
91+ width as i64 ,
92+ height as i64 ,
93+ bytes_len,
94+ image_pool,
95+ )
96+ . await ?;
97+ }
98+ }
99+
57100 let url = format ! ( "https://static.btcmap.org/images/areas/{file_name}" ) ;
58101 let patch_set = Map :: from_iter ( [ ( "icon:square" . into ( ) , url. into ( ) ) ] ) ;
59102 let area = db:: main:: area:: queries:: patch_tags ( area. id , patch_set, pool) . await ?;
0 commit comments