Add http upload

This commit is contained in:
videopi 2022-10-19 15:14:43 +02:00
parent 6f820fb201
commit caa6d4e10f
1 changed files with 47 additions and 5 deletions

50
app.py
View File

@ -1,18 +1,24 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, time, subprocess
from flask import Flask
from flask import Flask, flash, request, redirect, url_for
# HTTP auth
from flask_httpauth import HTTPBasicAuth
from werkzeug.security import generate_password_hash, check_password_hash
# FILE UPLOAD
from werkzeug.utils import secure_filename
UPLOAD_FOLDER = os.path.expanduser('~/Videos')
ALLOWED_EXTENSIONS = {'avi', 'mkv', 'mp4'}
#HTTPS
ASSETS_DIR = os.path.dirname(os.path.abspath(__file__))
# HTTP Serve
from waitress import serve
app = Flask(__name__)
#app.secret_key = b'flafoudi'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# Max upload size 100M ( see nginx default also )
app.config['MAX_CONTENT_LENGTH'] = 100 * 1000 * 1000
auth = HTTPBasicAuth()
users = {
@ -29,6 +35,14 @@ signal = 0
DEBUG = 0
# Check file extension is allowed
def allowed_file(filename):
# Check for dot in filename
if "." in filename:
# Split from right at first dot to find ext and allow files with "." in name
if filename.rsplit(".",1)[-1] in ALLOWED_EXTENSIONS:
return True
# Get Wifi signal level
def getRSSI():
signal = subprocess.run( local_bin + "get_rssi.sh", capture_output=True)
@ -76,7 +90,35 @@ def shutdown():
print(stdout)
return "Shuting down..."
# File upload
@app.route('/upload', methods=['GET', 'POST'])
@auth.login_required
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
return "No file part: " + str(request.files)
file = request.files['file']
# If the user does not select a file, the browser submits an
# empty file without a filename.
if file.filename == '':
return 'No selected file'
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return "File saved in " + UPLOAD_FOLDER
return "OK"
# return '''
# <!doctype html>
# <title>Upload new File</title>
# <h1>Upload new File</h1>
# <form method=post enctype=multipart/form-data>
# <input type=file name=file>
# <input type=submit value=Upload>
# </form>
# '''
if __name__ == '__main__':
# app.run()
# serve(app, host='0.0.0.0', port=5000)
serve(app, host='127.0.0.1', port=5000, url_scheme='https')