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']):
|
||||
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"
|
||||
|
||||
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)
|
||||
|
||||
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'])
|
||||
@login_required
|
||||
async def comic_upload():
|
||||
@ -695,9 +702,11 @@ async def comic_upload():
|
||||
return render_template('comic_upload.html')
|
||||
|
||||
if ct:
|
||||
tf = secure_filename(ct.filename)
|
||||
tf = f"{uuid.uuid4().hex}.webp"
|
||||
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)
|
||||
os.makedirs(cf, exist_ok=True)
|
||||
@ -716,10 +725,11 @@ async def comic_upload():
|
||||
pages = request.files.getlist('pages[]')
|
||||
for i, p in enumerate(sorted(pages, key=lambda x: x.filename), start=1):
|
||||
if p:
|
||||
filename = secure_filename(p.filename)
|
||||
filename = f"{uuid.uuid4().hex}.webp"
|
||||
file_path = os.path.join(cf, filename)
|
||||
webp_page = await convert_to_webp(p)
|
||||
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)
|
||||
db.session.add(new_page)
|
||||
@ -1100,7 +1110,7 @@ def update_related_tables(old_username, new_username):
|
||||
|
||||
@app.route('/upload_post', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def upload_post():
|
||||
async def upload_post():
|
||||
if request.method == 'POST':
|
||||
post_text = request.form.get('post_text')
|
||||
post_media = request.files.get('post_media')
|
||||
@ -1113,11 +1123,13 @@ def upload_post():
|
||||
db.session.add(new_post)
|
||||
|
||||
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']):
|
||||
media_filename = secure_filename(post_media.filename)
|
||||
media_path = os.path.join(app.config['UPLOAD_FOLDER']['posts'], media_filename)
|
||||
post_media.save(media_path)
|
||||
new_post.media_file = media_filename
|
||||
if await check_file_size(post_media, app.config['MAX_IMAGE_SIZE']):
|
||||
unique_filename = f"{uuid.uuid4().hex}.webp"
|
||||
media_path = os.path.join(app.config['UPLOAD_FOLDER']['posts'], unique_filename)
|
||||
webp_image = await convert_to_webp(post_media)
|
||||
async with aiofiles.open(media_path, 'wb') as f:
|
||||
await f.write(webp_image.read())
|
||||
new_post.media_file = unique_filename
|
||||
else:
|
||||
return redirect(url_for('upload_post'))
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user