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",
},
)

View file

@ -3,10 +3,9 @@ from flask import render_template, send_file, abort
from flaskwebgui import FlaskUI # import FlaskUI
import os
import sys
from flask_socketio import SocketIO
from api import apiPack
from api import apiPacks
from api import apiPack, apiPacks, apiDownload
def resource_path(relative_path):
@ -20,10 +19,11 @@ app = Flask(
static_folder=resource_path("static"),
template_folder=resource_path("templates"),
)
socketio = SocketIO(app, cors_allowed_origins="*")
app.register_blueprint(apiPack)
app.register_blueprint(apiPacks)
app.register_blueprint(apiDownload)
if os.getenv("is_dev") == "True":
@ -51,9 +51,23 @@ def page_not_found(e):
return render_template("404.html"), 404
@socketio.on("connect")
def handle_connect():
print("Client connected")
@socketio.on("disconnect")
def handle_disconnect():
print("Client disconnected")
if __name__ == "__main__":
if os.getenv("is_dev") == "True":
app.run(host="0.0.0.0", debug=True, use_reloader=True)
socketio.run(app, host="0.0.0.0", debug=True, use_reloader=True)
# app.run(host="0.0.0.0", debug=True, use_reloader=True)
else:
FlaskUI(app=app, server="flask").run()
# FlaskUI(app=app, server="flask").run()
FlaskUI(
app=app, socketio=socketio, server="flask_socketio", width=800, height=600
).run()