diff --git a/css_loader.py b/css_loader.py index 6c0b8be..479bbe0 100644 --- a/css_loader.py +++ b/css_loader.py @@ -3,6 +3,7 @@ from css_theme import Theme, CSS_LOADER_VER from css_themepatch import ThemePatch from css_browserhook import remove_all, commit_all +from css_remoteinstall import upload from asyncio import sleep from os import listdir, path, mkdir @@ -171,6 +172,17 @@ async def generate_preset_theme_from_theme_names(self, name : str, themeNames : except Exception as e: return Result(False, str(e)) + async def upload_theme(self, name : str, base_url : str, bearer_token : str) -> Result: + theme = await self._get_theme(name) + + if theme is None: + return Result(False, f"Could not find theme {name}") + + try: + return await upload(theme, base_url, bearer_token) + except Exception as e: + return Result(False, str(e)) + async def _enable_theme(self, theme : Theme, set_deps : bool = True, set_deps_value : bool = True, ignore_dependencies : list = []) -> Result: if theme is None: return Result(False) diff --git a/css_remoteinstall.py b/css_remoteinstall.py index 6de1042..cc0d5ed 100644 --- a/css_remoteinstall.py +++ b/css_remoteinstall.py @@ -1,6 +1,6 @@ -import asyncio, json, tempfile, os, aiohttp, zipfile +import asyncio, json, tempfile, os, aiohttp, zipfile, shutil from css_utils import Result, Log, get_theme_path, store_or_file_config -from css_theme import CSS_LOADER_VER +from css_theme import CSS_LOADER_VER, Theme async def run(command : str) -> str: proc = await asyncio.create_subprocess_shell(command, @@ -64,4 +64,30 @@ async def install(id : str, base_url : str, local_themes : list) -> Result: await install(x["id"], base_url, local_themes) - return Result(True) \ No newline at end of file + return Result(True) + +async def upload(theme : Theme, base_url : str, bearer_token : str) -> Result: + if not base_url.endswith("/"): + base_url = base_url + "/" + + url = f"{base_url}blobs" + + with tempfile.TemporaryDirectory() as tmp: + themePath = os.path.join(tmp, "theme.zip") + print(themePath[:-4]) + print(theme.themePath) + shutil.make_archive(themePath[:-4], 'zip', theme.themePath) + + with open(themePath, "rb") as file: + async with aiohttp.ClientSession(headers={"User-Agent": f"SDH-CSSLoader/{CSS_LOADER_VER}", "Authorization": f"Bearer {bearer_token}"}, connector=aiohttp.TCPConnector(verify_ssl=False)) as session: + try: + mp = aiohttp.FormData() + mp.add_field("file", file) + async with session.post(url, data=mp) as resp: + if resp.status != 200: + raise Exception(f"Invalid status code {resp.status}") + + data = await resp.json() + return Result(True, data) + except Exception as e: + return Result(False, str(e)) \ No newline at end of file diff --git a/main.py b/main.py index 207848e..ac33a59 100644 --- a/main.py +++ b/main.py @@ -144,6 +144,9 @@ async def get_last_load_errors(self): return { "fails": self.loader.last_load_errors } + + async def upload_theme(self, name : str, base_url : str, bearer_token : str) -> dict: + return (await self.loader.upload_theme(name, base_url, bearer_token)).to_dict() async def _main(self): global Initialized