@@ -173,6 +173,9 @@ fn services(originals: Originals, backend: Option<Backend>, files: Files, frozen
173173 screen_profile : None ,
174174 backup_library : Some ( Box :: new ( move |session : & mut Session | {
175175 let Some ( b) = backup_backend. clone ( ) else { return Err ( "nothing is stored in this browser session (?store=memory)" . into ( ) ) } ;
176+ if let Some ( why) = crate :: backup:: unsaved_refusal ( session. unsaved ( ) ) {
177+ return Err ( why) ;
178+ }
176179 // the photos' own names for the originals in the zip
177180 let names: std:: collections:: HashMap < String , String > = session
178181 . catalog
@@ -280,6 +283,9 @@ struct Boot {
280283 index : ThumbIndex ,
281284 /// The library folder in storage (`library`, or a restored backup's).
282285 lib_dir : String ,
286+ /// Library files that exist but couldn't be read (#1507): the library isn't opened and
287+ /// nothing is saved, so the stored copy stays as it was.
288+ load_error : Option < String > ,
283289}
284290
285291async fn boot ( opts : Options ) -> Boot {
@@ -298,6 +304,7 @@ async fn boot(opts: Options) -> Boot {
298304 let files = Files :: default ( ) ;
299305 let mut index = ThumbIndex :: default ( ) ;
300306 let mut lib_dir = DEFAULT_LIBRARY_DIR . to_string ( ) ;
307+ let mut load_error = None ;
301308 if let Some ( b) = & backend {
302309 if opts. reset {
303310 // everything this site keeps in the browser, photos included: never without asking
@@ -315,21 +322,32 @@ async fn boot(opts: Options) -> Boot {
315322 }
316323 }
317324 // a restored backup lives in its own folder (see `backup`)
318- if let Ok ( Some ( d) ) = b. read ( ACTIVE_LIBRARY ) . await {
319- let d = String :: from_utf8_lossy ( & d) . trim ( ) . to_string ( ) ;
320- if valid_library_dir ( & d) {
321- lib_dir = d;
325+ match b. read ( ACTIVE_LIBRARY ) . await {
326+ Ok ( Some ( d) ) => {
327+ let d = String :: from_utf8_lossy ( & d) . trim ( ) . to_string ( ) ;
328+ if valid_library_dir ( & d) {
329+ lib_dir = d;
330+ }
322331 }
332+ Ok ( None ) => { }
333+ // not knowing which library is in use, any library opened could be the wrong one
334+ Err ( e) => load_error = Some ( format ! ( "couldn't read which library is in use from browser storage ({e})" ) ) ,
323335 }
324- for name in LIBRARY_FILES {
325- match b. read ( & library_key ( & lib_dir, name) ) . await {
326- Ok ( Some ( data) ) => files. preload ( name, data) ,
327- Ok ( None ) => { }
328- Err ( e) => log:: error!( "reading {name}: {e}" ) ,
336+ if load_error. is_none ( ) {
337+ let mut reads = Vec :: new ( ) ;
338+ for name in LIBRARY_FILES {
339+ reads. push ( ( name, b. read ( & library_key ( & lib_dir, name) ) . await ) ) ;
340+ }
341+ if let Err ( e) = crate :: files:: preload_library ( & files, reads) {
342+ log:: error!( "{e}" ) ;
343+ load_error = Some ( e) ;
329344 }
330345 }
331- if let Ok ( Some ( j) ) = b. read ( THUMB_INDEX ) . await {
332- index = ThumbIndex :: from_json ( & j) ;
346+ let index_read = b. read ( THUMB_INDEX ) . await ;
347+ match & index_read {
348+ Ok ( Some ( j) ) => index = ThumbIndex :: from_json ( j) ,
349+ Ok ( None ) => { }
350+ Err ( e) => log:: warn!( "reading the thumbnail index: {e}" ) ,
333351 }
334352 crate :: backend:: request_persistence ( |granted| {
335353 if !granted {
@@ -339,7 +357,8 @@ async fn boot(opts: Options) -> Boot {
339357 ) ;
340358 }
341359 } ) ;
342- // thumbnails stored without an index entry (index saved late, or lost): delete them
360+ // thumbnails stored without an index entry (index saved late, or lost): delete them; not
361+ // when the index couldn't be read, which would make every thumbnail look unindexed
343362 let b2 = b. clone ( ) ;
344363 let known: std:: collections:: HashSet < String > = b
345364 . list ( "thumbs" )
@@ -349,7 +368,7 @@ async fn boot(opts: Options) -> Boot {
349368 . filter ( |n| n. ends_with ( ".jpg" ) )
350369 . map ( |n| n. trim_end_matches ( ".jpg" ) . to_string ( ) )
351370 . collect ( ) ;
352- let orphans: Vec < String > = known. into_iter ( ) . filter ( |k| !index. contains ( k) ) . collect ( ) ;
371+ let orphans: Vec < String > = if index_read . is_ok ( ) { known. into_iter ( ) . filter ( |k| !index. contains ( k) ) . collect ( ) } else { Vec :: new ( ) } ;
353372 if !orphans. is_empty ( ) {
354373 wasm_bindgen_futures:: spawn_local ( async move {
355374 for k in & orphans {
@@ -366,7 +385,7 @@ async fn boot(opts: Options) -> Boot {
366385 index. len( ) ,
367386 index. total( ) as f64 / 1e6
368387 ) ;
369- Boot { opts, backend, files, index, lib_dir }
388+ Boot { opts, backend, files, index, lib_dir, load_error }
370389}
371390
372391struct WebApp {
@@ -389,7 +408,7 @@ struct WebApp {
389408
390409impl WebApp {
391410 fn new ( cc : & eframe:: CreationContext < ' _ > , boot : Boot , chinese_font : Vec < u8 > ) -> Self {
392- let Boot { opts, backend, files, index, lib_dir } = boot;
411+ let Boot { opts, backend, files, index, lib_dir, load_error } = boot;
393412 let originals = Originals :: default ( ) ;
394413 let t = perf_now ( ) ;
395414 let mut session = Session :: new ( ) ;
@@ -401,7 +420,13 @@ impl WebApp {
401420 on_disk : false ,
402421 } ;
403422 let mut problem = None ;
404- match session. open_library_in ( stores, true ) . cloned ( ) {
423+ // a library that couldn't be read is not opened: it would show incomplete, then save over the stored copy
424+ let read_failed = load_error. is_some ( ) ;
425+ let opened = match load_error {
426+ Some ( e) => Err ( format ! ( "{e}. Nothing was changed in storage" ) ) ,
427+ None => session. open_library_in ( stores, true ) . cloned ( ) . map_err ( |e| e. to_string ( ) ) ,
428+ } ;
429+ match opened {
405430 Ok ( r) => log:: info!(
406431 "lightcraft: library {} in {:.0} ms ({} photos; snapshot seq {}, {} ops replayed)" ,
407432 if r. created { "created" } else { "loaded" } ,
@@ -432,7 +457,8 @@ impl WebApp {
432457 }
433458 // previews are large (≤ 2560 px, f32): keep few in a 32-bit address space
434459 session. media . preview_capacity = 3 ;
435- let frozen = Rc :: new ( Cell :: new ( false ) ) ;
460+ // nothing is saved while the stored library couldn't be read
461+ let frozen = Rc :: new ( Cell :: new ( read_failed) ) ;
436462 let mut app = LightcraftApp :: new ( session, services ( originals. clone ( ) , backend. clone ( ) , files. clone ( ) , frozen. clone ( ) , cc. egui_ctx . clone ( ) ) )
437463 . with_chinese_font ( chinese_font) ;
438464 let ui_written = files. get ( "ui.json" ) . unwrap_or_default ( ) ;
@@ -667,11 +693,40 @@ impl eframe::App for WebApp {
667693 }
668694}
669695
696+ /// "Auto" follows the browser's languages (issue #2085): the first of `navigator.languages` (else
697+ /// `navigator.language`) that LightCraft ships becomes the UI's default. Every step is fallible
698+ /// (as in [`crate::safety::acquire_tab_lock`]): without a usable navigator Auto stays English,
699+ /// as before.
700+ fn install_browser_language ( ) {
701+ let Ok ( navigator) = js_sys:: Reflect :: get ( & js_sys:: global ( ) , & "navigator" . into ( ) ) else { return } ;
702+ let mut tags: Vec < String > = Vec :: new ( ) ;
703+ if let Ok ( languages) = js_sys:: Reflect :: get ( & navigator, & "languages" . into ( ) )
704+ && let Some ( languages) = languages. dyn_ref :: < js_sys:: Array > ( )
705+ {
706+ for tag in languages. iter ( ) {
707+ if let Some ( tag) = tag. as_string ( ) {
708+ tags. push ( tag) ;
709+ }
710+ }
711+ }
712+ if tags. is_empty ( )
713+ && let Ok ( language) = js_sys:: Reflect :: get ( & navigator, & "language" . into ( ) )
714+ && let Some ( language) = language. as_string ( )
715+ {
716+ tags. push ( language) ;
717+ }
718+ if let Some ( language) = lightcraft_ui_egui:: i18n:: locale_from_tags ( & tags) {
719+ lightcraft_ui_egui:: i18n:: set_default_language ( language) ;
720+ log:: info!( "lightcraft: default language {language:?} from the browser's languages" ) ;
721+ }
722+ }
723+
670724/// Start the app with the separately fetched Simplified Chinese font (empty in font-free builds).
671725#[ wasm_bindgen]
672726pub fn start ( chinese_font : Vec < u8 > ) {
673727 eframe:: WebLogger :: init ( log:: LevelFilter :: Info ) . ok ( ) ;
674728 safety:: install_panic_hook ( ) ;
729+ install_browser_language ( ) ;
675730 log:: info!( "lightcraft: wasm instantiated at {:.0} ms" , perf_now( ) ) ;
676731 if chinese_font. is_empty ( ) {
677732 log:: warn!( "lightcraft: no Simplified Chinese font supplied; Chinese-only glyphs will show as boxes" ) ;
0 commit comments