@@ -220,6 +220,7 @@ function resolveProductMode(s: LocalSettings): string {
220220type PresetLoadConfig = {
221221 baseDir : string
222222 modeDir : string
223+ imageFallbackDirs : string [ ]
223224 includePresets ?: string [ ]
224225 excludePresets ?: string [ ]
225226}
@@ -228,9 +229,11 @@ function getPresetLoadConfig(s: LocalSettings): PresetLoadConfig {
228229 const mode = resolveProductMode ( s )
229230 const variant = s . isDemoModeEnabled ? 'demo' : 'presets'
230231 const modeConfig = loadModeConfig ( mode )
232+ const basePresetsDir = path . join ( modesDir , 'base' , 'presets' )
231233 return {
232234 baseDir : path . join ( modesDir , 'base' , variant ) ,
233235 modeDir : path . join ( modesDir , mode , variant ) ,
236+ imageFallbackDirs : variant === 'demo' ? [ basePresetsDir ] : [ ] ,
234237 includePresets : modeConfig ?. includePresets ,
235238 excludePresets : modeConfig ?. excludePresets ,
236239 }
@@ -242,7 +245,20 @@ function getModeDemoDir(s: LocalSettings): string {
242245
243246type PresetFile = { content : string ; image : string | null }
244247
245- async function readPresetsFromDir ( dir : string ) : Promise < Map < string , PresetFile > > {
248+ function findPresetImage ( baseName : string , dirs : string [ ] ) : string | null {
249+ for ( const dir of dirs ) {
250+ for ( const ext of [ '.png' , '.jpg' , '.jpeg' ] ) {
251+ const imagePath = path . join ( dir , `${ baseName } ${ ext } ` )
252+ if ( fs . existsSync ( imagePath ) ) return imagePath
253+ }
254+ }
255+ return null
256+ }
257+
258+ async function readPresetsFromDir (
259+ dir : string ,
260+ imageFallbackDirs : string [ ] = [ ] ,
261+ ) : Promise < Map < string , PresetFile > > {
246262 const result = new Map < string , PresetFile > ( )
247263 if ( ! fs . existsSync ( dir ) ) return result
248264
@@ -257,17 +273,15 @@ async function readPresetsFromDir(dir: string): Promise<Map<string, PresetFile>>
257273
258274 const baseName = path . basename ( file , '.json' )
259275 let imageBase64 : string | null = null
260- for ( const ext of [ '.png' , '.jpg' , '.jpeg' ] ) {
261- const imagePath = path . join ( dir , `${ baseName } ${ ext } ` )
262- if ( fs . existsSync ( imagePath ) ) {
263- try {
264- const imageBuffer = await fs . promises . readFile ( imagePath )
265- const mimeType = ext === '.png' ? 'image/png' : 'image/jpeg'
266- imageBase64 = `data:${ mimeType } ;base64,${ imageBuffer . toString ( 'base64' ) } `
267- break
268- } catch ( error ) {
269- appLogger . warn ( `Failed to read image file ${ imagePath } : ${ error } ` , 'electron-backend' )
270- }
276+ const imagePath = findPresetImage ( baseName , [ dir , ...imageFallbackDirs ] )
277+ if ( imagePath ) {
278+ try {
279+ const imageBuffer = await fs . promises . readFile ( imagePath )
280+ const ext = path . extname ( imagePath ) . toLowerCase ( )
281+ const mimeType = ext === '.png' ? 'image/png' : 'image/jpeg'
282+ imageBase64 = `data:${ mimeType } ;base64,${ imageBuffer . toString ( 'base64' ) } `
283+ } catch ( error ) {
284+ appLogger . warn ( `Failed to read image file ${ imagePath } : ${ error } ` , 'electron-backend' )
271285 }
272286 }
273287
@@ -1365,8 +1379,11 @@ function initEventHandle() {
13651379 appLogger . error ( `Failed to filter partner presets: ${ error } ` , 'electron-backend' )
13661380 }
13671381 try {
1368- const basePresets = applyPresetFilter ( await readPresetsFromDir ( config . baseDir ) , config )
1369- const modePresets = await readPresetsFromDir ( config . modeDir )
1382+ const basePresets = applyPresetFilter (
1383+ await readPresetsFromDir ( config . baseDir , config . imageFallbackDirs ) ,
1384+ config ,
1385+ )
1386+ const modePresets = await readPresetsFromDir ( config . modeDir , config . imageFallbackDirs )
13701387
13711388 for ( const [ name , preset ] of modePresets ) {
13721389 basePresets . set ( name , preset )
0 commit comments