@@ -4,58 +4,96 @@ export const working_folder = writable('');
44export const file_server_port = writable ( 0 ) ;
55export const available_files = writable < string [ ] > ( [ ] ) ;
66
7- const VALID_IMAGE_FORMATS = [ '.jpg' , '.png' , '.gif' , '.bmp' , '.webp' , '.jpeg' , '.avif' ]
7+ const VALID_IMAGE_FORMATS = [ '.jpg' , '.png' , '.gif' , '.bmp' , '.webp' , '.jpeg' , '.avif' ] ;
88
99// Have a derived store, which combines all available files into an image file, and a corresponding caption file (the first non image file with the same name)
10- export const available_images = writable < { image : string , caption : string , caption_file : string , viewed : boolean } [ ] > ( [ ] ) ;
10+ export const available_images = writable <
11+ { image : string ; caption : string ; caption_file : string ; viewed : boolean } [ ]
12+ > ( [ ] ) ;
1113export const is_loading_image_data = writable ( false ) ;
1214
1315const update_available_files = async ( ) => {
14- const image_map = new Map < string , { image : string , caption : string , caption_file : string , viewed : boolean } > ( ) ;
15-
16-
17- const unprocessed_files = [ ] ;
18-
19- for ( const file of get ( available_files ) ) {
20- // Get the file without the extension
21- const [ file_name , file_extension ] = file . split ( '.' ) ;
22- if ( VALID_IMAGE_FORMATS . includes ( `.${ file_extension . toLocaleLowerCase ( ) } ` ) ) {
23- image_map . set ( file_name , {
24- image : file ,
25- caption : '' ,
26- caption_file : '' ,
27- viewed : false ,
28- } ) ;
29- } else {
30- unprocessed_files . push ( file ) ;
31- }
32- } ;
33-
34- for ( const file of unprocessed_files ) {
35- const [ file_name , file_extension ] = file . split ( '.' ) ;
36- const image = image_map . get ( file_name ) ;
37- if ( image && image . caption === '' ) {
38- image . caption_file = file ;
39-
40- // Fetch the text caption from our file server
41- image . caption = await fetch ( `http://localhost:${ get ( file_server_port ) } /${ file } ` ) . then ( ( response ) => {
42- return response . text ( ) ;
43- } ) . catch ( ( error ) => {
44- console . error ( error ) ;
45- } ) || '' ;
46-
47- }
48- }
49-
50- return Array . from ( image_map . values ( ) ) ;
51- }
16+ const image_map = new Map <
17+ string ,
18+ { image : string ; caption : string ; caption_file : string ; viewed : boolean }
19+ > ( ) ;
5220
21+ const unprocessed_files = [ ] ;
22+
23+ for ( const file of get ( available_files ) ) {
24+ // Get the file without the extension
25+ const [ file_name , file_extension ] = file . split ( '.' ) ;
26+ if ( file_extension && VALID_IMAGE_FORMATS . includes ( `.${ file_extension . toLowerCase ( ) } ` ) ) {
27+ image_map . set ( file_name , {
28+ image : file ,
29+ caption : '' ,
30+ caption_file : '' ,
31+ viewed : false
32+ } ) ;
33+ } else {
34+ unprocessed_files . push ( file ) ;
35+ }
36+ }
37+
38+ const caption_counts : Map < string , number > = new Map ( ) ;
39+
40+ for ( const file of unprocessed_files ) {
41+ const [ file_name , file_extension ] = file . split ( '.' ) ;
42+
43+ // If a file extension exists
44+ if ( file_extension !== undefined ) {
45+ // Count how often we see the different caption extensions
46+ if ( caption_counts . has ( file_extension . toLowerCase ( ) ) ) {
47+ caption_counts . set (
48+ file_extension . toLocaleLowerCase ( ) ,
49+ caption_counts . get ( file_extension . toLowerCase ( ) ) ! + 1
50+ ) ;
51+ } else {
52+ caption_counts . set ( file_extension . toLowerCase ( ) , 1 ) ;
53+ }
54+ }
55+
56+ const image = image_map . get ( file_name ) ;
57+ if ( image && image . caption === '' ) {
58+ image . caption_file = file ;
59+
60+ // Fetch the text caption from our file server, disable caching
61+ image . caption =
62+ ( await fetch ( `http://localhost:${ get ( file_server_port ) } /${ file } ` , { cache : 'no-store' } )
63+ . then ( ( response ) => {
64+ return response . text ( ) ;
65+ } )
66+ . catch ( ( error ) => {
67+ console . error ( error ) ;
68+ } ) ) || '' ;
69+ }
70+ }
71+
72+ let final_caption_extension = 'txt' ;
73+
74+ if ( caption_counts . size > 0 ) {
75+ // Find the most common caption extension
76+ const most_common_caption_extensions = [ ...caption_counts . entries ( ) ] . reduce ( ( a , e ) =>
77+ e [ 1 ] > a [ 1 ] ? e : a
78+ ) ;
79+ if ( most_common_caption_extensions . length > 0 ) {
80+ [ final_caption_extension ] = most_common_caption_extensions ;
81+ }
82+ }
83+
84+ // Iterate all images, and set the caption file if it is not already set (using the same name as the image, but with the most common caption extension)
85+ for ( const image of image_map . values ( ) ) {
86+ if ( image . caption_file === '' ) {
87+ image . caption_file = `${ image . image . split ( '.' ) [ 0 ] } .${ final_caption_extension } ` ;
88+ }
89+ }
90+
91+ return Array . from ( image_map . values ( ) ) ;
92+ } ;
5393
5494// But we need to manually, asynchronously update the available images by subscribing to the available files store
5595available_files . subscribe ( async ( files ) => {
56- is_loading_image_data . set ( true ) ;
57- available_images . set ( await update_available_files ( ) ) ;
58- is_loading_image_data . set ( false ) ;
96+ is_loading_image_data . set ( true ) ;
97+ available_images . set ( await update_available_files ( ) ) ;
98+ is_loading_image_data . set ( false ) ;
5999} ) ;
60-
61-
0 commit comments