diff --git a/gallery/__init__.py b/gallery/__init__.py index 259f7b8..f3bd732 100644 --- a/gallery/__init__.py +++ b/gallery/__init__.py @@ -60,7 +60,7 @@ def create_app(test_config=None): app.config.from_mapping(test_config) # Load theme - theme_manager.CompileTheme('default', app.root_path) + theme_manager.compile_theme('default', app.root_path) # Bundle JS files js_scripts = Bundle('js/*.js', output='gen/packed.js') @@ -82,7 +82,7 @@ def create_app(test_config=None): app.register_blueprint(auth.blueprint) # Load the different routes - from .routes import api, groups, routing, settings + from gallery.routes import api, groups, routing, settings app.register_blueprint(api.blueprint) app.register_blueprint(groups.blueprint) app.register_blueprint(routing.blueprint) diff --git a/gallery/auth.py b/gallery/auth.py index 0c7cfa4..0518a60 100644 --- a/gallery/auth.py +++ b/gallery/auth.py @@ -65,15 +65,16 @@ def register(): """ Register a new user """ - username = request.form['username'] - email = request.form['email'] - password = request.form['password'] - password_repeat = request.form['password-repeat'] + # Thanks Fennec for reminding me to strip out the whitespace lol + username = request.form['username'].strip() + email = request.form['email'].strip() + password = request.form['password'].strip() + password_repeat = request.form['password-repeat'].strip() error = [] email_regex = re.compile(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b') - username_regex = re.compile(r'\b[A-Za-z0-9._%+-]+\b') + username_regex = re.compile(r'\b[A-Za-z0-9._-]+\b') if not username or not username_regex.match(username): error.append('Username is invalid!') @@ -116,8 +117,8 @@ def login(): """ Log in a registered user by adding the user id to the session """ - username = request.form['username'] - password = request.form['password'] + username = request.form['username'].strip() + password = request.form['password'].strip() user = db_session.query(db.Users).filter_by(username=username).first() error = [] diff --git a/gallery/routes/__init__.py b/gallery/routes/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/gallery/routes/api.py b/gallery/routes/api.py index dbe9dca..517838f 100644 --- a/gallery/routes/api.py +++ b/gallery/routes/api.py @@ -33,6 +33,7 @@ def file(file_name): r for resolution, 400x400 or thumb for thumbnail """ res = request.args.get('r', default=None, type=str) # Type of file (thumb, etc) + ext = request.args.get('e', default=None, type=str) # File extension file_name = secure_filename(file_name) # Sanitize file name # if no args are passed, return the raw file @@ -42,7 +43,7 @@ def file(file_name): return send_from_directory(current_app.config['UPLOAD_FOLDER'], file_name) - thumb = generate_thumbnail(file_name, res) + thumb = generate_thumbnail(file_name, res, ext) if not thumb: abort(404) diff --git a/gallery/routes/routing.py b/gallery/routes/routing.py index cfcc915..9d38a72 100644 --- a/gallery/routes/routing.py +++ b/gallery/routes/routing.py @@ -23,7 +23,7 @@ def index(): db.Posts.image_colours, db.Posts.created_at, db.Posts.id).order_by(db.Posts.id.desc()).all() - + if request.args.get('coffee') == 'please': abort(418) diff --git a/gallery/utils/generate_image.py b/gallery/utils/generate_image.py index 2b5bbd7..bae6861 100644 --- a/gallery/utils/generate_image.py +++ b/gallery/utils/generate_image.py @@ -34,10 +34,12 @@ def generate_thumbnail(file_name, resolution, ext=None): ext = "jpeg" # Set resolution based on preset resolutions - if resolution in ['thumb', 'thumbnail']: - res_x, res_y = (400, 400) - elif resolution in ['prev', 'preview']: + if resolution in ['prev', 'preview']: res_x, res_y = (1920, 1080) + elif resolution in ['thumb', 'thumbnail']: + res_x, res_y = (400, 400) + elif resolution in ['icon', 'favicon']: + res_x, res_y = (10, 10) elif len(resolution.split('x')) == 2: res_x, res_y = resolution.split('x') else: diff --git a/gallery/utils/theme_manager.py b/gallery/utils/theme_manager.py index 5e32dcf..6915a3e 100644 --- a/gallery/utils/theme_manager.py +++ b/gallery/utils/theme_manager.py @@ -8,75 +8,53 @@ from datetime import datetime import sass -class CompileTheme: +def compile_theme(theme_name, app_path): """ Compiles the theme into the static folder """ - def __init__(self, theme_name, app_path): - """ - Initialize the theme manager - Compiles the theme into the static folder and loads the fonts - """ + print(f"Loading '{theme_name}' theme...") - print(f"Loading '{theme_name}' theme...") + # Set Paths + THEME_SRC = os.path.join(app_path, 'themes', theme_name) + THEME_DEST = os.path.join(app_path, 'static', 'theme') - theme_path = os.path.join(app_path, 'themes', theme_name) - theme_dest = os.path.join(app_path, 'static', 'theme') + # If the theme doesn't exist, exit + if not os.path.exists(THEME_SRC): + print("Theme does not exist!") + sys.exit(1) - if not os.path.exists(theme_path): - print("Theme does not exist!") - sys.exit(1) + # If the destination folder doesn't exist, create it + if not os.path.exists(THEME_DEST): + os.makedirs(THEME_DEST) - if not os.path.exists(theme_dest): - os.makedirs(theme_dest) - - self.load_sass(theme_path, theme_dest) - self.load_fonts(theme_path, theme_dest) - - print(f"{datetime.now().hour}:{datetime.now().minute}:{datetime.now().second} - Done!\n") - - @staticmethod - def load_sass(source_path, css_dest): - """ - Compile the sass (or scss) file into css and save it to the static folder - """ - if os.path.join(source_path, 'style.sass'): - sass_path = os.path.join(source_path, 'style.sass') - elif os.path.join(source_path, 'style.scss'): - sass_path = os.path.join(source_path, 'style.scss') - else: - print("No sass file found!") - sys.exit(1) - - with open(os.path.join(css_dest, 'style.css'), encoding='utf-8', mode='w+') as file: - try: - file.write(sass.compile(filename=sass_path,output_style='compressed')) - except sass.CompileError as err: - print("Failed to compile!\n", err) - sys.exit(1) - - print("Compiled successfully!") - - @staticmethod - def load_fonts(source_path, font_dest): - """ - Copy the fonts folder to the static folder - """ - # Append fonts to the destination path - source_path = os.path.join(source_path, 'fonts') - font_dest = os.path.join(font_dest, 'fonts') - - if os.path.exists(font_dest): - try: - shutil.rmtree(font_dest) - except Exception as err: - print("Failed to remove old fonts!\n", err) - sys.exit(1) + # Theme source file doesn't exist, exit + if not os.path.join(THEME_SRC, 'style.sass'): + print("No sass file found!") + sys.exit(1) + # Compile the theme + with open(os.path.join(THEME_DEST, 'style.css'), encoding='utf-8', mode='w+') as file: try: - shutil.copytree(source_path, font_dest) + file.write(sass.compile(filename=os.path.join(THEME_SRC, 'style.sass'),output_style='compressed')) + except sass.CompileError as err: + print("Failed to compile!\n", err) + sys.exit(1) + print("Compiled successfully!") + + # If the destination folder exists, remove it + if os.path.exists(os.path.join(THEME_DEST, 'fonts')): + try: + shutil.rmtree(os.path.join(THEME_DEST, 'fonts')) except Exception as err: - print("Failed to copy fonts!\n", err) + print("Failed to remove old fonts!\n", err) sys.exit(1) + # Copy the fonts + try: + shutil.copytree(os.path.join(THEME_SRC, 'fonts'), os.path.join(THEME_DEST, 'fonts')) print("Fonts copied successfully!") + except Exception as err: + print("Failed to copy fonts!\n", err) + sys.exit(1) + + print(f"{datetime.now().hour}:{datetime.now().minute}:{datetime.now().second} - Done!\n")