mirror of
https://github.com/Radiquum/YAMPD.git
synced 2025-05-20 23:59:35 +05:00
feat: pack creating
This commit is contained in:
parent
5be021789b
commit
af7b8a6fea
27 changed files with 677 additions and 130 deletions
5
src/api/__init__.py
Normal file
5
src/api/__init__.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
from flask import Blueprint
|
||||
|
||||
api = Blueprint('api', __name__, url_prefix="/api/pack")
|
||||
|
||||
from . import pack
|
71
src/api/pack.py
Normal file
71
src/api/pack.py
Normal file
|
@ -0,0 +1,71 @@
|
|||
import os
|
||||
import re
|
||||
from . import api
|
||||
from flask import request, jsonify
|
||||
from config import PACKS_FOLDER, IMG_ALLOWED_MIME
|
||||
import json
|
||||
from PIL import Image
|
||||
from io import BytesIO
|
||||
import base64
|
||||
|
||||
@api.route("/new", methods=["POST"])
|
||||
def APIPackNew():
|
||||
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(" ", "_")
|
||||
|
||||
if os.path.exists(f"{PACKS_FOLDER}/{title}"):
|
||||
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,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@api.route("/<id>/image/edit", methods=["POST"])
|
||||
def APIPackImageEdit(id):
|
||||
|
||||
image_string = request.json.get("image")
|
||||
image_mime = request.json.get("mimetype")
|
||||
if image_string == None:
|
||||
return jsonify({"status": "error", "message": "no image provided"})
|
||||
if image_mime == None or image_mime not in IMG_ALLOWED_MIME:
|
||||
return jsonify({"status": "error", "message": "wrong image format"})
|
||||
|
||||
image_data = base64.b64decode(re.sub("^data:image/.+;base64,", "", request.json.get("image")))
|
||||
|
||||
image = Image.open(BytesIO(image_data))
|
||||
image = image.resize((512, 512), Image.Resampling.LANCZOS)
|
||||
image.save(
|
||||
f"{PACKS_FOLDER}/{id}/packicon.png",
|
||||
"png",
|
||||
)
|
||||
|
||||
return jsonify(
|
||||
{
|
||||
"status": "ok",
|
||||
"message": "image updated",
|
||||
}
|
||||
)
|
6
src/config.py
Normal file
6
src/config.py
Normal file
|
@ -0,0 +1,6 @@
|
|||
import os
|
||||
PACKS_FOLDER = "./packs"
|
||||
if os.getenv("is_dev") == "True":
|
||||
PACKS_FOLDER = "../packs"
|
||||
|
||||
IMG_ALLOWED_MIME = {"image/png", "image/jpg", "image/jpeg", "image/webp", "image/jfif"}
|
|
@ -2,8 +2,13 @@ from flask import Flask
|
|||
from flask import render_template, redirect, url_for, send_file, abort
|
||||
from flaskwebgui import FlaskUI # import FlaskUI
|
||||
import os
|
||||
from flask_cors import CORS
|
||||
|
||||
from api import api
|
||||
|
||||
app = Flask(__name__)
|
||||
app.register_blueprint(api)
|
||||
CORS(app, resources={r"/*": {"origins": "*"}})
|
||||
|
||||
|
||||
@app.route("/")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue