From 2d0fd6ca8a6b609e42cecc7a155c41f852ac15b6 Mon Sep 17 00:00:00 2001 From: Fluffy-Bean Date: Thu, 20 Apr 2023 19:20:58 +0000 Subject: [PATCH] Clean up config folder Create pfps folder and organise everything better :3 --- onlylegs/__init__.py | 15 +++++---------- onlylegs/api.py | 14 ++++---------- onlylegs/config.py | 16 +++++++++++----- onlylegs/utils/generate_image.py | 24 ++++++++++-------------- setup/configuration.py | 5 ++++- 5 files changed, 34 insertions(+), 40 deletions(-) diff --git a/onlylegs/__init__.py b/onlylegs/__init__.py index 8433882..203186a 100644 --- a/onlylegs/__init__.py +++ b/onlylegs/__init__.py @@ -4,7 +4,6 @@ This is the main app file, it loads all the other files and sets up the app """ import os import logging -import platformdirs from flask_assets import Bundle from flask_migrate import init as migrate_init @@ -14,21 +13,17 @@ from werkzeug.exceptions import HTTPException from werkzeug.security import generate_password_hash from onlylegs.extensions import db, migrate, login_manager, assets, compress, cache -from onlylegs.views import index, image, group, settings, profile -from onlylegs import api -from onlylegs import auth +from onlylegs.config import INSTANCE_DIR, MIGRATIONS_DIR from onlylegs.models import User - - -INSTACE_DIR = os.path.join(platformdirs.user_config_dir("onlylegs"), "instance") -MIGRATIONS_DIR = os.path.join(INSTACE_DIR, "migrations") +from onlylegs.views import index, image, group, settings, profile +from onlylegs import api, auth def create_app(): # pylint: disable=R0914 """ Create and configure the main app """ - app = Flask(__name__, instance_path=INSTACE_DIR) + app = Flask(__name__, instance_path=INSTANCE_DIR) app.config.from_pyfile("config.py") # DATABASE @@ -36,7 +31,7 @@ def create_app(): # pylint: disable=R0914 migrate.init_app(app, db, directory=MIGRATIONS_DIR) # If database file doesn't exist, create it - if not os.path.exists(os.path.join(INSTACE_DIR, "gallery.sqlite3")): + if not os.path.exists(os.path.join(INSTANCE_DIR, "gallery.sqlite3")): print("Creating database") with app.app_context(): db.create_all() diff --git a/onlylegs/api.py b/onlylegs/api.py index 3ddfc3b..0631cf3 100644 --- a/onlylegs/api.py +++ b/onlylegs/api.py @@ -5,7 +5,6 @@ from uuid import uuid4 import os import pathlib import logging -import platformdirs from flask import Blueprint, send_from_directory, abort, flash, request, current_app from werkzeug.utils import secure_filename @@ -105,11 +104,9 @@ def delete_image(image_id): """ Deletes an image from the server and database """ - post = Post.query.filter_by(id=image_id).first() + post = db.get_or_404(Post, image_id) # Check if image exists and if user is allowed to delete it (author) - if post is None: - abort(404) if post.author_id != current_user.id: abort(403) @@ -122,9 +119,8 @@ def delete_image(image_id): ) # Delete cached files - cache_path = os.path.join(platformdirs.user_config_dir("onlylegs"), "cache") cache_name = post.filename.rsplit(".")[0] - for cache_file in pathlib.Path(cache_path).glob(cache_name + "*"): + for cache_file in pathlib.Path(current_app.config["CACHE_FOLDER"]).glob(cache_name + "*"): os.remove(cache_file) GroupJunction.query.filter_by(post_id=image_id).delete() @@ -190,11 +186,9 @@ def delete_group(): Deletes a group """ group_id = request.form["group"] - group = Group.query.filter_by(id=group_id).first() + group = db.get_or_404(Group, group_id) - if group is None: - abort(404) - elif group.author_id != current_user.id: + if group.author_id != current_user.id: abort(403) GroupJunction.query.filter_by(group_id=group_id).delete() diff --git a/onlylegs/config.py b/onlylegs/config.py index 57f42da..aa9dde0 100644 --- a/onlylegs/config.py +++ b/onlylegs/config.py @@ -12,11 +12,11 @@ user_dir = platformdirs.user_config_dir("onlylegs") instance_dir = os.path.join(user_dir, "instance") # Load environment variables -print("Loading environment variables...") +# print("Loading environment variables...") load_dotenv(os.path.join(user_dir, ".env")) # Load config from user dir -print("Loading config...") +# print("Loading config...") with open(os.path.join(user_dir, "conf.yml"), encoding="utf-8", mode="r") as file: conf = safe_load(file) @@ -24,13 +24,19 @@ with open(os.path.join(user_dir, "conf.yml"), encoding="utf-8", mode="r") as fil # Flask config SECRET_KEY = os.environ.get("FLASK_SECRET") SQLALCHEMY_DATABASE_URI = "sqlite:///gallery.sqlite3" - -# Upload config MAX_CONTENT_LENGTH = 1024 * 1024 * conf["upload"]["max-size"] -UPLOAD_FOLDER = os.path.join(user_dir, "uploads") ALLOWED_EXTENSIONS = conf["upload"]["allowed-extensions"] # Pass YAML config to app ADMIN_CONF = conf["admin"] UPLOAD_CONF = conf["upload"] WEBSITE_CONF = conf["website"] + +# Directories +UPLOAD_FOLDER = os.path.join(user_dir, "media", "uploads") +CACHE_FOLDER = os.path.join(user_dir, "media", "cache") +PFP_FOLDER = os.path.join(user_dir, "media", "pfp") + +# Database +INSTANCE_DIR = instance_dir +MIGRATIONS_DIR = os.path.join(INSTANCE_DIR, "migrations") diff --git a/onlylegs/utils/generate_image.py b/onlylegs/utils/generate_image.py index 7a172cc..2c0f1af 100644 --- a/onlylegs/utils/generate_image.py +++ b/onlylegs/utils/generate_image.py @@ -3,15 +3,11 @@ Tools for generating images and thumbnails """ import os -import platformdirs from PIL import Image, ImageOps +from onlylegs.config import UPLOAD_FOLDER, CACHE_FOLDER from werkzeug.utils import secure_filename -CACHE_PATH = os.path.join(platformdirs.user_config_dir("onlylegs"), "cache") -UPLOAD_PATH = os.path.join(platformdirs.user_config_dir("onlylegs"), "uploads") - - def generate_thumbnail(file_name, resolution, ext=None): """ Image thumbnail generator @@ -21,8 +17,8 @@ def generate_thumbnail(file_name, resolution, ext=None): ext is the file extension of the image """ # Make image cache directory if it doesn't exist - if not os.path.exists(CACHE_PATH): - os.makedirs(CACHE_PATH) + if not os.path.exists(CACHE_FOLDER): + os.makedirs(CACHE_FOLDER) # no sussy business file_name, file_ext = secure_filename(file_name).rsplit(".") @@ -44,15 +40,15 @@ def generate_thumbnail(file_name, resolution, ext=None): return None # If image has been already generated, return it from the cache - if os.path.exists(os.path.join(CACHE_PATH, f"{file_name}_{res_x}x{res_y}.{ext}")): - return os.path.join(CACHE_PATH, f"{file_name}_{res_x}x{res_y}.{ext}") + if os.path.exists(os.path.join(CACHE_FOLDER, f"{file_name}_{res_x}x{res_y}.{ext}")): + return os.path.join(CACHE_FOLDER, f"{file_name}_{res_x}x{res_y}.{ext}") # Check if image exists in the uploads directory - if not os.path.exists(os.path.join(UPLOAD_PATH, f"{file_name}.{file_ext}")): + if not os.path.exists(os.path.join(UPLOAD_FOLDER, f"{file_name}.{file_ext}")): return None # Open image and rotate it based on EXIF data and get ICC profile so colors are correct - image = Image.open(os.path.join(UPLOAD_PATH, f"{file_name}.{file_ext}")) + image = Image.open(os.path.join(UPLOAD_FOLDER, f"{file_name}.{file_ext}")) image_icc = image.info.get("icc_profile") img_x, img_y = image.size @@ -63,7 +59,7 @@ def generate_thumbnail(file_name, resolution, ext=None): # Save image to cache directory try: image.save( - os.path.join(CACHE_PATH, f"{file_name}_{res_x}x{res_y}.{ext}"), + os.path.join(CACHE_FOLDER, f"{file_name}_{res_x}x{res_y}.{ext}"), icc_profile=image_icc, ) except OSError: @@ -71,11 +67,11 @@ def generate_thumbnail(file_name, resolution, ext=None): # so we convert to RGB and try again image = image.convert("RGB") image.save( - os.path.join(CACHE_PATH, f"{file_name}_{res_x}x{res_y}.{ext}"), + os.path.join(CACHE_FOLDER, f"{file_name}_{res_x}x{res_y}.{ext}"), icc_profile=image_icc, ) # No need to keep the image in memory, learned the hard way image.close() - return os.path.join(CACHE_PATH, f"{file_name}_{res_x}x{res_y}.{ext}") + return os.path.join(CACHE_FOLDER, f"{file_name}_{res_x}x{res_y}.{ext}") diff --git a/setup/configuration.py b/setup/configuration.py index d01ddf9..2727ce0 100644 --- a/setup/configuration.py +++ b/setup/configuration.py @@ -45,7 +45,10 @@ class Configuration: """ os.makedirs(USER_DIR) os.makedirs(os.path.join(USER_DIR, "instance")) - os.makedirs(os.path.join(USER_DIR, "uploads")) + os.makedirs(os.path.join(USER_DIR, "media")) + os.makedirs(os.path.join(USER_DIR, "media", "uploads")) + os.makedirs(os.path.join(USER_DIR, "media", "cache")) + os.makedirs(os.path.join(USER_DIR, "media", "pfp")) print("Created user directory at:", USER_DIR)