feat: mod pack download

This commit is contained in:
Kentai Radiquum 2025-05-06 23:51:15 +05:00
parent c01fc7bd5a
commit 56569917c1
Signed by: Radiquum
GPG key ID: 858E8EE696525EED
8 changed files with 346 additions and 15 deletions

View file

@ -2,6 +2,8 @@ from flask import Blueprint
apiPack = Blueprint("pack", __name__, url_prefix="/api/pack")
apiPacks = Blueprint("packs", __name__, url_prefix="/api/packs")
apiDownload = Blueprint("download", __name__, url_prefix="/api/download")
from . import pack
from . import packs
from . import download

110
src/api/download.py Normal file
View file

@ -0,0 +1,110 @@
import os
from . import apiDownload
from flask import request, jsonify, send_file, redirect, url_for, abort
from config import PACKS_FOLDER
import json
from flask_socketio import emit
import requests
def download(path, url, name, total):
r = requests.get(url, stream=True)
if r.status_code != 200:
emit(
"download_current",
{
"status": "error",
"message": f"Got a HTTP ERROR {r.status_code} while downloading {name}",
},
)
return {
"status": "error",
"message": f"Got a HTTP ERROR {r.status_code} while downloading {name}",
}
downloaded = 0
if os.path.exists(f"{path}/{name}"):
emit(
"download_current",
{"status": "ok", "message": f"{name} already downloaded"},
namespace="/",
broadcast=True,
)
return {"status": "ok", "message": f"{name} already downloaded"}
with open(f"{path}/{name}", "wb") as fp:
for data in r.iter_content(chunk_size=1024):
size = fp.write(data)
downloaded += size
emit(
"download_current",
{
"status": "pending",
"total_bytes": total,
"download_bytes": downloaded,
},
namespace="/",
broadcast=True,
)
emit(
"download_current",
{"status": "ok", "message": f"{name} downloaded"},
namespace="/",
broadcast=True,
)
return {
"status": "ok",
"message": f"{name} downloaded",
}
@apiDownload.route("/pack", methods=["POST"])
def downloadPack():
pack = {}
pack_id = request.json.get("pack_id")
with open(f"{PACKS_FOLDER}/{pack_id}/packfile.json") as fp:
pack = json.load(fp)
fp.close()
mods = pack.get("mods", [])
total = len(mods)
os.makedirs(f"{PACKS_FOLDER}/{pack_id}/mods", exist_ok=True)
for i, mod in enumerate(mods):
emit(
"download_total",
{
"status": "ok",
"total": total,
"current": i,
"title": mod.get("title"),
"filename": mod.get("file").get("filename"),
},
namespace="/",
broadcast=True,
)
download(
f"{PACKS_FOLDER}/{pack_id}/mods",
mod.get("file").get("url"),
mod.get("file").get("filename"),
mod.get("file").get("size"),
)
emit(
"download_total",
{
"status": "ok",
"total": total,
"current": total,
"title": "",
"filename": "",
},
namespace="/",
broadcast=True,
)
return jsonify(
{
"status": "ok",
"message": f"download of {pack_id} with {total} mods finished",
},
)