Homebridge-config-ui-x: Make custom wallpapers expire earlier / daily

Created on 22 May 2020  路  4Comments  路  Source: oznu/homebridge-config-ui-x

Both the default (static) and custom login wallpapers currently never expire. I would argue while this makes sense for the default Wallpaper, the expiry for the custom login wallpaper should be lower, maybe around a day or even hours, and certainly not immutable: This allows to set a changing wallpaper (e.g. by separately downloading a daily image from one of the many different sites that provide that) to a fixed location and have HomeBridge Config UI X display it reliably.

https://github.com/oznu/homebridge-config-ui-x/blob/586cbd127c0c37b6bb7e3ef10f3b186a7a4d89fb/src/main.ts#L86-L99

So for example revalidate / download daily:

  // login page image
  app.getHttpAdapter().get('/assets/snapshot.jpg', async (req, res) => {
    if (configService.ui.loginWallpaper) {
      if (!await fs.pathExists(configService.ui.loginWallpaper)) {
        logger.error(`Custom Login Wallpaper does not exist: ${configService.ui.loginWallpaper}`);
        return res.code(404).send('Not Found');
      }
      res.type('image/jpg');
      // Invalidate custom login wallpaper daily
      res.header('Cache-Control', 'public,max-age=86400');
      res.send(await fs.readFile(path.resolve(configService.ui.loginWallpaper)));
    } else {
      res.header('Cache-Control', 'public,max-age=31536000,immutable');
      res.sendFile('assets/snapshot.jpg');
    }
  });

There are obviously other approaches, e.g. allowing for URL's in the setting, but these bring additional troubles with Content Policy, offline access and more. So I think reducing the cache time for custom wallpapers is a pragmatic tradeoff for most use cases.

Most helpful comment

On startup or config save the UI will now check to see if a custom login wallpaper path has been set in the config.json or if a file named ui-wallpaper.jpg exists in the homebridge storage folder (eg. ~/.homebridge/ui-wallpaper.jpg) and do a stat on the file and creating a sha256 checksum of the created time, modified time and size of the file.

When the UI is loaded it calls the /settings endpoint as per normal and if a custom wallpaper was found an extra attribute is added with the hash. This hash used to load the wallpaper from the endpoint /wallpaper/:hash. If no custom wallpaper was set, it falls back to the standard /assets/snapshot.jpg.

The cache time is still set to effectively never expire on these files, but by doing it this way, if the custom wallpaper is modified or added, the changes will be reflected immediately.

https://github.com/oznu/homebridge-config-ui-x/wiki/How-To-Test-Upcoming-Changes

All 4 comments

I've been thinking about this lately. I think the best way would be to generate a unique URL for the wallpaper (sha256.jpg) and include that in the settings returned to the client before authentication. That way the cache time can still be high, but will break immediately when the user changes the wallpaper image (even if it's named the same thing).

Would adding a redirect to that unique file-name from /assets/snapshot.jpg instead of having to let the frontend know about a changing filename be simpler? IIRC, caching would still work on the final file URL?

On startup or config save the UI will now check to see if a custom login wallpaper path has been set in the config.json or if a file named ui-wallpaper.jpg exists in the homebridge storage folder (eg. ~/.homebridge/ui-wallpaper.jpg) and do a stat on the file and creating a sha256 checksum of the created time, modified time and size of the file.

When the UI is loaded it calls the /settings endpoint as per normal and if a custom wallpaper was found an extra attribute is added with the hash. This hash used to load the wallpaper from the endpoint /wallpaper/:hash. If no custom wallpaper was set, it falls back to the standard /assets/snapshot.jpg.

The cache time is still set to effectively never expire on these files, but by doing it this way, if the custom wallpaper is modified or added, the changes will be reflected immediately.

https://github.com/oznu/homebridge-config-ui-x/wiki/How-To-Test-Upcoming-Changes

Funny I was going to play with a cronjob to fetch an image from unsplash and save it using @oznu s new dynamic image. If anyone is interested I could post the result. Below is my custom login. :)

image

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mbierman picture mbierman  路  5Comments

afharo picture afharo  路  5Comments

charona picture charona  路  3Comments

DJay-X picture DJay-X  路  3Comments

Finch106 picture Finch106  路  3Comments