from __main__ import app from flask import jsonify, request, send_from_directory import base64 import json import modules.db_connect from modules.functions import reduceartcv2 import os contentpath = modules.db_connect.contentpath() # Fetch and present all information from one _index.nfo-file @app.route('/game', methods=['POST']) def game(predefinednfo=False, return_dict=False, skip_artwork=False): nfopath = "" if predefinednfo is not False: nfopath = predefinednfo else: nfopath = base64.b64decode(request.json).decode() nfo = json.load(open(nfopath, 'r')) nfo['path'] = base64.b64encode(nfopath.encode('utf-8')).decode() # Add front cover artwork in medium size artpath = os.path.dirname(nfopath)+'/art/' i = 0 if 'artwork' in nfo['game']: if nfo['game']['artwork']: for art in nfo['game']['artwork']: try: if skip_artwork is False: nfo['game']['artwork'][i]['data'] = reduceartcv2( artpath+art['filename'], 'thumbnail') if art['type'] == 'front': nfo['game']['displayimage'] = reduceartcv2( artpath+art['filename'], 'thumbnail') except Exception as e: print("Failing cover: " + art['filename']) print(e) i += 1 if 'displayimage' not in nfo['game']: for art in nfo['game']['artwork']: if art['type'] in ['cd', 'dvd']: nfo['game']['displayimage'] = reduceartcv2( artpath+art['filename'], 'thumbnail') else: nfo['game']['displayimage'] = "" if return_dict is False: return jsonify(nfo) return nfo # Fetch all artwork from a given _index.nfo-file @app.route('/game/artwork/size/', methods=['POST']) @app.route('/game/artwork', methods=['POST']) def artwork(size='max'): f = open(request.json['nfo'], 'r') nfo = json.load(f) artpath = os.path.dirname(request.json['nfo'])+'/art/' artlist = [] for art in nfo['game']['artwork']: img = "" # If max size, send image as-is if size == 'max': with open(artpath+art['filename'], mode='rb') as file: img = file.read() art['img'] = base64.b64encode(img).decode() # Changes filetype to jpeg for size reduction. This also drops # Alpha-channel else: art['img'] = reduceartcv2(artpath+art['filename'], size) artlist.append(art) return jsonify(artlist) # Server game manual @app.route('/getmanual', methods=["GET"]) def getmanual(): nfopath = base64.b64decode(request.json).decode() nfo = json.load(open(nfopath, 'r')) nfo['path'] = base64.b64encode(nfopath.encode('utf-8')).decode() manualname = nfo['game']['manual'] manualpath = os.path.dirname(nfopath)+'/' return send_from_directory(manualpath, manualname, as_attachment=True)