images webp converting
This commit is contained in:
parent
07c2cc11c3
commit
5b8185173f
34
app.py
34
app.py
@ -655,7 +655,7 @@ async def upload_video():
|
|||||||
if not allowed_file(video_thumbnail.filename, app.config['ALLOWED_IMAGE_EXTENSIONS']):
|
if not allowed_file(video_thumbnail.filename, app.config['ALLOWED_IMAGE_EXTENSIONS']):
|
||||||
return redirect(url_for('upload_video'))
|
return redirect(url_for('upload_video'))
|
||||||
|
|
||||||
video_filename = await generate_unique_filename(video_file.filename, app.config['UPLOAD_FOLDER']['videos'])
|
video_filename = await generate_unique_filename(app.config['UPLOAD_FOLDER']['videos'], 'mp4')
|
||||||
thumbnail_filename = f"{uuid.uuid4().hex}.webp"
|
thumbnail_filename = f"{uuid.uuid4().hex}.webp"
|
||||||
|
|
||||||
video_path = os.path.join(app.config['UPLOAD_FOLDER']['videos'], video_filename)
|
video_path = os.path.join(app.config['UPLOAD_FOLDER']['videos'], video_filename)
|
||||||
@ -683,6 +683,13 @@ async def upload_video():
|
|||||||
|
|
||||||
return render_template('upload_video.html', form=form)
|
return render_template('upload_video.html', form=form)
|
||||||
|
|
||||||
|
async def generate_unique_filename(upload_folder, extension):
|
||||||
|
while True:
|
||||||
|
unique_filename = f"{uuid.uuid4().hex}.{extension}"
|
||||||
|
file_path = os.path.join(upload_folder, unique_filename)
|
||||||
|
if not await aiofiles.os.path.exists(file_path):
|
||||||
|
return unique_filename
|
||||||
|
|
||||||
@app.route('/comic_upload', methods=['GET', 'POST'])
|
@app.route('/comic_upload', methods=['GET', 'POST'])
|
||||||
@login_required
|
@login_required
|
||||||
async def comic_upload():
|
async def comic_upload():
|
||||||
@ -695,9 +702,11 @@ async def comic_upload():
|
|||||||
return render_template('comic_upload.html')
|
return render_template('comic_upload.html')
|
||||||
|
|
||||||
if ct:
|
if ct:
|
||||||
tf = secure_filename(ct.filename)
|
tf = f"{uuid.uuid4().hex}.webp"
|
||||||
tp = os.path.join(app.config['UPLOAD_FOLDER']['comicthumbs'], tf)
|
tp = os.path.join(app.config['UPLOAD_FOLDER']['comicthumbs'], tf)
|
||||||
ct.save(tp)
|
webp_thumbnail = await convert_to_webp(ct)
|
||||||
|
async with aiofiles.open(tp, 'wb') as f:
|
||||||
|
await f.write(webp_thumbnail.read())
|
||||||
|
|
||||||
cf = os.path.join(app.config['UPLOAD_FOLDER']['comics'], n)
|
cf = os.path.join(app.config['UPLOAD_FOLDER']['comics'], n)
|
||||||
os.makedirs(cf, exist_ok=True)
|
os.makedirs(cf, exist_ok=True)
|
||||||
@ -716,10 +725,11 @@ async def comic_upload():
|
|||||||
pages = request.files.getlist('pages[]')
|
pages = request.files.getlist('pages[]')
|
||||||
for i, p in enumerate(sorted(pages, key=lambda x: x.filename), start=1):
|
for i, p in enumerate(sorted(pages, key=lambda x: x.filename), start=1):
|
||||||
if p:
|
if p:
|
||||||
filename = secure_filename(p.filename)
|
filename = f"{uuid.uuid4().hex}.webp"
|
||||||
file_path = os.path.join(cf, filename)
|
file_path = os.path.join(cf, filename)
|
||||||
|
webp_page = await convert_to_webp(p)
|
||||||
async with aiofiles.open(file_path, 'wb') as f:
|
async with aiofiles.open(file_path, 'wb') as f:
|
||||||
await f.write(p.read())
|
await f.write(webp_page.read())
|
||||||
|
|
||||||
new_page = ComicPage(comic_id=new_comic.id, page_number=i, file_path=file_path)
|
new_page = ComicPage(comic_id=new_comic.id, page_number=i, file_path=file_path)
|
||||||
db.session.add(new_page)
|
db.session.add(new_page)
|
||||||
@ -1100,7 +1110,7 @@ def update_related_tables(old_username, new_username):
|
|||||||
|
|
||||||
@app.route('/upload_post', methods=['GET', 'POST'])
|
@app.route('/upload_post', methods=['GET', 'POST'])
|
||||||
@login_required
|
@login_required
|
||||||
def upload_post():
|
async def upload_post():
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
post_text = request.form.get('post_text')
|
post_text = request.form.get('post_text')
|
||||||
post_media = request.files.get('post_media')
|
post_media = request.files.get('post_media')
|
||||||
@ -1113,11 +1123,13 @@ def upload_post():
|
|||||||
db.session.add(new_post)
|
db.session.add(new_post)
|
||||||
|
|
||||||
if post_media and allowed_file(post_media.filename, app.config['ALLOWED_IMAGE_EXTENSIONS']):
|
if post_media and allowed_file(post_media.filename, app.config['ALLOWED_IMAGE_EXTENSIONS']):
|
||||||
if check_file_size(post_media, app.config['MAX_IMAGE_SIZE']):
|
if await check_file_size(post_media, app.config['MAX_IMAGE_SIZE']):
|
||||||
media_filename = secure_filename(post_media.filename)
|
unique_filename = f"{uuid.uuid4().hex}.webp"
|
||||||
media_path = os.path.join(app.config['UPLOAD_FOLDER']['posts'], media_filename)
|
media_path = os.path.join(app.config['UPLOAD_FOLDER']['posts'], unique_filename)
|
||||||
post_media.save(media_path)
|
webp_image = await convert_to_webp(post_media)
|
||||||
new_post.media_file = media_filename
|
async with aiofiles.open(media_path, 'wb') as f:
|
||||||
|
await f.write(webp_image.read())
|
||||||
|
new_post.media_file = unique_filename
|
||||||
else:
|
else:
|
||||||
return redirect(url_for('upload_post'))
|
return redirect(url_for('upload_post'))
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user