Example #1
0
def uploads():
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files:
            flash('No file part')
        else:
            file = request.files['file']
            # if user does not select file, browser also
            # submit an empty part without filename
            if file.filename == '':
                flash('No selected file')
            elif file and allowed_file(file.filename):
                if file.filename.endswith(".zip"):
                    patches = set()
                    spoiler = ""
                    multidata = None
                    with zipfile.ZipFile(file, 'r') as zfile:
                        infolist = zfile.infolist()

                        for file in infolist:
                            if file.filename.endswith(banned_zip_contents):
                                return "Uploaded data contained a rom file, which is likely to contain copyrighted material. Your file was deleted."
                            elif file.filename.endswith(".bmbp"):
                                player = int(file.filename.split("P")[1].split(".")[0].split("_")[0])
                                patches.add(Patch(data=zfile.open(file, "r").read(), player=player))
                            elif file.filename.endswith(".txt"):
                                spoiler = zfile.open(file, "r").read().decode("utf-8-sig")
                            elif file.filename.endswith("multidata"):
                                try:
                                    multidata = json.loads(zlib.decompress(zfile.open(file).read()).decode("utf-8-sig"))
                                except:
                                    flash("Could not load multidata. File may be corrupted or incompatible.")
                        if multidata:
                            commit()  # commit patches
                            seed = Seed(multidata=multidata, spoiler=spoiler, patches=patches, owner=session["_id"])
                            commit()  # create seed
                            for patch in patches:
                                patch.seed = seed

                            return redirect(url_for("view_seed", seed=seed.id))
                        else:
                            flash("No multidata was found in the zip file, which is required.")
                else:
                    try:
                        multidata = json.loads(zlib.decompress(file.read()).decode("utf-8-sig"))
                    except:
                        flash("Could not load multidata. File may be corrupted or incompatible.")
                    else:
                        seed = Seed(multidata=multidata, owner=session["_id"])
                        commit()  # place into DB and generate ids
                        return redirect(url_for("view_seed", seed=seed.id))
            else:
                flash("Not recognized file format. Awaiting a .multidata file.")
    rooms = select(room for room in Room if room.owner == session["_id"])
    return render_template("uploads.html", rooms=rooms)
Example #2
0
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

        patch_data = update_patch_data(
            patch.data, server=f"{app.config['PATCH_TARGET']}:{last_port}")
        patch_data = io.BytesIO(patch_data)

        fname = f"P{patch.player_id}_{patch.player_name}_{app.jinja_env.filters['suuid'](room_id)}.apbp"
        return send_file(patch_data,
                         as_attachment=True,
                         attachment_filename=fname)
Example #3
0
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)