diff --git a/.gitignore b/.gitignore index a52b68f..82f01cf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ # Remove all development files gallery/user/uploads/* +gallery/user/conf.yml +gallery/user/conf.json gallery/static/theme/* .idea diff --git a/gallery/__init__.py b/gallery/__init__.py index 046877b..8dcf6e8 100644 --- a/gallery/__init__.py +++ b/gallery/__init__.py @@ -12,6 +12,7 @@ Created by Fluffy Bean - Version 110123 import time import sys import os +import yaml # Import flask from flask import * @@ -26,13 +27,19 @@ def create_app(test_config=None): # Get environment variables load_dotenv(os.path.join(app.root_path, 'user', '.env')) + + # Get config file + with open(os.path.join(app.root_path, 'user', 'conf.yml'), 'r') as f: + conf = yaml.load(f, Loader=yaml.FullLoader) + print("Loaded config") + print(conf['upload']['allowed-extensions']) # App configuration app.config.from_mapping( SECRET_KEY=os.environ.get('FLASK_SECRET'), DATABASE=os.path.join(app.instance_path, 'gallery.sqlite'), UPLOAD_FOLDER=os.path.join(app.root_path, 'user', 'uploads'), - ALLOWED_EXTENSIONS=os.environ.get('FLASK_EXTENSIONS'), + ALLOWED_EXTENSIONS=conf['upload']['allowed-extensions'], ) if test_config is None: diff --git a/gallery/gallery.py b/gallery/gallery.py index c7500a2..ba5e1bc 100644 --- a/gallery/gallery.py +++ b/gallery/gallery.py @@ -5,8 +5,6 @@ from gallery.auth import login_required from gallery.db import get_db import os import datetime -from PIL import Image -from PIL.ExifTags import TAGS dt = datetime.datetime.now() blueprint = Blueprint('gallery', __name__) @@ -41,9 +39,11 @@ def upload(): if not file: flash('No selected file') return abort(404) - if secure_filename(file.filename).lower().split('.')[-1] in current_app.config['ALLOWED_EXTENSIONS']: - file_name = f"GWAGWA_{dt.year}{dt.month}{dt.day}-{dt.microsecond}.{secure_filename(file.filename).lower().split('.')[-1]}" - file.save(os.path.join(current_app.config['UPLOAD_FOLDER']+'/original', file_name)) + if not secure_filename(file.filename).lower().split('.')[-1] in current_app.config['ALLOWED_EXTENSIONS']: + abort(403) + + file_name = file_name = f"GWAGWA_{dt.year}{dt.month}{dt.day}-{dt.microsecond}.{secure_filename(file.filename).lower().split('.')[-1]}" + file.save(os.path.join(current_app.config['UPLOAD_FOLDER']+'/original', file_name)) db = get_db() db.execute( diff --git a/gallery/image.py b/gallery/image.py index 7c4787c..a9ab4bd 100644 --- a/gallery/image.py +++ b/gallery/image.py @@ -1,7 +1,6 @@ from flask import Blueprint, flash, g, redirect, render_template, request, url_for, jsonify, current_app from werkzeug.exceptions import abort from werkzeug.utils import secure_filename -from gallery.auth import login_required from gallery.db import get_db import os import datetime @@ -63,21 +62,25 @@ def image(id): abort(404) # Get exif data from image - file = Image.open(os.path.join(current_app.config['UPLOAD_FOLDER'], 'original', image['file_name'])) - raw_exif = file.getexif() - human_exif = {} - - for tag in raw_exif: - name = TAGS.get(tag, tag) - value = raw_exif.get(tag) + try: + file = Image.open(os.path.join(current_app.config['UPLOAD_FOLDER'], 'original', image['file_name'])) + raw_exif = file.getexif() + human_exif = {} - if isinstance(value, bytes): - value = value.decode() + for tag in raw_exif: + name = TAGS.get(tag, tag) + value = raw_exif.get(tag) + + if isinstance(value, bytes): + value = value.decode() + + human_exif[name] = value - human_exif[name] = value - - if len(human_exif) == 0: + if len(human_exif) == 0: + human_exif = False + except: + # Cringe, no file present human_exif = False # All in le head - return render_template('image.html', image=image, exif=human_exif) \ No newline at end of file + return render_template('image.html', image=image, exif=human_exif, file=file) \ No newline at end of file diff --git a/gallery/static/images/error.png b/gallery/static/images/error.png new file mode 100644 index 0000000..8f7cc08 Binary files /dev/null and b/gallery/static/images/error.png differ diff --git a/gallery/templates/image.html b/gallery/templates/image.html index 6733180..4cd0ba8 100644 --- a/gallery/templates/image.html +++ b/gallery/templates/image.html @@ -5,30 +5,44 @@ {% endblock %} {% block content %} +
{{ image['description'] }}
+{{ image['file_name'] }}
-{{ image['id'] }}
-{{ image['author_id'] }}
-{{ image['created_at'] }}
-{{ image['description'] }}
+Filename: {{ image['file_name'] }}
+Image ID: {{ image['id'] }}
+Author: {{ image['author_id'] }}
+Upload date: {{ image['created_at'] }}
+Dimensions: {{ file['width'] }}x{{ file['height'] }}