Loading images on scroll

Better api
This commit is contained in:
Michał Gdula 2022-12-14 19:55:40 +00:00
parent 4279ebed00
commit 3f64c56ec8
9 changed files with 169 additions and 87 deletions

77
app.py
View file

@ -2,7 +2,6 @@ from flask import Flask, render_template, send_from_directory, abort, url_for, j
from werkzeug.utils import secure_filename
import os
# Get database stuff
DB_USER = os.environ.get('USERNAME')
DB_PASS = os.environ.get('PASSWORD')
@ -11,13 +10,12 @@ DB_PORT = os.environ.get('PORT')
DB = os.environ.get('DATABASE')
# Set flask config
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
IMAGE_DIR = os.path.join(BASE_DIR, 'static/images')
UPLOAD_FOLDER = os.path.join(BASE_DIR, 'uploads')
app = Flask(__name__)
app.config['IMAGE_DIR'] = IMAGE_DIR
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
#
@ -27,31 +25,35 @@ app.config['IMAGE_DIR'] = IMAGE_DIR
def method_not_allowed(e):
error = '405'
msg = 'Method sussy wussy'
return render_template('error.html', error = error, msg = msg), 404
return render_template('error.html', error=error, msg=msg), 404
@app.errorhandler(404)
def page_not_found(e):
error = '404'
msg = 'Could not find what you need!'
return render_template('error.html', error = error, msg = msg), 404
return render_template('error.html', error=error, msg=msg), 404
@app.errorhandler(403)
def forbidden(e):
error = '403'
msg = 'Go away! This is no place for you!'
return render_template('error.html', error = error, msg = msg), 403
return render_template('error.html', error=error, msg=msg), 403
@app.errorhandler(410)
def gone(e):
error = '410'
msg = 'The page is no longer available! *sad face*'
return render_template('error.html', error = error, msg = msg), 410
return render_template('error.html', error=error, msg=msg), 410
@app.errorhandler(500)
def internal_server_error(e):
error = '500'
msg = 'Server died inside :c'
return render_template('error.html', error = error, msg = msg), 500
return render_template('error.html', error=error, msg=msg), 500
#
@ -59,33 +61,50 @@ def internal_server_error(e):
#
@app.route('/')
def home():
image_list = os.listdir(app.config['IMAGE_DIR'])
return render_template('home.html', images = image_list)
return render_template('home.html')
@app.route('/image/<id>')
def image(id):
@app.route('/image/<file_id>')
def image(file_id):
try:
id = int(id)
file_id = int(file_id)
except ValueError:
abort(404)
image_list = os.listdir(app.config['IMAGE_DIR'])
fileName = image_list[id]
return render_template('image.html', fileName = fileName, id = id)
file_list = os.listdir(os.path.join(app.config['UPLOAD_FOLDER'], 'original'))
file_name = file_list[file_id]
return render_template('image.html', fileName=file_name, id=file_id)
#
# METHODS
#
@app.route('/fileList', methods = ['GET'])
def imageList():
image_list = os.listdir(app.config['IMAGE_DIR'])
return jsonify(image_list)
@app.route('/fileList/<item_type>', methods=['GET'])
def image_list(item_type):
if request.method != 'GET':
abort(405)
@app.route('/file/<filename>', methods = ['GET'])
def pfp(filename):
if (request.method == 'GET'):
return send_from_directory(app.config['IMAGE_DIR'], filename)
else:
return None
item_type = secure_filename(item_type)
type_dir = os.path.join(app.config['UPLOAD_FOLDER'], item_type)
if not os.path.isdir(type_dir):
abort(404)
return jsonify(os.listdir(type_dir))
@app.route('/uploads/<item_type>/<file_id>', methods=['GET'])
def uploads(item_type, file_id):
if request.method != 'GET':
abort(405)
item_type = secure_filename(item_type)
type_dir = os.path.join(app.config['UPLOAD_FOLDER'], item_type)
if not os.path.isdir(type_dir):
abort(404)
file_id = secure_filename(file_id)
if not os.path.isfile(os.path.join(type_dir, file_id)):
abort(404)
return send_from_directory(type_dir, file_id)