diff --git a/files/__main__.py b/files/__main__.py index d8344edb5..5a87caf6d 100644 --- a/files/__main__.py +++ b/files/__main__.py @@ -12,8 +12,6 @@ from flask import Flask from flask_caching import Cache from flask_compress import Compress from flask_limiter import Limiter -from sqlalchemy import * -from sqlalchemy.orm import scoped_session, sessionmaker from files.helpers.config.const import * from files.helpers.const_stateful import const_initialize @@ -48,7 +46,6 @@ app.config["PERMANENT_SESSION_LIFETIME"] = 60 * 60 * 24 * 999 app.config['SESSION_REFRESH_EACH_REQUEST'] = False app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False -app.config['SQLALCHEMY_DATABASE_URL'] = environ.get("DATABASE_URL").strip() app.config["CACHE_KEY_PREFIX"] = f"{SITE}_flask_cache_" app.config["CACHE_TYPE"] = "RedisCache" @@ -74,11 +71,7 @@ limiter = Limiter( storage_uri=app.config["CACHE_REDIS_URL"], ) -engine = create_engine(app.config['SQLALCHEMY_DATABASE_URL']) - -db_session = scoped_session(sessionmaker(bind=engine)) - -const_initialize(db_session()) +const_initialize() reload_settings() start_watching_settings() diff --git a/files/helpers/config/const.py b/files/helpers/config/const.py index 901499e0f..e93bc6bdf 100644 --- a/files/helpers/config/const.py +++ b/files/helpers/config/const.py @@ -1263,3 +1263,9 @@ GIRL_NAMES = { 'Y': ['Yesenia', 'Yasmine', 'Yasmin', 'Yvette', 'Yolanda', 'Yadira', 'Yvonne', 'Yamilet', 'Yazmin', 'Yasmeen', 'Yessenia'], 'Z': ['Zoe', 'Zoey', 'Zaria', 'Zoie'] } + +from sqlalchemy import * +from sqlalchemy.orm import scoped_session, sessionmaker + +engine = create_engine(environ.get("DATABASE_URL").strip()) +db_session = scoped_session(sessionmaker(bind=engine)) diff --git a/files/helpers/const_stateful.py b/files/helpers/const_stateful.py index b149e0d30..8fd9f2579 100644 --- a/files/helpers/const_stateful.py +++ b/files/helpers/const_stateful.py @@ -11,9 +11,11 @@ SNAPPY_MARSEYS = [] SNAPPY_QUOTES = [] STEALTH_HOLES = [] -def const_initialize(db): +def const_initialize(): global marseys_const, marseys_const2, marsey_mappings, SNAPPY_KONGS, SNAPPY_MARSEYS, SNAPPY_QUOTES, STEALTH_HOLES + db = db_session() + marseys_const = [x[0] for x in db.query(Emoji.name).filter(Emoji.kind=="Marsey", Emoji.submitter_id==None, Emoji.name!='chudsey').all()] marseys_const2 = marseys_const + ['chudsey','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','exclamationpoint','period','questionmark'] marseys = db.query(Emoji).filter(Emoji.kind=="Marsey", Emoji.submitter_id==None).all() diff --git a/files/helpers/media.py b/files/helpers/media.py index a74073716..7d9490a09 100644 --- a/files/helpers/media.py +++ b/files/helpers/media.py @@ -12,7 +12,6 @@ from werkzeug.utils import secure_filename from PIL import Image from PIL import UnidentifiedImageError from PIL.ImageSequence import Iterator -from sqlalchemy.orm.session import Session from files.classes.media import * from files.helpers.cloudflare import purge_files_in_cache @@ -107,7 +106,7 @@ def process_audio(file, v): return new -def convert_to_mp4(old, new, vid, db): +def convert_to_mp4(old, new, vid): tmp = new.replace('.mp4', '-t.mp4') try: subprocess.run(["ffmpeg", "-y", "-loglevel", "warning", "-nostats", "-threads:v", "1", "-i", old, "-map_metadata", "-1", tmp], check=True, stderr=subprocess.STDOUT, timeout=SUBPROCESS_TIMEOUT_DURATION) @@ -126,8 +125,15 @@ def convert_to_mp4(old, new, vid, db): user_id=vid, size=os.stat(new).st_size ) + + db = db_session() db.add(media) - db.commit() + + try: + db.commit() + except: + db.rollback() + db.close() purge_files_in_cache(f"{SITE_FULL}{new}") @@ -152,8 +158,7 @@ def process_video(file, v): if extension not in {'mp4','avi','mkv'}: new = new.replace(f'.{extension}', '.mp4') copyfile(old, new) - db = Session(bind=g.db.get_bind()) - gevent.spawn(convert_to_mp4, old, new, v.id, db) + gevent.spawn(convert_to_mp4, old, new, v.id) else: try: subprocess.run(["ffmpeg", "-y", "-loglevel", "warning", "-nostats", "-i", old, "-map_metadata", "-1", "-c:v", "copy", "-c:a", "copy", new], check=True, timeout=SUBPROCESS_TIMEOUT_DURATION)