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

feat: bundle asset images with repo #620

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 3 additions & 83 deletions app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,87 +22,16 @@


DATA_PATH = Path.cwd() / ".data"
ACHIEVEMENTS_ASSETS_PATH = DATA_PATH / "assets/medals/client"
DEFAULT_AVATAR_PATH = DATA_PATH / "avatars/default.jpg"
ASSETS_PATH = Path.cwd() / "assets"
ACHIEVEMENTS_ASSETS_PATH = ASSETS_PATH / "medals"
DEFAULT_AVATAR_PATH = ASSETS_PATH / "default_avatar.jpg"


def make_safe_name(name: str) -> str:
"""Return a name safe for usage in sql."""
return name.lower().replace(" ", "_")


def _download_achievement_images_osu(achievements_path: Path) -> bool:
"""Download all used achievement images (one by one, from osu!)."""
achs: list[str] = []

for resolution in ("", "@2x"):
for mode in ("osu", "taiko", "fruits", "mania"):
# only osu!std has 9 & 10 star pass/fc medals.
for star_rating in range(1, 1 + (10 if mode == "osu" else 8)):
achs.append(f"{mode}-skill-pass-{star_rating}{resolution}.png")
achs.append(f"{mode}-skill-fc-{star_rating}{resolution}.png")

for combo in (500, 750, 1000, 2000):
achs.append(f"osu-combo-{combo}{resolution}.png")

for mod in (
"suddendeath",
"hidden",
"perfect",
"hardrock",
"doubletime",
"flashlight",
"easy",
"nofail",
"nightcore",
"halftime",
"spunout",
):
achs.append(f"all-intro-{mod}{resolution}.png")

log("Downloading achievement images from osu!.", Ansi.LCYAN)

for ach in achs:
resp = httpx.get(f"https://assets.ppy.sh/medals/client/{ach}")
if resp.status_code != 200:
return False

log(f"Saving achievement: {ach}", Ansi.LCYAN)
(achievements_path / ach).write_bytes(resp.content)

return True


def download_achievement_images(achievements_path: Path) -> None:
"""Download all used achievement images (using the best available source)."""

# download individual files from the official osu! servers
downloaded = _download_achievement_images_osu(achievements_path)

if downloaded:
log("Downloaded all achievement images.", Ansi.LGREEN)
else:
# TODO: make the code safe in this state
log("Failed to download achievement images.", Ansi.LRED)
achievements_path.rmdir()

# allow passthrough (don't hard crash).
# the server will *mostly* work in this state.
pass


def download_default_avatar(default_avatar_path: Path) -> None:
"""Download an avatar to use as the server's default."""
resp = httpx.get("https://i.cmyui.xyz/U24XBZw-4wjVME-JaEz3.png")

if resp.status_code != 200:
log("Failed to fetch default avatar.", Ansi.LRED)
return

log("Downloaded default avatar.", Ansi.LGREEN)
default_avatar_path.write_bytes(resp.content)


def has_internet_connectivity(timeout: float = 1.0) -> bool:
"""Check for an active internet connection."""
COMMON_DNS_SERVERS = (
Expand Down Expand Up @@ -187,15 +116,6 @@ def ensure_persistent_volumes_are_available() -> None:
subdir = DATA_PATH / sub_dir
subdir.mkdir(exist_ok=True)

# download achievement images from osu!
if not ACHIEVEMENTS_ASSETS_PATH.exists():
ACHIEVEMENTS_ASSETS_PATH.mkdir(parents=True)
download_achievement_images(ACHIEVEMENTS_ASSETS_PATH)

# download a default avatar image for new users
if not DEFAULT_AVATAR_PATH.exists():
download_default_avatar(DEFAULT_AVATAR_PATH)


def is_running_as_admin() -> bool:
try:
Expand Down
Binary file added assets/default_avatar.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/medals/all-intro-doubletime.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/medals/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/medals/all-intro-easy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/medals/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/medals/all-intro-flashlight.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/medals/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/medals/all-intro-halftime.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/medals/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/medals/all-intro-hardrock.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/medals/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/medals/all-intro-hidden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/medals/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/medals/all-intro-nightcore.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/medals/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/medals/all-intro-nofail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/medals/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/medals/all-intro-perfect.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/medals/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/medals/all-intro-spunout.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/medals/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/medals/all-intro-suddendeath.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/medals/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/medals/fruits-skill-fc-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/medals/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/medals/fruits-skill-fc-2.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/fruits-skill-fc-3.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/fruits-skill-fc-4.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/fruits-skill-fc-5.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/fruits-skill-fc-6.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/fruits-skill-fc-7.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/fruits-skill-fc-8.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/fruits-skill-pass-1.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/fruits-skill-pass-2.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/fruits-skill-pass-3.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/fruits-skill-pass-4.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/fruits-skill-pass-5.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/fruits-skill-pass-6.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/fruits-skill-pass-7.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/fruits-skill-pass-8.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/mania-skill-fc-1.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/mania-skill-fc-2.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/mania-skill-fc-3.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/mania-skill-fc-4.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/mania-skill-fc-5.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/mania-skill-fc-6.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/mania-skill-fc-7.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/mania-skill-fc-8.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/mania-skill-pass-1.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/mania-skill-pass-2.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/mania-skill-pass-3.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/mania-skill-pass-4.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/mania-skill-pass-5.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/mania-skill-pass-6.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/mania-skill-pass-7.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/mania-skill-pass-8.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/osu-combo-1000.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/osu-combo-2000.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/osu-combo-500.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/osu-combo-750.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/osu-skill-fc-1.png
Binary file added assets/medals/osu-skill-fc-10.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/[email protected]
Binary file added assets/medals/osu-skill-fc-2.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/osu-skill-fc-3.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/osu-skill-fc-4.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/osu-skill-fc-5.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/osu-skill-fc-6.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/osu-skill-fc-7.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/osu-skill-fc-8.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/osu-skill-fc-9.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/osu-skill-pass-1.png
Binary file added assets/medals/osu-skill-pass-10.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/[email protected]
Binary file added assets/medals/osu-skill-pass-2.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/osu-skill-pass-3.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/osu-skill-pass-4.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/osu-skill-pass-5.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/osu-skill-pass-6.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/osu-skill-pass-7.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/osu-skill-pass-8.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/osu-skill-pass-9.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/taiko-skill-fc-1.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/taiko-skill-fc-2.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/taiko-skill-fc-3.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/taiko-skill-fc-4.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/taiko-skill-fc-5.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/taiko-skill-fc-6.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/taiko-skill-fc-7.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/taiko-skill-fc-8.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/taiko-skill-pass-1.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/taiko-skill-pass-2.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/taiko-skill-pass-3.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/taiko-skill-pass-4.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/taiko-skill-pass-5.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/taiko-skill-pass-6.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/taiko-skill-pass-7.png
Binary file added assets/medals/[email protected]
Binary file added assets/medals/taiko-skill-pass-8.png
Binary file added assets/medals/[email protected]
20 changes: 0 additions & 20 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,6 @@
# (We do not need an asynchronous http client for our tests)


@pytest.fixture(autouse=True)
def mock_out_initial_image_downloads(respx_mock: respx.MockRouter) -> None:
# mock out default avatar download
respx_mock.get("https://i.cmyui.xyz/U24XBZw-4wjVME-JaEz3.png").mock(
return_value=httpx.Response(
status_code=status.HTTP_200_OK,
headers={"Content-Type": "image/png"},
content=b"i am a png file",
),
)
# mock out achievement image downloads
respx_mock.get(url__regex=r"https://assets.ppy.sh/medals/client/.+").mock(
return_value=httpx.Response(
status_code=status.HTTP_200_OK,
headers={"Content-Type": "image/png"},
content=b"i am a png file",
),
)


@pytest.fixture
async def app() -> AsyncIterator[ASGIApp]:
async with LifespanManager(
Expand Down
Loading