Move downloads to downloads.py

This commit is contained in:
Fabian Dill 2020-08-03 19:27:40 +02:00
parent 2a8f39a0a1
commit da3a65fd53
3 changed files with 49 additions and 45 deletions

View File

@ -136,4 +136,4 @@ def favicon():
from WebHostLib.customserver import run_server_process
from . import tracker, upload, landing, check, generate # to trigger app routing picking up on it
from . import tracker, upload, landing, check, generate, downloads # to trigger app routing picking up on it

47
WebHostLib/downloads.py Normal file
View File

@ -0,0 +1,47 @@
from flask import send_file, Response
from pony.orm import select
from Patch import update_patch_data
from WebHostLib import app, Patch, Room, Seed
@app.route("/dl_patch/<suuid:room_id>/<int:patch_id>")
def download_patch(room_id, patch_id):
patch = Patch.get(id=patch_id)
if not patch:
return "Patch not found"
else:
import io
room = Room.get(id=room_id)
last_port = room.last_port
pname = room.seed.multidata["names"][0][patch.player - 1]
patch_data = update_patch_data(patch.data, server="berserkermulti.world:" + str(last_port))
patch_data = io.BytesIO(patch_data)
fname = f"P{patch.player}_{pname}_{app.jinja_env.filters['suuid'](room_id)}.bmbp"
return send_file(patch_data, as_attachment=True, attachment_filename=fname)
@app.route("/dl_spoiler/<suuid:seed_id>")
def download_spoiler(seed_id):
return Response(Seed.get(id=seed_id).spoiler, mimetype="text/plain")
@app.route("/dl_raw_patch/<suuid:seed_id>/<int:player_id>")
def download_raw_patch(seed_id, player_id):
patch = select(patch for patch in Patch if patch.player == player_id and patch.seed.id == seed_id).first()
if not patch:
return "Patch not found"
else:
import io
pname = patch.seed.multidata["names"][0][patch.player - 1]
patch_data = update_patch_data(patch.data, server="")
patch_data = io.BytesIO(patch_data)
fname = f"P{patch.player}_{pname}_{app.jinja_env.filters['suuid'](seed_id)}.bmbp"
return send_file(patch_data, as_attachment=True, attachment_filename=fname)

View File

@ -4,12 +4,11 @@ import random
import zlib
import json
from flask import request, flash, redirect, url_for, session, render_template, send_file, Response
from flask import request, flash, redirect, url_for, session, render_template
from EntranceRandomizer import parse_arguments
from Main import main as ERmain
from Main import get_seed, seeddigits
from Patch import update_patch_data
from .models import *
from WebHostLib import app
@ -41,48 +40,6 @@ def generate(race=False):
return render_template("generate.html", race=race)
@app.route("/dl_patch/<int:patch_id>/<suuid:room_id>")
def download_patch(patch_id, room_id):
patch = Patch.get(id=patch_id)
if not patch:
return "Patch not found"
else:
import io
room = Room.get(id=room_id)
last_port = room.last_port
pname = room.seed.multidata["names"][0][patch.player - 1]
patch_data = update_patch_data(patch.data, server="berserkermulti.world:" + str(last_port))
patch_data = io.BytesIO(patch_data)
fname = f"P{patch.player}_{pname}_{app.jinja_env.filters['suuid'](room_id)}.bmbp"
return send_file(patch_data, as_attachment=True, attachment_filename=fname)
@app.route("/dl_spoiler/<suuid:seed_id>")
def download_spoiler(seed_id):
return Response(Seed.get(id=seed_id).spoiler, mimetype="text/plain")
@app.route("/dl_raw_patch/<suuid:seed_id>/<int:player_id>")
def download_raw_patch(seed_id, player_id):
patch = select(patch for patch in Patch if patch.player == player_id and patch.seed.id == seed_id).first()
if not patch:
return "Patch not found"
else:
import io
pname = patch.seed.multidata["names"][0][patch.player - 1]
patch_data = update_patch_data(patch.data, server="")
patch_data = io.BytesIO(patch_data)
fname = f"P{patch.player}_{pname}_{app.jinja_env.filters['suuid'](seed_id)}.bmbp"
return send_file(patch_data, as_attachment=True, attachment_filename=fname)
def gen(gen_options, race=False):
target = tempfile.TemporaryDirectory()
with target: