Fixed manifest updating for smaller url prefix

When the value of the URL prefix would be changed, if the resulting json would be smaller than the current json, it would create invalid json. Now we wipe the contents before writing to avoid this.
This commit is contained in:
CasVT
2024-03-10 11:53:12 +01:00
parent 37cec53649
commit 99d59cd0e7

View File

@@ -5,7 +5,7 @@ Getting and setting settings
""" """
import logging import logging
from json import dumps, loads from json import dump, load
from typing import Any from typing import Any
from backend.custom_exceptions import InvalidKeyValue, KeyNotFound from backend.custom_exceptions import InvalidKeyValue, KeyNotFound
@@ -67,7 +67,10 @@ def _format_setting(key: str, value):
if not isinstance(value, str): if not isinstance(value, str):
raise InvalidKeyValue(key, value) raise InvalidKeyValue(key, value)
if value: if value == '/':
value = ''
elif value:
value = '/' + value.strip('/') value = '/' + value.strip('/')
elif key == 'log_level' and not value in (logging.INFO, logging.DEBUG): elif key == 'log_level' and not value in (logging.INFO, logging.DEBUG):
@@ -171,12 +174,16 @@ def update_manifest(url_base: str) -> None:
Args: Args:
url_base (str): The url base to use in the file. url_base (str): The url base to use in the file.
""" """
with open(folder_path('frontend', 'static', 'json', 'pwa_manifest.json'), 'r+') as f: filename = folder_path('frontend', 'static', 'json', 'pwa_manifest.json')
manifest = loads(f.read())
with open(filename, 'r') as f:
manifest = load(f)
manifest['start_url'] = url_base + '/' manifest['start_url'] = url_base + '/'
manifest['icons'][0]['src'] = f'{url_base}/static/img/favicon.svg' manifest['icons'][0]['src'] = f'{url_base}/static/img/favicon.svg'
f.seek(0)
f.write(dumps(manifest, indent=4)) with open(filename, 'w') as f:
dump(manifest, f, indent=4)
return return
def backup_hosting_settings() -> None: def backup_hosting_settings() -> None:
@@ -228,5 +235,7 @@ def restore_hosting_settings() -> None:
"UPDATE config SET value = ? WHERE key = ?", "UPDATE config SET value = ? WHERE key = ?",
((v, k) for k, v in hosting_settings.items()) ((v, k) for k, v in hosting_settings.items())
) )
update_manifest(hosting_settings['url_prefix'])
return return