Fixed bugs

This commit is contained in:
Heorhii Mykhailenko 2023-06-17 17:09:50 +03:00
parent d887231d04
commit abfece95e7
3 changed files with 77 additions and 72 deletions

2
mail.py Executable file → Normal file
View file

@ -38,7 +38,7 @@ def send_email(destination, attachment):
# Add header as key/value pair to attachment part # Add header as key/value pair to attachment part
part.add_header( part.add_header(
'Content-Disposition', 'Content-Disposition',
f'attachment; filename= {os.path.normpath(attachment)}', f'attachment; filename= {os.path.basename(attachment)}',
) )
# Add attachment to message and convert message to string # Add attachment to message and convert message to string

17
main.py Executable file → Normal file
View file

@ -76,7 +76,8 @@ def video():
abort(400) abort(400)
path = os.path.join('videos', id) path = os.path.join('videos', id)
if os.path.isfile(path): if os.path.isfile(path):
clip = moviepy.VideoFileClip(path) with moviepy.VideoFileClip(path) as clip:
length = int(clip.duration)
if request.method == 'POST': if request.method == 'POST':
start = request.form.get('start') start = request.form.get('start')
end = request.form.get('end') end = request.form.get('end')
@ -117,7 +118,7 @@ def video():
file.save(watermarkPath) file.save(watermarkPath)
formatter = {'PNG': 'RGBA', 'JPEG': 'RGB'} formatter = {'PNG': 'RGBA', 'JPEG': 'RGB'}
img = Image.open(watermarkPath) with Image.open(watermarkPath) as img:
rgbimg = Image.new(formatter.get( rgbimg = Image.new(formatter.get(
img.format, 'RGB'), img.size) img.format, 'RGB'), img.size)
rgbimg.paste(img) rgbimg.paste(img)
@ -136,18 +137,22 @@ def video():
if changed: if changed:
logger = BarLogger(id) logger = BarLogger(id)
if 'newId' in locals(): if 'newId' in locals():
os.remove(path) oldPath = path
id = newId path = os.path.join('videos', newId)
path = os.path.join('videos', id)
if audio: if audio:
audio.write_audiofile(path, logger=logger) audio.write_audiofile(path, logger=logger)
audio.close()
else: else:
clip.write_videofile(path, logger=logger) clip.write_videofile(path, logger=logger)
clip.close()
send_email(email, path) send_email(email, path)
if changed:
if 'oldPath' in locals():
os.remove(oldPath)
os.remove(path) os.remove(path)
return render_template('success.html', error=error) return render_template('success.html', error=error)
return render_template('video.html', length=int(clip.duration), error=error) return render_template('video.html', length=length, error=error)
else: else:
abort(404) abort(404)

2
templates/success.html Executable file → Normal file
View file

@ -11,7 +11,7 @@
<p>The following errors occurred:<br> <p>The following errors occurred:<br>
{{ error | safe }}</p> {{ error | safe }}</p>
{% endif %} {% endif %}
<p><a href="index.html">Upload another video</a></p> <p><a href="/">Upload another video</a></p>
</body> </body>
</html> </html>