"""Authentication routes""" # pylint: disable=invalid-name import os from PIL import Image from flask import Blueprint, render_template, session, redirect, url_for, \ request, flash, current_app, abort, send_from_directory from app import db from app.models import Tile tiles_bp = Blueprint('tiles', __name__) def valid_tile(z, x, y): """Returns true if the tile coordinates are valid""" # check z in [0, 22] if z < 0 or z > 22: return False # check x in [0, 2^zoom[ if x < 0 or x >= 2**z: return False # check y in [0, 2^zoom[ if y < 0 or y >= 2 ** z: return False return True @tiles_bp.route('/tiles/upload') def upload(): """Render an upload form""" return ''' Upload new File

Upload new File

''' @tiles_bp.route('/tiles///', methods=['GET']) def get_tile(z, x, y): """Serves a tile""" if not valid_tile(z, x, y): return abort(404) folder = current_app.config['UPLOAD_FOLDER'] filename = f'{z}-{x}-{y}.png' if not os.path.isfile(os.path.join(folder, filename)): return send_from_directory('static', 'missing.png') return send_from_directory('../' + folder, filename) @tiles_bp.route('/tiles///', methods=['POST']) def create_tile(z, x, y): if not valid_tile(z, x, y): return abort(404) if 'file' not in request.files: flash('No file part') return redirect(url_for('index')) file = request.files['file'] try: image = Image.open(file.stream) image = image.resize((256, 256)) except OSError: flash('cannot read image') return redirect(url_for('index')) folder = current_app.config['UPLOAD_FOLDER'] filename = f'{z}-{x}-{y}.png' image.save(os.path.join(folder, filename)) flash('Uploaded tile successfully') return redirect(url_for('index'))