mirror of
https://github.com/Radiquum/YAMPD.git
synced 2025-05-20 23:59:35 +05:00
add types and decouple packs functions from flask
This commit is contained in:
parent
ee133e9111
commit
f4d15c5eaf
7 changed files with 174 additions and 48 deletions
|
@ -1,70 +1,37 @@
|
|||
import os
|
||||
from . import apiPacks
|
||||
from flask import request, jsonify
|
||||
from config import PACKS_FOLDER
|
||||
import json
|
||||
import shutil
|
||||
from shared.packs import getPacks, createPack, deletePack
|
||||
|
||||
|
||||
@apiPacks.route("/all", methods=["GET"])
|
||||
def getPacks():
|
||||
packs = []
|
||||
|
||||
if not os.path.exists(f"{PACKS_FOLDER}"):
|
||||
os.makedirs(f"{PACKS_FOLDER}", exist_ok=True)
|
||||
return jsonify(packs)
|
||||
|
||||
pack_folders = [f.name for f in os.scandir(PACKS_FOLDER) if f.is_dir()]
|
||||
for pack_folder in pack_folders:
|
||||
if not os.path.exists(f"{PACKS_FOLDER}/{pack_folder}/packfile.json"):
|
||||
continue
|
||||
with open(f"{PACKS_FOLDER}/{pack_folder}/packfile.json") as fp:
|
||||
pack = json.load(fp)
|
||||
pack["_id"] = pack_folder
|
||||
packs.append(pack)
|
||||
fp.close()
|
||||
return jsonify(packs)
|
||||
def getPacksEndpoint():
|
||||
return jsonify(getPacks())
|
||||
|
||||
|
||||
@apiPacks.route("/new", methods=["POST"])
|
||||
def createPack():
|
||||
pack = {
|
||||
"formatVersion": 0,
|
||||
"modpackVersion": 0,
|
||||
"title": request.json.get("title"),
|
||||
"author": request.json.get("author"),
|
||||
"version": request.json.get("version"),
|
||||
"modloader": request.json.get("modloader"),
|
||||
"updateURL": "",
|
||||
"mods": [],
|
||||
}
|
||||
title = pack.get("title").replace(" ", "_")
|
||||
def createPackEndpoint():
|
||||
pack, is_exists = createPack(
|
||||
request.json.get("title"),
|
||||
request.json.get("author"),
|
||||
request.json.get("version"),
|
||||
request.json.get("modloader"),
|
||||
)
|
||||
|
||||
if os.path.exists(f"{PACKS_FOLDER}/{title}"):
|
||||
if is_exists:
|
||||
return jsonify({"status": "error", "message": "pack already exists"})
|
||||
|
||||
os.makedirs(f"{PACKS_FOLDER}/{title}", exist_ok=True)
|
||||
|
||||
with open(
|
||||
os.path.abspath(f"{PACKS_FOLDER}/{title}/packfile.json"),
|
||||
mode="w",
|
||||
encoding="utf-8",
|
||||
) as fp:
|
||||
json.dump(pack, fp)
|
||||
fp.close()
|
||||
|
||||
return jsonify(
|
||||
{
|
||||
"status": "ok",
|
||||
"message": f"pack {pack.get('title')} created",
|
||||
"id": title,
|
||||
"message": f"pack {pack.title} created",
|
||||
"id": pack._id,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@apiPacks.route("/<id>/delete", methods=["GET"])
|
||||
def deletePack(id):
|
||||
shutil.rmtree(f"{PACKS_FOLDER}/{id}")
|
||||
def deletePackEndpoint(id):
|
||||
deletePack(id)
|
||||
return jsonify(
|
||||
{
|
||||
"status": "ok",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue