feat: use hash for unique extension id

This commit is contained in:
Vlad Stan 2022-11-29 18:48:11 +02:00
parent 8f8ebbeeaa
commit a6e3b5e049
2 changed files with 15 additions and 10 deletions

View File

@ -170,7 +170,7 @@ def register_ext_routes(app: FastAPI, ext: Extension) -> None:
logger.trace(f"adding route for extension {ext_module}") logger.trace(f"adding route for extension {ext_module}")
prefix = f"/upgrades/{ext.version}" if ext.version != "" else "" prefix = f"/upgrades/{ext.hash}" if ext.hash != "" else ""
app.include_router(router=ext_route, prefix=prefix) app.include_router(router=ext_route, prefix=prefix)

View File

@ -794,23 +794,28 @@ async def api_install_extension(
try: try:
ext_dir = os.path.join("lnbits/extensions", ext_id) ext_dir = os.path.join("lnbits/extensions", ext_id)
# shutil.rmtree(ext_dir, True) shutil.rmtree(ext_dir, True)
# with zipfile.ZipFile(ext_zip_file, "r") as zip_ref: with zipfile.ZipFile(ext_zip_file, "r") as zip_ref:
# zip_ref.extractall("lnbits/extensions") zip_ref.extractall("lnbits/extensions")
ext_upgrade_dir = os.path.join("lnbits/upgrades", f"{extension.id}-{extension.hash}")
os.makedirs("lnbits/upgrades", exist_ok=True)
shutil.rmtree(ext_upgrade_dir, True)
with zipfile.ZipFile(ext_zip_file, "r") as zip_ref:
zip_ref.extractall(ext_upgrade_dir)
# todo: is admin only # todo: is admin only
# lnbits/extensions/satspay/upgrade/111/satspay/__init__.py
ext = Extension( ext = Extension(
code=extension.id, code=extension.id,
is_valid=True, is_valid=True,
is_admin_only=False, is_admin_only=False,
name=extension.name, name=extension.name,
version="111", hash=extension.hash,
) )
# current_versions = await get_dbversions() current_versions = await get_dbversions()
# current_version = current_versions.get(ext.code, 0) current_version = current_versions.get(ext.code, 0)
# await migrate_extension_database(ext, current_version) # todo: test await migrate_extension_database(ext, current_version) # todo: use new module
# disable by default # disable by default
await update_user_extension(user_id=USER_ID_ALL, extension=ext_id, active=False) await update_user_extension(user_id=USER_ID_ALL, extension=ext_id, active=False)
@ -826,7 +831,7 @@ async def api_install_extension(
os.remove(ext_zip_file) os.remove(ext_zip_file)
# remove module from extensions # remove module from extensions
# shutil.rmtree(ext_dir, True) shutil.rmtree(ext_dir, True)
raise HTTPException( raise HTTPException(
status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail=str(ex) status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail=str(ex)
) )