You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.1 KiB
Python

6 years ago
"""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 '''
<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form action="/tiles/1/0/1" method=post enctype=multipart/form-data>
<input type=file name=file>
<input type=submit value=Upload>
</form>
'''
@tiles_bp.route('/tiles/<int:z>/<int:x>/<int:y>', 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/<int:z>/<int:x>/<int:y>', 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'))