Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make custom wallpapers expire earlier / daily #700

Closed
johannrichard opened this issue May 22, 2020 · 4 comments
Closed

Make custom wallpapers expire earlier / daily #700

johannrichard opened this issue May 22, 2020 · 4 comments

Comments

@johannrichard
Copy link

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.

@oznu
Copy link
Member

oznu commented May 22, 2020

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).

@johannrichard
Copy link
Author

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?

oznu added a commit that referenced this issue May 22, 2020
@oznu
Copy link
Member

oznu commented May 22, 2020

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

@oznu oznu closed this as completed May 22, 2020
@mbierman
Copy link

mbierman commented May 23, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants