From 07c2cc11c35965d7991bbc8a451247b91a839fe2 Mon Sep 17 00:00:00 2001 From: aneuhmanh Date: Mon, 24 Feb 2025 13:07:32 +0200 Subject: [PATCH] changes --- app.py | 78 ++++++++++++++++++++++++++++--------- templates/comic_edit.html | 10 ++--- templates/upload_video.html | 11 +++--- 3 files changed, 69 insertions(+), 30 deletions(-) diff --git a/app.py b/app.py index a2e8c50..c102b65 100644 --- a/app.py +++ b/app.py @@ -54,7 +54,7 @@ app.config.update( ALLOWED_IMAGE_EXTENSIONS={'png', 'jpg', 'jpeg', 'gif', 'webp'}, ALLOWED_VIDEO_EXTENSIONS={'mp4', 'avi', 'mov'}, MAX_IMAGE_SIZE=15 * 1024 * 1024, - MAX_VIDEO_SIZE=1 * 1024 * 1024 * 1024 + MAX_VIDEO_SIZE=10 * 1024 * 1024 * 1024 ) def allowed_file(filename, allowed_extensions): @@ -157,12 +157,24 @@ class Comic(db.Model): comic_folder = db.Column(db.String(100), nullable=False) comic_thumbnail_file = db.Column(db.String(100), nullable=False) username = db.Column(db.String(20), db.ForeignKey('user.username'), nullable=False) - name = db.Column(db.String(100), nullable=False) + name = db.Column(db.String(100), nullable=False, unique=True) publication_date = db.Column(db.DateTime, default=datetime.utcnow) tags = db.Column(db.String(100), nullable=True) cookie_votes = db.Column(db.Integer, default=0) comments = db.relationship('Comments', back_populates='comic', overlaps="comic_link") + pages = db.relationship('ComicPage', back_populates='comic', cascade="all, delete-orphan") + + +class ComicPage(db.Model): + __tablename__ = 'comic_pages' + + id = db.Column(db.Integer, primary_key=True) + comic_id = db.Column(db.Integer, db.ForeignKey('comics.id', ondelete='CASCADE'), nullable=False) + page_number = db.Column(db.Integer, nullable=False) + file_path = db.Column(db.String(200), nullable=False) + + comic = db.relationship('Comic', back_populates='pages') class ComicVotes(db.Model): __tablename__ = 'comic_votes' @@ -644,7 +656,7 @@ async def upload_video(): return redirect(url_for('upload_video')) video_filename = await generate_unique_filename(video_file.filename, app.config['UPLOAD_FOLDER']['videos']) - thumbnail_filename = await generate_unique_filename(video_thumbnail.filename, app.config['UPLOAD_FOLDER']['thumbnails']) + thumbnail_filename = f"{uuid.uuid4().hex}.webp" video_path = os.path.join(app.config['UPLOAD_FOLDER']['videos'], video_filename) thumbnail_path = os.path.join(app.config['UPLOAD_FOLDER']['thumbnails'], thumbnail_filename) @@ -652,8 +664,9 @@ async def upload_video(): async with aiofiles.open(video_path, 'wb') as f: await f.write(video_file.read()) + webp_thumbnail = await convert_to_webp(video_thumbnail) async with aiofiles.open(thumbnail_path, 'wb') as f: - await f.write(video_thumbnail.read()) + await f.write(webp_thumbnail.read()) video = Video( video_file=video_filename, @@ -697,21 +710,25 @@ async def comic_upload(): tags=tags ) db.session.add(new_comic) - try: - db.session.commit() - except IntegrityError: - db.session.rollback() - return render_template('comic_upload.html') + db.session.flush() async def save_pages(): - for p in request.files.getlist('pages[]'): + pages = request.files.getlist('pages[]') + for i, p in enumerate(sorted(pages, key=lambda x: x.filename), start=1): if p: - async with aiofiles.open(os.path.join(cf, secure_filename(p.filename)), 'wb') as f: + filename = secure_filename(p.filename) + file_path = os.path.join(cf, filename) + async with aiofiles.open(file_path, 'wb') as f: await f.write(p.read()) + + new_page = ComicPage(comic_id=new_comic.id, page_number=i, file_path=file_path) + db.session.add(new_page) + db.session.commit() await save_pages() - return redirect(url_for('comics')) + + return render_template('comic_upload.html') @app.route('/user_pubs//') def user_pubs(pub_type, username): @@ -1022,20 +1039,43 @@ def comic_edit(comic_id): if not os.path.exists(cfp): os.makedirs(cfp) - comic_pages = sorted(os.listdir(cfp)) form = EmptyForm() if request.method == 'POST' and form.validate_on_submit(): action = request.form.get('action') - if action == 'delete' and (page_to_delete := request.form.get('page')): - os.remove(os.path.join(cfp, page_to_delete)) - elif action in ['update', 'add'] and 'new_page' in request.files: + + if action == 'delete' and (page_id := request.form.get('page')): + page = ComicPage.query.get(page_id) + if page: + os.remove(page.file_path) + db.session.delete(page) + db.session.commit() + + elif action == 'update' and (page_id := request.form.get('page')) and 'new_page' in request.files: new_page = request.files['new_page'] - new_page.save(os.path.join(cfp, secure_filename(new_page.filename))) + filename = secure_filename(new_page.filename) + file_path = os.path.join(cfp, filename) + new_page.save(file_path) + + page = ComicPage.query.get(page_id) + if page: + os.remove(page.file_path) + page.file_path = file_path + db.session.commit() + + elif action == 'add' and 'new_page' in request.files: + new_page = request.files['new_page'] + filename = secure_filename(new_page.filename) + file_path = os.path.join(cfp, filename) + new_page.save(file_path) + + page_number = (db.session.query(db.func.max(ComicPage.page_number)).filter_by(comic_id=comic.id).scalar() or 0) + 1 + db.session.add(ComicPage(comic_id=comic.id, page_number=page_number, file_path=file_path)) + db.session.commit() return redirect(url_for('comic_edit', comic_id=comic.id)) - return render_template('comic_edit.html', comic=comic, comic_pages=comic_pages, form=form) + return render_template('comic_edit.html', comic=comic, comic_pages=comic.pages, form=form) def update_related_tables(old_username, new_username): @@ -1570,4 +1610,4 @@ def buy_item(item_id): if __name__ == '__main__': with app.app_context(): db.create_all() - app.run(debug=True) \ No newline at end of file + app.run(debug=False) \ No newline at end of file diff --git a/templates/comic_edit.html b/templates/comic_edit.html index 1b55426..3575338 100644 --- a/templates/comic_edit.html +++ b/templates/comic_edit.html @@ -6,8 +6,7 @@ - - + 🫐comic edit - artberry🫐 @@ -26,12 +25,12 @@
    {% for page in comic_pages %}
  • - Comic Page {{ loop.index }} + Comic Page {{ loop.index }}

    Page {{ loop.index }}

    {{ form.hidden_tag() }} - +
    @@ -40,6 +39,7 @@ {{ form.hidden_tag() }} + @@ -49,4 +49,4 @@ Return to Comics List - + \ No newline at end of file diff --git a/templates/upload_video.html b/templates/upload_video.html index fbb888e..f495c3b 100644 --- a/templates/upload_video.html +++ b/templates/upload_video.html @@ -6,8 +6,7 @@ Upload Video - - + @@ -39,9 +38,9 @@ {{ form.tags(placeholder="tag1, tag2", class="input-field") }}
    - - {{ form.description(placeholder="Write a description...", class="input-field") }} -
    + + {{ form.description(placeholder="Write a description...", class="input-field") }} +
    {{ form.recaptcha() }}
    @@ -56,4 +55,4 @@ - + \ No newline at end of file