Added direct download as zip #28

Merged
odecif merged 1 commits from download_as_zip into master 2023-09-19 02:23:57 +02:00
6 changed files with 51 additions and 6 deletions

View File

@ -1,5 +1,8 @@
import cv2 import cv2
import base64 import base64
from io import BytesIO
import zipfile
import pathlib
global gamelist global gamelist
gamelist = [] gamelist = []
@ -61,3 +64,14 @@ def image_resize(image, width=None, height=None, inter=cv2.INTER_AREA):
# return the resized image # return the resized image
return resized 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

View File

@ -1,9 +1,9 @@
from __main__ import app 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 base64
import json import json
import modules.db_connect import modules.db_connect
from modules.functions import reduceartcv2 from modules.functions import reduceartcv2, make_archive
import os import os
contentpath = modules.db_connect.contentpath() contentpath = modules.db_connect.contentpath()
@ -86,3 +86,18 @@ def getmanual():
manualname = nfo['game']['manual'] manualname = nfo['game']['manual']
manualpath = os.path.dirname(nfopath)+'/' manualpath = os.path.dirname(nfopath)+'/'
return send_from_directory(manualpath, manualname, as_attachment=True) 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')

View File

@ -17,7 +17,8 @@
"lang_game_manual_not_found": "Manual not available", "lang_game_manual_not_found": "Manual not available",
"lang_game_download_header": "Download game", "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_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_download_magnet_link": "Magnet link",
"lang_game_artwork": "Artwork", "lang_game_artwork": "Artwork",
"lang_game_year": "Release date", "lang_game_year": "Release date",

View File

@ -4,5 +4,6 @@
"lang_home_change_language": "Byt språk", "lang_home_change_language": "Byt språk",
"lang_home_change_language_button": "Byt", "lang_home_change_language_button": "Byt",
"lang_navigation_home": "Hem", "lang_navigation_home": "Hem",
"lang_navigation_gamelist": "Spellista" "lang_navigation_gamelist": "Spellista",
"lang_game_download_zip_button": "Ladda ned Zip"
} }

View File

@ -46,6 +46,19 @@ def artwork(lang_code):
# Download a game # Download a game
@app.route("/<lang_code>/game/download") @app.route("/<lang_code>/game/download")
def download(lang_code): def download(lang_code):
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 pass

View File

@ -22,7 +22,8 @@
{% endif%} {% endif%}
<h3>{{lang_game_download_header}}</h3> <h3>{{lang_game_download_header}}</h3>
<p>{{lang_game_download_ingress}}</p> <p>{{lang_game_download_ingress}}</p>
<a href="{{ url_for('download', gamepath=game.path, lang_code=lang_code)}}"><button type="button">{{lang_game_download_torrent_button}}</button></a> <a href="{{ url_for('download', targettype="torrent", gametitle=game.game.title, gamepath=game.path, lang_code=lang_code)}}"><button type="button">{{lang_game_download_torrent_button}}</button></a>
<a href="{{ url_for('download', targettype="zip", gamepath=game.path, gametitle=game.game.title, lang_code=lang_code)}}"><button type="button">{{lang_game_download_zip_button}}</button></a>
<p><a href="">{{lang_game_download_magnet_link}}</a></p> <p><a href="">{{lang_game_download_magnet_link}}</a></p>
<table class="game"> <table class="game">
<tr> <tr>