-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
132 lines (98 loc) · 3.19 KB
/
app.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import os, boto3, traceback,uuid
import mysql.connector.pooling
from flask import *
from dotenv import load_dotenv
load_dotenv()
dbconfig = {
"host":'dbfromaws.ckbctsif2sjr.us-east-1.rds.amazonaws.com',
"port":'3306',
"database":'message_db',
"user": os.getenv('RDS_USER'),
"password": os.getenv('RDS_PASSWORD'),
}
pool = mysql.connector.pooling.MySQLConnectionPool(pool_name = "mypool", pool_size = 5, **dbconfig) #create a pool which connect with DB
app = Flask (__name__)
app.config["JSON_AS_ASCII"] = False
app.config["TEMPLATES_AUTO_RELOAD"] = True
app.config['JSON_SORT_KEYS'] = False
app.secret_key = os.urandom(24)
ALLOWED_EXTENSIONS = {'png','jpg', 'jpeg', 'gif','tif'}
s3 = boto3.client('s3', aws_access_key_id = os.getenv('AWS_ACCESS_KEY'),
aws_secret_access_key = os.getenv('AWS_SECRET_KEY'))
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def upload_file(msg,img_url):
global data
try:
CN1 = pool.get_connection() #get a connection with pool.
print(CN1.connection_id,'RDS ID create')
cursor = CN1.cursor()
insert = """ insert into `message` (`word`, `img`) values(%s,%s);"""
cursor.execute(insert, (msg , img_url))
except Exception as e:
data = {"error": True,"message": "資料庫內部錯誤"}
print('資料庫內部錯誤',type(e),e)
CN1.rollback()
else:
data = {"ok": True, 'msg' : msg, 'img_url': img_url}
print('upload_file commit')
CN1.commit()
finally:
print(CN1.connection_id, 'upload_file close...', CN1.is_connected())
cursor.close()
CN1.close()
def get_file():
global data
try:
CN1 = pool.get_connection() #get a connection with pool.
print(CN1.connection_id,'RDS ID create')
cursor = CN1.cursor()
sql = """SELECT * FROM `message`
order by `id` desc
"""
cursor.execute(sql)
allList = cursor.fetchall() #tuple or None
data = {'ok' : True, 'allfiles': allList}
except Exception as e:
data = {"error": True,"message": "資料庫內部錯誤"}
print('資料庫內部錯誤',type(e),e)
CN1.rollback()
finally:
print(CN1.connection_id, 'get_file close...', CN1.is_connected())
cursor.close()
CN1.close()
# Pages
@app.route("/")
def index():
return render_template("index.html")
@app.route('/send', methods = ['POST'])
def send():
global data
#測試上傳檔案正確與否
try:
message = request.form['message']
picture = request.files['picture']
except :
print(traceback.format_exc(message))
data = {"error": True,"message": "輸入的檔案錯誤"}
return jsonify(data)
#附檔名是否合格
if allowed_file(picture.filename):
s3_object = 'message_img/' + uuid.uuid1().hex +'.' + picture.filename.rsplit('.', 1)[1].lower()
cdn_url = 'https://d3i2i3wop7mzlm.cloudfront.net/' + s3_object
try:
s3.upload_fileobj(picture, 'bucketfromaws', s3_object)
upload_file(message, cdn_url)
except:
print(traceback.format_exc())
data = {"error": True,"message": "伺服器內部錯誤"}
return jsonify(data)
else:
data = {"error": True,"message": "非圖片檔"}
return jsonify(data)
@app.route('/send', methods = ['GET'])
def getall():
global data
get_file()
return jsonify(data)
app.run(host='0.0.0.0')