From dfa6c89311929a489562929e384c4a7f664cf475 Mon Sep 17 00:00:00 2001 From: aneuhmanh Date: Wed, 19 Feb 2025 23:44:38 +0200 Subject: [PATCH] comic upload optimization --- app.py | 28 +++++++++++++++------------- requirements.txt | Bin 912 -> 956 bytes 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/app.py b/app.py index 1984c71..637e7fd 100644 --- a/app.py +++ b/app.py @@ -677,14 +677,13 @@ async def upload_video(): @app.route('/comic_upload', methods=['GET', 'POST']) @login_required -def comic_upload(): +async def comic_upload(): if request.method == 'POST': - ct = request.files['thumbnail'] n = request.form['title'] tags = request.form.get('tags', '') - if Comic.query.filter_by(name=n).first(): + if db.session.execute(db.select(Comic).filter_by(name=n)).scalar(): return render_template('comic_upload.html') if ct: @@ -703,16 +702,22 @@ def comic_upload(): tags=tags ) db.session.add(new_comic) - db.session.commit() + try: + db.session.commit() + except IntegrityError: + db.session.rollback() + return render_template('comic_upload.html') - for p in request.files.getlist('pages[]'): - if p: - p.save(os.path.join(cf, secure_filename(p.filename))) + async def save_pages(): + for p in request.files.getlist('pages[]'): + if p: + async with aiofiles.open(os.path.join(cf, secure_filename(p.filename)), 'wb') as f: + await f.write(p.read()) + + await save_pages() return redirect(url_for('comics')) - return render_template('comic_upload.html') - @app.route('/user_pubs//') def user_pubs(pub_type, username): p = request.args.get('page', 1, type=int) @@ -886,7 +891,7 @@ def delete(content_type, content_id): @app.route('/delete_comment/', methods=['POST']) @login_required def delete_comment(comment_id): - comment = Comments.query.get_or_404(comment_id) + comment = db.session.get(Comments, comment_id) or abort(404) if comment.image_id: content_type = 'art' @@ -900,7 +905,6 @@ def delete_comment(comment_id): if comment.username == current_user.username: try: - if hasattr(comment, 'post_id') and comment.post_id: post_id = comment.post_id db.session.delete(comment) @@ -910,10 +914,8 @@ def delete_comment(comment_id): db.session.delete(comment) db.session.commit() return redirect(url_for('view', content_type=content_type, id=content_id)) - except IntegrityError: db.session.rollback() - return redirect(url_for('view', content_type=content_type, id=content_id)) return redirect(request.referrer or url_for('index')) diff --git a/requirements.txt b/requirements.txt index a91a4ecf765812a173358a9c6f5a7a758b5df593..1085bb03ef9ed7db54d77806adae96839dfa3bce 100644 GIT binary patch delta 51 zcmbQhzK4Co8YcAuhDwGKh75*$hCBvch7=%O!jKANmoeA^p&^4Fg8>j5PF~LR5&%W{ B3(Noj delta 11 TcmdnPK7oD18m7rNn4SOt97_cX