mirror of
https://github.com/Derpy-Leggies/OnlyLegs.git
synced 2024-12-28 18:36:21 +00:00
Clean up config folder
Create pfps folder and organise everything better :3
This commit is contained in:
parent
32b96a39ac
commit
2d0fd6ca8a
|
@ -4,7 +4,6 @@ This is the main app file, it loads all the other files and sets up the app
|
||||||
"""
|
"""
|
||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
import platformdirs
|
|
||||||
|
|
||||||
from flask_assets import Bundle
|
from flask_assets import Bundle
|
||||||
from flask_migrate import init as migrate_init
|
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 werkzeug.security import generate_password_hash
|
||||||
|
|
||||||
from onlylegs.extensions import db, migrate, login_manager, assets, compress, cache
|
from onlylegs.extensions import db, migrate, login_manager, assets, compress, cache
|
||||||
from onlylegs.views import index, image, group, settings, profile
|
from onlylegs.config import INSTANCE_DIR, MIGRATIONS_DIR
|
||||||
from onlylegs import api
|
|
||||||
from onlylegs import auth
|
|
||||||
from onlylegs.models import User
|
from onlylegs.models import User
|
||||||
|
from onlylegs.views import index, image, group, settings, profile
|
||||||
|
from onlylegs import api, auth
|
||||||
INSTACE_DIR = os.path.join(platformdirs.user_config_dir("onlylegs"), "instance")
|
|
||||||
MIGRATIONS_DIR = os.path.join(INSTACE_DIR, "migrations")
|
|
||||||
|
|
||||||
|
|
||||||
def create_app(): # pylint: disable=R0914
|
def create_app(): # pylint: disable=R0914
|
||||||
"""
|
"""
|
||||||
Create and configure the main app
|
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")
|
app.config.from_pyfile("config.py")
|
||||||
|
|
||||||
# DATABASE
|
# DATABASE
|
||||||
|
@ -36,7 +31,7 @@ def create_app(): # pylint: disable=R0914
|
||||||
migrate.init_app(app, db, directory=MIGRATIONS_DIR)
|
migrate.init_app(app, db, directory=MIGRATIONS_DIR)
|
||||||
|
|
||||||
# If database file doesn't exist, create it
|
# 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")
|
print("Creating database")
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
db.create_all()
|
db.create_all()
|
||||||
|
|
|
@ -5,7 +5,6 @@ from uuid import uuid4
|
||||||
import os
|
import os
|
||||||
import pathlib
|
import pathlib
|
||||||
import logging
|
import logging
|
||||||
import platformdirs
|
|
||||||
|
|
||||||
from flask import Blueprint, send_from_directory, abort, flash, request, current_app
|
from flask import Blueprint, send_from_directory, abort, flash, request, current_app
|
||||||
from werkzeug.utils import secure_filename
|
from werkzeug.utils import secure_filename
|
||||||
|
@ -105,11 +104,9 @@ def delete_image(image_id):
|
||||||
"""
|
"""
|
||||||
Deletes an image from the server and database
|
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)
|
# 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:
|
if post.author_id != current_user.id:
|
||||||
abort(403)
|
abort(403)
|
||||||
|
|
||||||
|
@ -122,9 +119,8 @@ def delete_image(image_id):
|
||||||
)
|
)
|
||||||
|
|
||||||
# Delete cached files
|
# Delete cached files
|
||||||
cache_path = os.path.join(platformdirs.user_config_dir("onlylegs"), "cache")
|
|
||||||
cache_name = post.filename.rsplit(".")[0]
|
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)
|
os.remove(cache_file)
|
||||||
|
|
||||||
GroupJunction.query.filter_by(post_id=image_id).delete()
|
GroupJunction.query.filter_by(post_id=image_id).delete()
|
||||||
|
@ -190,11 +186,9 @@ def delete_group():
|
||||||
Deletes a group
|
Deletes a group
|
||||||
"""
|
"""
|
||||||
group_id = request.form["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:
|
if group.author_id != current_user.id:
|
||||||
abort(404)
|
|
||||||
elif group.author_id != current_user.id:
|
|
||||||
abort(403)
|
abort(403)
|
||||||
|
|
||||||
GroupJunction.query.filter_by(group_id=group_id).delete()
|
GroupJunction.query.filter_by(group_id=group_id).delete()
|
||||||
|
|
|
@ -12,11 +12,11 @@ user_dir = platformdirs.user_config_dir("onlylegs")
|
||||||
instance_dir = os.path.join(user_dir, "instance")
|
instance_dir = os.path.join(user_dir, "instance")
|
||||||
|
|
||||||
# Load environment variables
|
# Load environment variables
|
||||||
print("Loading environment variables...")
|
# print("Loading environment variables...")
|
||||||
load_dotenv(os.path.join(user_dir, ".env"))
|
load_dotenv(os.path.join(user_dir, ".env"))
|
||||||
|
|
||||||
# Load config from user dir
|
# 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:
|
with open(os.path.join(user_dir, "conf.yml"), encoding="utf-8", mode="r") as file:
|
||||||
conf = safe_load(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
|
# Flask config
|
||||||
SECRET_KEY = os.environ.get("FLASK_SECRET")
|
SECRET_KEY = os.environ.get("FLASK_SECRET")
|
||||||
SQLALCHEMY_DATABASE_URI = "sqlite:///gallery.sqlite3"
|
SQLALCHEMY_DATABASE_URI = "sqlite:///gallery.sqlite3"
|
||||||
|
|
||||||
# Upload config
|
|
||||||
MAX_CONTENT_LENGTH = 1024 * 1024 * conf["upload"]["max-size"]
|
MAX_CONTENT_LENGTH = 1024 * 1024 * conf["upload"]["max-size"]
|
||||||
UPLOAD_FOLDER = os.path.join(user_dir, "uploads")
|
|
||||||
ALLOWED_EXTENSIONS = conf["upload"]["allowed-extensions"]
|
ALLOWED_EXTENSIONS = conf["upload"]["allowed-extensions"]
|
||||||
|
|
||||||
# Pass YAML config to app
|
# Pass YAML config to app
|
||||||
ADMIN_CONF = conf["admin"]
|
ADMIN_CONF = conf["admin"]
|
||||||
UPLOAD_CONF = conf["upload"]
|
UPLOAD_CONF = conf["upload"]
|
||||||
WEBSITE_CONF = conf["website"]
|
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")
|
||||||
|
|
|
@ -3,15 +3,11 @@ Tools for generating images and thumbnails
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import platformdirs
|
|
||||||
from PIL import Image, ImageOps
|
from PIL import Image, ImageOps
|
||||||
|
from onlylegs.config import UPLOAD_FOLDER, CACHE_FOLDER
|
||||||
from werkzeug.utils import secure_filename
|
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):
|
def generate_thumbnail(file_name, resolution, ext=None):
|
||||||
"""
|
"""
|
||||||
Image thumbnail generator
|
Image thumbnail generator
|
||||||
|
@ -21,8 +17,8 @@ def generate_thumbnail(file_name, resolution, ext=None):
|
||||||
ext is the file extension of the image
|
ext is the file extension of the image
|
||||||
"""
|
"""
|
||||||
# Make image cache directory if it doesn't exist
|
# Make image cache directory if it doesn't exist
|
||||||
if not os.path.exists(CACHE_PATH):
|
if not os.path.exists(CACHE_FOLDER):
|
||||||
os.makedirs(CACHE_PATH)
|
os.makedirs(CACHE_FOLDER)
|
||||||
|
|
||||||
# no sussy business
|
# no sussy business
|
||||||
file_name, file_ext = secure_filename(file_name).rsplit(".")
|
file_name, file_ext = secure_filename(file_name).rsplit(".")
|
||||||
|
@ -44,15 +40,15 @@ def generate_thumbnail(file_name, resolution, ext=None):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# If image has been already generated, return it from the cache
|
# 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}")):
|
if os.path.exists(os.path.join(CACHE_FOLDER, f"{file_name}_{res_x}x{res_y}.{ext}")):
|
||||||
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}")
|
||||||
|
|
||||||
# Check if image exists in the uploads directory
|
# 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
|
return None
|
||||||
|
|
||||||
# Open image and rotate it based on EXIF data and get ICC profile so colors are correct
|
# 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")
|
image_icc = image.info.get("icc_profile")
|
||||||
img_x, img_y = image.size
|
img_x, img_y = image.size
|
||||||
|
|
||||||
|
@ -63,7 +59,7 @@ def generate_thumbnail(file_name, resolution, ext=None):
|
||||||
# Save image to cache directory
|
# Save image to cache directory
|
||||||
try:
|
try:
|
||||||
image.save(
|
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,
|
icc_profile=image_icc,
|
||||||
)
|
)
|
||||||
except OSError:
|
except OSError:
|
||||||
|
@ -71,11 +67,11 @@ def generate_thumbnail(file_name, resolution, ext=None):
|
||||||
# so we convert to RGB and try again
|
# so we convert to RGB and try again
|
||||||
image = image.convert("RGB")
|
image = image.convert("RGB")
|
||||||
image.save(
|
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,
|
icc_profile=image_icc,
|
||||||
)
|
)
|
||||||
|
|
||||||
# No need to keep the image in memory, learned the hard way
|
# No need to keep the image in memory, learned the hard way
|
||||||
image.close()
|
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}")
|
||||||
|
|
|
@ -45,7 +45,10 @@ class Configuration:
|
||||||
"""
|
"""
|
||||||
os.makedirs(USER_DIR)
|
os.makedirs(USER_DIR)
|
||||||
os.makedirs(os.path.join(USER_DIR, "instance"))
|
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)
|
print("Created user directory at:", USER_DIR)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue