Skip to content

Commit

Permalink
Add theme blob upload via py
Browse files Browse the repository at this point in the history
  • Loading branch information
suchmememanyskill committed Mar 2, 2024
1 parent c30575d commit 0097f83
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
12 changes: 12 additions & 0 deletions css_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
32 changes: 29 additions & 3 deletions css_remoteinstall.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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)
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))
3 changes: 3 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0097f83

Please sign in to comment.