allow webhost handling of APMC files
This commit is contained in:
		
							parent
							
								
									685de847c4
								
							
						
					
					
						commit
						de31fc320c
					
				|  | @ -187,8 +187,9 @@ if __name__ == "__main__": | ||||||
| 
 | 
 | ||||||
|             with concurrent.futures.ThreadPoolExecutor() as pool: |             with concurrent.futures.ThreadPoolExecutor() as pool: | ||||||
|                 futures = [] |                 futures = [] | ||||||
|  |                 files = os.listdir(output_path) | ||||||
|                 with zipfile.ZipFile(zipname, "w", compression=compression, compresslevel=9) as zf: |                 with zipfile.ZipFile(zipname, "w", compression=compression, compresslevel=9) as zf: | ||||||
|                     for file in os.listdir(output_path): |                     for file in files: | ||||||
|                         if seed_name in file: |                         if seed_name in file: | ||||||
|                             if file.endswith(".sfc"): |                             if file.endswith(".sfc"): | ||||||
|                                 futures.append(pool.submit(_handle_sfc_file, file)) |                                 futures.append(pool.submit(_handle_sfc_file, file)) | ||||||
|  | @ -196,6 +197,9 @@ if __name__ == "__main__": | ||||||
|                                 futures.append(pool.submit(_handle_diff_file, file)) |                                 futures.append(pool.submit(_handle_diff_file, file)) | ||||||
|                             elif file.endswith(".apmc"): |                             elif file.endswith(".apmc"): | ||||||
|                                 futures.append(pool.submit(_handle_apmc_file, file)) |                                 futures.append(pool.submit(_handle_apmc_file, file)) | ||||||
|  |                             # just handle like a diff file for now | ||||||
|  |                             elif file.endswith(".zip"): | ||||||
|  |                                 futures.append(pool.submit(_handle_diff_file, file)) | ||||||
| 
 | 
 | ||||||
|                     if zip_multidata and os.path.exists(os.path.join(output_path, multidataname)): |                     if zip_multidata and os.path.exists(os.path.join(output_path, multidataname)): | ||||||
|                         pack_file(multidataname) |                         pack_file(multidataname) | ||||||
|  |  | ||||||
|  | @ -44,3 +44,20 @@ def download_raw_patch(seed_id, player_id: int): | ||||||
| 
 | 
 | ||||||
|         fname = f"P{patch.player_id}_{patch.player_name}_{app.jinja_env.filters['suuid'](seed_id)}.apbp" |         fname = f"P{patch.player_id}_{patch.player_name}_{app.jinja_env.filters['suuid'](seed_id)}.apbp" | ||||||
|         return send_file(patch_data, as_attachment=True, attachment_filename=fname) |         return send_file(patch_data, as_attachment=True, attachment_filename=fname) | ||||||
|  | 
 | ||||||
|  | @app.route("/slot_file/<suuid:seed_id>/<int:player_id>") | ||||||
|  | def download_slot_file(seed_id, player_id: int): | ||||||
|  |     seed = Seed.get(id=seed_id) | ||||||
|  |     slot_data: Slot = select(patch for patch in seed.slots if | ||||||
|  |                    patch.player_id == player_id).first() | ||||||
|  | 
 | ||||||
|  |     if not slot_data: | ||||||
|  |         return "Slot Data not found" | ||||||
|  |     else: | ||||||
|  |         import io | ||||||
|  | 
 | ||||||
|  |         if slot_data.game == "Minecraft": | ||||||
|  |             fname = f"AP_{app.jinja_env.filters['suuid'](seed_id)}_P{slot_data.player_id}_{slot_data.player_name}.apmc" | ||||||
|  |         else: | ||||||
|  |             return "Game download not supported." | ||||||
|  |         return send_file(io.BytesIO(slot_data.data), as_attachment=True, attachment_filename=fname) | ||||||
|  | @ -21,7 +21,7 @@ | ||||||
|         you can simply refresh this page and the server will be started again.<br> |         you can simply refresh this page and the server will be started again.<br> | ||||||
|         {% if room.last_port %} |         {% if room.last_port %} | ||||||
|             You can connect to this room by using '/connect archipelago.gg:{{ room.last_port }}' |             You can connect to this room by using '/connect archipelago.gg:{{ room.last_port }}' | ||||||
|             in the <a href="https://github.com/Berserker66/MultiWorld-Utilities/releases">client</a>.<br>{% endif %} |             in the <a href="{{ url_for("tutorial_landing")}}">client</a>.<br>{% endif %} | ||||||
|         {{ macros.list_patches_room(room) }} |         {{ macros.list_patches_room(room) }} | ||||||
|         {% if room.owner == session["_id"] %} |         {% if room.owner == session["_id"] %} | ||||||
|             <form method=post> |             <form method=post> | ||||||
|  |  | ||||||
|  | @ -10,8 +10,13 @@ | ||||||
|     {% if room.seed.slots %} |     {% if room.seed.slots %} | ||||||
|         <ul> |         <ul> | ||||||
|             {% for patch in room.seed.slots|list|sort(attribute="player_id") %} |             {% for patch in room.seed.slots|list|sort(attribute="player_id") %} | ||||||
|  |                 {% if patch.game == "Minecraft" %} | ||||||
|  |                 <li><a href="{{ url_for("download_slot_file", seed_id=room.seed.id, player_id=patch.player_id) }}"> | ||||||
|  |                     APMC for player {{ patch.player_id }} - {{ patch.player_name }}</a></li> | ||||||
|  |                 {% else %} | ||||||
|                 <li><a href="{{ url_for("download_patch", patch_id=patch.id, room_id=room.id) }}"> |                 <li><a href="{{ url_for("download_patch", patch_id=patch.id, room_id=room.id) }}"> | ||||||
|                     Patch for player {{ patch.player_id }} - {{ patch.player_name }}</a></li> |                     Patch for player {{ patch.player_id }} - {{ patch.player_name }}</a></li> | ||||||
|  |                 {% endif %} | ||||||
|             {% endfor %} |             {% endfor %} | ||||||
|         </ul> |         </ul> | ||||||
|     {% endif %} |     {% endif %} | ||||||
|  |  | ||||||
|  | @ -1,5 +1,7 @@ | ||||||
| import zipfile | import zipfile | ||||||
| import lzma | import lzma | ||||||
|  | import json | ||||||
|  | import base64 | ||||||
| import MultiServer | import MultiServer | ||||||
| 
 | 
 | ||||||
| from flask import request, flash, redirect, url_for, session, render_template | from flask import request, flash, redirect, url_for, session, render_template | ||||||
|  | @ -43,11 +45,18 @@ def uploads(): | ||||||
|                                 yaml_data = parse_yaml(lzma.decompress(data).decode("utf-8-sig")) |                                 yaml_data = parse_yaml(lzma.decompress(data).decode("utf-8-sig")) | ||||||
|                                 if yaml_data["version"] < 2: |                                 if yaml_data["version"] < 2: | ||||||
|                                     return "Old format cannot be uploaded (outdated .apbp)", 500 |                                     return "Old format cannot be uploaded (outdated .apbp)", 500 | ||||||
| 
 |  | ||||||
|                                 metadata = yaml_data["meta"] |                                 metadata = yaml_data["meta"] | ||||||
|                                 slots.add(Slot(data=data, player_name=metadata["player_name"], |                                 slots.add(Slot(data=data, player_name=metadata["player_name"], | ||||||
|                                                player_id=metadata["player_id"], |                                                player_id=metadata["player_id"], | ||||||
|                                                game="A Link to the Past")) |                                                game="A Link to the Past")) | ||||||
|  | 
 | ||||||
|  |                             elif file.filename.endswith(".apmc"): | ||||||
|  |                                 data = zfile.open(file, "r").read() | ||||||
|  |                                 metadata = json.loads(base64.b64decode(data).decode("utf-8")) | ||||||
|  |                                 slots.add(Slot(data=data, player_name=metadata["player_name"], | ||||||
|  |                                                player_id=metadata["player_id"], | ||||||
|  |                                                game="Minecraft")) | ||||||
|  | 
 | ||||||
|                             elif file.filename.endswith(".txt"): |                             elif file.filename.endswith(".txt"): | ||||||
|                                 spoiler = zfile.open(file, "r").read().decode("utf-8-sig") |                                 spoiler = zfile.open(file, "r").read().decode("utf-8-sig") | ||||||
|                             elif file.filename.endswith(".archipelago"): |                             elif file.filename.endswith(".archipelago"): | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue