2022-12-01 18:48:31 +00:00
|
|
|
from flask import Flask, render_template, send_from_directory, abort, url_for, jsonify, redirect, request, session
|
|
|
|
from werkzeug.utils import secure_filename
|
|
|
|
import os
|
|
|
|
|
|
|
|
# Get database stuff
|
|
|
|
DB_USER = os.environ.get('USERNAME')
|
|
|
|
DB_PASS = os.environ.get('PASSWORD')
|
|
|
|
DB_HOST = os.environ.get('HOST')
|
|
|
|
DB_PORT = os.environ.get('PORT')
|
|
|
|
|
|
|
|
DB = os.environ.get('DATABASE')
|
|
|
|
|
|
|
|
# Set flask config
|
|
|
|
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
2022-12-14 19:55:40 +00:00
|
|
|
UPLOAD_FOLDER = os.path.join(BASE_DIR, 'uploads')
|
2022-12-01 18:48:31 +00:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
2022-12-14 19:55:40 +00:00
|
|
|
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
2022-12-01 18:48:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# ERROR HANDLERS
|
|
|
|
#
|
|
|
|
@app.errorhandler(405)
|
|
|
|
def method_not_allowed(e):
|
|
|
|
error = '405'
|
|
|
|
msg = 'Method sussy wussy'
|
2022-12-14 19:55:40 +00:00
|
|
|
return render_template('error.html', error=error, msg=msg), 404
|
|
|
|
|
2022-12-01 18:48:31 +00:00
|
|
|
|
|
|
|
@app.errorhandler(404)
|
|
|
|
def page_not_found(e):
|
|
|
|
error = '404'
|
|
|
|
msg = 'Could not find what you need!'
|
2022-12-14 19:55:40 +00:00
|
|
|
return render_template('error.html', error=error, msg=msg), 404
|
|
|
|
|
2022-12-01 18:48:31 +00:00
|
|
|
|
|
|
|
@app.errorhandler(403)
|
|
|
|
def forbidden(e):
|
|
|
|
error = '403'
|
|
|
|
msg = 'Go away! This is no place for you!'
|
2022-12-14 19:55:40 +00:00
|
|
|
return render_template('error.html', error=error, msg=msg), 403
|
|
|
|
|
2022-12-01 18:48:31 +00:00
|
|
|
|
|
|
|
@app.errorhandler(410)
|
|
|
|
def gone(e):
|
|
|
|
error = '410'
|
|
|
|
msg = 'The page is no longer available! *sad face*'
|
2022-12-14 19:55:40 +00:00
|
|
|
return render_template('error.html', error=error, msg=msg), 410
|
|
|
|
|
2022-12-01 18:48:31 +00:00
|
|
|
|
|
|
|
@app.errorhandler(500)
|
|
|
|
def internal_server_error(e):
|
|
|
|
error = '500'
|
|
|
|
msg = 'Server died inside :c'
|
2022-12-14 19:55:40 +00:00
|
|
|
return render_template('error.html', error=error, msg=msg), 500
|
2022-12-01 18:48:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# ROUTES
|
|
|
|
#
|
|
|
|
@app.route('/')
|
|
|
|
def home():
|
2022-12-14 19:55:40 +00:00
|
|
|
return render_template('home.html')
|
|
|
|
|
2022-12-01 18:48:31 +00:00
|
|
|
|
2022-12-14 19:55:40 +00:00
|
|
|
@app.route('/image/<file_id>')
|
|
|
|
def image(file_id):
|
2022-12-01 18:48:31 +00:00
|
|
|
try:
|
2022-12-14 19:55:40 +00:00
|
|
|
file_id = int(file_id)
|
2022-12-01 18:48:31 +00:00
|
|
|
except ValueError:
|
|
|
|
abort(404)
|
2022-12-14 19:55:40 +00:00
|
|
|
|
|
|
|
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)
|
2022-12-01 18:48:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# METHODS
|
|
|
|
#
|
2022-12-14 19:55:40 +00:00
|
|
|
@app.route('/fileList/<item_type>', methods=['GET'])
|
|
|
|
def image_list(item_type):
|
|
|
|
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)
|
|
|
|
|
|
|
|
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)
|