You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
3.1 KiB

import json
import os
import os.path
from collections import defaultdict
from sanic import Sanic
from sanic.log import logger
def load_assets():
config = Sanic.get_app().config
if config["ASSETS_LIST_PATH"] and os.path.exists(config["ASSETS_LIST_PATH"]):
with open(config["ASSETS_LIST_PATH"], "r") as f:
assets_list = json.load(f,
object_hook=lambda d: {int(k) if k.lstrip('-').isdigit() else k: v for k, v in d.items()})
logger.info(f"Loaded packages list from {config['ASSETS_LIST_PATH']}")
else:
assets_list = { "Android": {}, "iOS": {} }
for i in [0, 1, 2, 3, 4, 5, 6, 99]:
assets_list["Android"][i] = defaultdict(list)
assets_list["iOS"][i] = defaultdict(list)
android_i = 0
tmp_android_package = []
if config["ASSETS_PATH_ANDROID"]:
for r, d, fs in os.walk(config["ASSETS_PATH_ANDROID"]):
for f in fs:
if f.endswith(".zip"):
package_type, package_id, package_order = [int(i) for i in f.replace(".zip", "").split("_")]
package_size = os.stat(os.path.join(r, f)).st_size
tmp_android_package.append((package_type, package_id, package_order, package_size))
android_i += 1
tmp_android_package.sort(key=lambda x: x[2])
tmp_android_package.sort(key=lambda x: x[1])
for p in tmp_android_package:
assets_list["Android"][p[0]][p[1]].append((p[2], p[3]))
logger.info(f"Loaded {android_i} Android packages from {config['ASSETS_PATH_ANDROID']}")
ios_i = 0
tmp_ios_package = []
if config["ASSETS_PATH_IOS"]:
for r, d, fs in os.walk(config["ASSETS_PATH_IOS"]):
for f in fs:
if f.endswith(".zip"):
package_type, package_id, package_order = [int(i) for i in f.replace(".zip", "").split("_")]
package_size = os.stat(os.path.join(r, f)).st_size
tmp_ios_package.append((package_type, package_id, package_order, package_size))
ios_i += 1
tmp_ios_package.sort(key=lambda x: x[2])
tmp_ios_package.sort(key=lambda x: x[1])
for p in tmp_ios_package:
assets_list["iOS"][p[0]][p[1]].append((p[2], p[3]))
logger.info(f"Loaded {ios_i} iOS packages from {config['ASSETS_PATH_IOS']}")
dirname = os.path.dirname(config["ASSETS_LIST_PATH"])
if not os.path.exists(dirname): os.makedirs(dirname)
with open(config["ASSETS_LIST_PATH"], "w") as f:
json.dump(assets_list, f)
Sanic.get_app().ctx.assets_list = assets_list
if config["ASSETS_PATH_ANDROID"]:
Sanic.get_app().static("/Android", config["ASSETS_PATH_ANDROID"])
logger.info(f"Hosted {config['ASSETS_PATH_ANDROID']} to {config['STATIC_ROOT']}/Android")
if config["ASSETS_PATH_IOS"]:
Sanic.get_app().static("/iOS", config["ASSETS_PATH_IOS"])
logger.info(f"Hosted {config['ASSETS_PATH_IOS']} to {config['STATIC_ROOT']}/iOS")