forked from poemusica/cloudinary-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
75 lines (54 loc) · 2.18 KB
/
server.py
File metadata and controls
75 lines (54 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from flask import Flask, jsonify, render_template, request, redirect, url_for
import cloudinary.uploader
import os
# CLOUDINARY INFORMATION
CLOUDINARY_KEY = os.environ['CLOUDINARY_KEY']
CLOUDINARY_SECRET = os.environ['CLOUDINARY_SECRET']
CLOUD_NAME = "heather-mahan" #"YOUR-CLOUD-NAME-HERE"
app = Flask(__name__)
# Flask routes
@app.route('/')
def homepage():
"""Show the homepage"""
return render_template('homepage.html')
@app.route('/media-upload-form')
def show_upload_form():
"""Show the simple media upload form"""
return render_template('media-upload-form.html')
@app.route('/async-media-upload-form')
def show_async_upload_form():
"""Show the async media upload form"""
return render_template('async-media-upload-form.html')
@app.route('/show-image')
def show_image():
"""Show the saved media on a web page"""
img_url = request.args.get('imgURL')
return render_template('results.html', img_src=img_url)
@app.route('/post-form-data', methods=['POST'])
def post_form_data():
"""Process form data and redirect to /show-image page"""
my_file = request.files['my-file']
img_url = upload_to_cloudinary(my_file)
add_user_img_record(img_url)
return redirect(url_for('show_image', imgURL=img_url))
@app.route('/post-form-data-async', methods=['POST'])
def post_form_data_async():
"""Process form data and return the url generated by Cloudinary"""
my_file = request.files['my-file']
img_url = upload_to_cloudinary(my_file)
add_user_img_record(img_url)
return jsonify(img_url)
# Helper functions
def upload_to_cloudinary(media_file):
"""Upload media file to Cloudinary"""
result = cloudinary.uploader.upload(media_file,
api_key=CLOUDINARY_KEY,
api_secret=CLOUDINARY_SECRET,
cloud_name=CLOUD_NAME)
return result['secure_url']
def add_user_img_record(img_url):
"""Stub function for persisting data to database"""
print("\n".join([f"{'*' * 20}", "SAVE THIS url to your database!!",
img_url, f"{'*' * 20}" ]))
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')