setup flask page
parent
e6d64256ce
commit
a64753bf74
@ -1,6 +1,10 @@
|
||||
__pycache__/
|
||||
bin
|
||||
lib
|
||||
lib64
|
||||
media
|
||||
pyvenv.cfg
|
||||
uploads
|
||||
tmp
|
||||
*.log
|
||||
|
||||
|
@ -0,0 +1,79 @@
|
||||
from flask import Flask, render_template, request, flash, redirect, url_for
|
||||
from werkzeug.utils import secure_filename
|
||||
from PIL import Image
|
||||
import os
|
||||
import ffmpeg
|
||||
import datetime;
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
app.config['UPLOAD_FOLDER'] = "uploads"
|
||||
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)
|
||||
|
||||
|
||||
@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)
|
||||
|
||||
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(app.config['UPLOAD_FOLDER'], filename))
|
||||
return redirect(url_for('submitted'))
|
||||
elif ext in ['mp4', 'avi', 'mov', 'flv']:
|
||||
out_path = os.path.join(app.config['UPLOAD_FOLDER'], filename.rsplit('.', 1)[0].lower() + '.mp4')
|
||||
file.save(temp_path)
|
||||
stream = ffmpeg.input(temp_path)
|
||||
stream = ffmpeg.hflip(stream)
|
||||
stream = ffmpeg.output(stream, out_path)
|
||||
ffmpeg.run(stream)
|
||||
print("ive outputted somethign")
|
||||
# ffmpeg.input(temp_path).output(out_path,vf="scale=300:230", crf=24).run()
|
||||
# os.remove(temp_path)
|
||||
return redirect(url_for('submitted'))
|
||||
elif ext in ['mp3', 'wav', 'flac']:
|
||||
out_name = filename.rsplit('.', 1)[0].lower() + '.mp3'
|
||||
file.save(temp_path)
|
||||
ffmpeg.input(temp_path).output(os.path.join(app.config['UPLOAD_FOLDER'], out_name)).run()
|
||||
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")
|
@ -0,0 +1,146 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Document</title>
|
||||
|
||||
<style>
|
||||
html,
|
||||
body {
|
||||
margin: 0 0;
|
||||
font-family: sans-serif;
|
||||
padding: 0 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
* {
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
body {
|
||||
padding: 2rem;
|
||||
}
|
||||
|
||||
h1,
|
||||
h2,
|
||||
h3 {
|
||||
margin: 0 0;
|
||||
}
|
||||
|
||||
main {
|
||||
max-width: min(900px, calc(100vw - 4rem));
|
||||
margin: 2rem auto;
|
||||
}
|
||||
|
||||
|
||||
button,
|
||||
input[type="submit"] {
|
||||
margin: 1rem auto;
|
||||
background-color: black;
|
||||
outline: none;
|
||||
color: white;
|
||||
padding: .5rem 1rem;
|
||||
border-radius: 2px;
|
||||
border: none;
|
||||
z-index: 2;
|
||||
cursor: pointer;
|
||||
transition: .2s linear;
|
||||
border: 2px solid black;
|
||||
width: 100%;
|
||||
font-weight: bolder;
|
||||
}
|
||||
|
||||
button:hover,
|
||||
input[type="submit"]:hover {
|
||||
background-color: white;
|
||||
border: 2px solid black;
|
||||
color: black;
|
||||
}
|
||||
|
||||
form {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
border: 2px dashed black;
|
||||
margin: 2rem auto;
|
||||
padding: 2rem;
|
||||
gap: 1rem;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
form[loading="true"] * {
|
||||
opacity: 0;
|
||||
}
|
||||
|
||||
@keyframes spinning {
|
||||
0% {
|
||||
transform: rotate(0deg);
|
||||
}
|
||||
|
||||
100% {
|
||||
transform: rotate(360deg);
|
||||
}
|
||||
}
|
||||
|
||||
form[loading="true"]:before {
|
||||
content: "";
|
||||
position: absolute;
|
||||
width: 2rem;
|
||||
height: 2rem;
|
||||
border-top: 2px solid black;
|
||||
border-left: 2px solid black;
|
||||
border-radius: 100%;
|
||||
z-index: 2;
|
||||
top: calc(50% - 1rem);
|
||||
left: calc(50% - 1rem);
|
||||
transform: translate3d(-50%, -50%, 0);
|
||||
animation: .5s linear infinite spinning;
|
||||
}
|
||||
|
||||
.error {
|
||||
color: red;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<main class="container">
|
||||
|
||||
|
||||
<h1>Official ministry of infra-ordinary</h1>
|
||||
<h2>Ordinary sightings form</h2>
|
||||
<p>Have you seen anything infra-ordinary? Upload your media here!</p>
|
||||
|
||||
{% if error %}
|
||||
<h1 class="error">Oops.. Something went wrong: {{ error }}!</h1>
|
||||
{% endif %}
|
||||
|
||||
{% if submitted %}
|
||||
<p>Thank you for your contribution!</p>
|
||||
<a href="/">back</a>
|
||||
|
||||
{% else %}
|
||||
|
||||
<form method="POST" action="upload" enctype="multipart/form-data">
|
||||
<input type="file" name="file" accept="image/*,video/*,audio/*" required>
|
||||
<input type="submit">
|
||||
</form>
|
||||
|
||||
{% endif %}
|
||||
</main>
|
||||
<script type="text/javascript">
|
||||
window.addEventListener("DOMContentLoaded", function () {
|
||||
var form = document.querySelector("form");
|
||||
if (!form) return false
|
||||
|
||||
form.addEventListener("submit", function () {
|
||||
console.log("submit")
|
||||
form.setAttribute("loading", "true");
|
||||
})
|
||||
})
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
Loading…
Reference in New Issue