You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
3.5 KiB
Python
110 lines
3.5 KiB
Python
from flask import Flask, render_template, request, flash, redirect, url_for
|
|
from werkzeug.utils import secure_filename
|
|
from PIL import Image
|
|
import os, random, datetime
|
|
from ffmpeg import FFmpeg
|
|
|
|
app = Flask(__name__)
|
|
|
|
app.config['UPLOAD_FOLDER'] = "media"
|
|
app.config['TMP_FOLDER'] = "tmp"
|
|
|
|
def get_extension(filename):
|
|
return filename.rsplit('.', 1)[1].lower()
|
|
|
|
@app.route("/")
|
|
def index():
|
|
args = request.args
|
|
print("name")
|
|
print(args.get('result'))
|
|
return render_template('index.html', result=args.get('result'))
|
|
|
|
@app.route("/submitted")
|
|
def submitted():
|
|
print("doingsubmit")
|
|
return render_template('index.html', error=request.args.get('error'), submitted=True)
|
|
|
|
def get_random_subfolder():
|
|
# folder = os.listdir(app.config['UPLOAD_FOLDER'])
|
|
folder = [f for f in os.listdir(app.config['UPLOAD_FOLDER']) if not os.path.isfile(os.path.join(app.config['UPLOAD_FOLDER'], f))]
|
|
|
|
|
|
random.shuffle(folder)
|
|
print(folder[0])
|
|
return folder[0]
|
|
|
|
@app.route('/upload', methods=['POST'])
|
|
def upload_file():
|
|
global file_count
|
|
|
|
if request.method == 'POST':
|
|
if 'file' not in request.files:
|
|
flash('No file part')
|
|
return redirect(request.url)
|
|
file = request.files['file']
|
|
if file.filename == '':
|
|
flash('No selected file')
|
|
return redirect(request.url)
|
|
|
|
time = datetime.datetime.now().isoformat()
|
|
filename = f'{time}_{secure_filename(file.filename)}'
|
|
ext = get_extension(file.filename)
|
|
temp_path = os.path.join(app.config['TMP_FOLDER'], filename)
|
|
|
|
# random_sub = random.choice(os.listdir(app.config['UPLOAD_FOLDER']))
|
|
# sub_folder = os.path.join(app.config['UPLOAD_FOLDER'], random_sub)
|
|
sub_folder = os.path.join(app.config['UPLOAD_FOLDER'], get_random_subfolder())
|
|
print("saving to:")
|
|
print(sub_folder)
|
|
|
|
if ext in ['png', 'jpg', 'jpeg', 'gif']:
|
|
print("dealing with an image!")
|
|
img = Image.open(file)
|
|
img.thumbnail((100, 100), Image.LANCZOS)
|
|
|
|
img.save(os.path.join(sub_folder, filename))
|
|
return redirect(url_for('submitted'))
|
|
elif ext in ['mp4', 'avi', 'mov', 'flv']:
|
|
file.save(temp_path)
|
|
|
|
out_path = os.path.join(sub_folder, filename.rsplit('.', 1)[0].lower() + '.mp4')
|
|
|
|
ffmpeg = (
|
|
FFmpeg()
|
|
.option("y")
|
|
.input(temp_path)
|
|
.output(
|
|
out_path,
|
|
crf=40,
|
|
)
|
|
)
|
|
|
|
ffmpeg.execute()
|
|
os.remove(temp_path)
|
|
return redirect(url_for('submitted'))
|
|
elif ext in ['mp3', 'wav', 'flac']:
|
|
out_path = os.path.join(sub_folder, filename.rsplit('.', 1)[0].lower() + '.mp3')
|
|
file.save(temp_path)
|
|
|
|
ffmpeg = (
|
|
FFmpeg()
|
|
.option("y")
|
|
.input(temp_path)
|
|
.output(
|
|
out_path,
|
|
)
|
|
)
|
|
|
|
ffmpeg.execute()
|
|
|
|
os.remove(temp_path)
|
|
return redirect(url_for('submitted'))
|
|
else:
|
|
print("not an image!")
|
|
return redirect(url_for('submitted', Error="The file you are trying to upload is not supported by our infra-ordinary"))
|
|
|
|
print("do you even get here?")
|
|
|
|
else:
|
|
return render_template('index.html', Error="server error")
|