app.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # IMPORTS
  2. from flask import Flask, render_template, request
  3. from werkzeug.utils import secure_filename
  4. import os
  5. # CONFIG
  6. app = Flask(__name__, instance_relative_config=True)
  7. app.config.from_object(os.environ['APP_SETTINGS'])
  8. from tools import upload_file_to_s3
  9. ALLOWED_EXTENSIONS = app.config["ALLOWED_EXTENSIONS"]
  10. # ROUTES
  11. def allowed_file(filename):
  12. return '.' in filename and \
  13. filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
  14. @app.route("/", methods=['GET', 'POST'])
  15. def index():
  16. if request.method == 'POST':
  17. # There is no file selected to upload
  18. if "user_file" not in request.files:
  19. return "No user_file key in request.files"
  20. file = request.files["user_file"]
  21. # There is no file selected to upload
  22. if file.filename == "":
  23. return "Please select a file"
  24. # File is selected, upload to S3 and show S3 URL
  25. if file and allowed_file(file.filename):
  26. file.filename = secure_filename(file.filename)
  27. output = upload_file_to_s3(file, app.config["S3_BUCKET"])
  28. return str(output)
  29. else:
  30. return render_template("index.html")