From 9b7da75154a0f926b4a887ced6ca6a72ce3bf7dc Mon Sep 17 00:00:00 2001 From: odecif Date: Tue, 19 Sep 2023 02:22:16 +0200 Subject: [PATCH] Added direct download as zip [fix #22] --- api/modules/functions.py | 14 ++++++++++++++ api/modules/game.py | 19 +++++++++++++++++-- site/language/en_US.json | 3 ++- site/language/sv_SE.json | 3 ++- site/modules/game.py | 15 ++++++++++++++- site/templates/game.html | 3 ++- 6 files changed, 51 insertions(+), 6 deletions(-) diff --git a/api/modules/functions.py b/api/modules/functions.py index a66e192..058cb6f 100644 --- a/api/modules/functions.py +++ b/api/modules/functions.py @@ -1,5 +1,8 @@ import cv2 import base64 +from io import BytesIO +import zipfile +import pathlib global gamelist gamelist = [] @@ -61,3 +64,14 @@ def image_resize(image, width=None, height=None, inter=cv2.INTER_AREA): # return the resized image return resized + + +# Create a ZIP-file on the fly for downloading games quicker +def make_archive(dirname, path): + base_path = pathlib.Path(path) + b = BytesIO() + with zipfile.ZipFile(b, mode="w", compression=zipfile.ZIP_DEFLATED) as z: + for f_name in base_path.iterdir(): + z.write(f_name, arcname=f_name.name) + b.seek(0) + return b diff --git a/api/modules/game.py b/api/modules/game.py index d485ac1..5a67c79 100644 --- a/api/modules/game.py +++ b/api/modules/game.py @@ -1,9 +1,9 @@ from __main__ import app -from flask import jsonify, request, send_from_directory +from flask import jsonify, request, send_from_directory, send_file import base64 import json import modules.db_connect -from modules.functions import reduceartcv2 +from modules.functions import reduceartcv2, make_archive import os contentpath = modules.db_connect.contentpath() @@ -86,3 +86,18 @@ def getmanual(): manualname = nfo['game']['manual'] manualpath = os.path.dirname(nfopath)+'/' return send_from_directory(manualpath, manualname, as_attachment=True) + + +# Game folder as ZIP-file (kinda slow) +@app.route('/getzipfile', methods=["GET"]) +def getzipfile(): + nfopath = base64.b64decode(request.json).decode() + nfo = json.load(open(nfopath, 'r')) + nfo['path'] = base64.b64encode(nfopath.encode('utf-8')).decode() + path = os.path.dirname(nfopath) + dirname = os.path.basename(nfopath) + return send_file( + make_archive(dirname, path), + mimetype='application/zip', + as_attachment=True, + download_name=str(dirname) + '.zip') diff --git a/site/language/en_US.json b/site/language/en_US.json index 79e08a8..d38760d 100644 --- a/site/language/en_US.json +++ b/site/language/en_US.json @@ -17,7 +17,8 @@ "lang_game_manual_not_found": "Manual not available", "lang_game_download_header": "Download game", "lang_game_download_ingress": "Here you can download the game using either torrent file or a magnet-link!", - "lang_game_download_torrent_button": "Download", + "lang_game_download_zip_button": "Download Zip", + "lang_game_download_torrent_button": "Download Torrent", "lang_game_download_magnet_link": "Magnet link", "lang_game_artwork": "Artwork", "lang_game_year": "Release date", diff --git a/site/language/sv_SE.json b/site/language/sv_SE.json index 172ddf6..193c5e4 100644 --- a/site/language/sv_SE.json +++ b/site/language/sv_SE.json @@ -4,5 +4,6 @@ "lang_home_change_language": "Byt språk", "lang_home_change_language_button": "Byt", "lang_navigation_home": "Hem", - "lang_navigation_gamelist": "Spellista" + "lang_navigation_gamelist": "Spellista", + "lang_game_download_zip_button": "Ladda ned Zip" } diff --git a/site/modules/game.py b/site/modules/game.py index 2c1c88a..9a5433c 100644 --- a/site/modules/game.py +++ b/site/modules/game.py @@ -46,7 +46,20 @@ def artwork(lang_code): # Download a game @app.route("//game/download") def download(lang_code): - pass + gamepath = request.args.get("gamepath") + gametitle = request.args.get("gametitle") + targettype = request.args.get("targettype") + if "zip" in targettype: + gamezip = requests.get(host_endpoint + '/getzipfile', json=gamepath) + return Response(gamezip, mimetype="application/zip", + headers={ + "Content-Disposition": + "attachment;filename=" + str(gametitle)}) + elif "torrent" in targettype: + pass + + else: + pass # Download manual diff --git a/site/templates/game.html b/site/templates/game.html index 39e2f94..52a7267 100644 --- a/site/templates/game.html +++ b/site/templates/game.html @@ -22,7 +22,8 @@ {% endif%}

{{lang_game_download_header}}

{{lang_game_download_ingress}}

- + +

{{lang_game_download_magnet_link}}