-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
152 lines (132 loc) · 6.07 KB
/
server.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from flask import Flask, url_for, send_from_directory, request
import logging, os
from werkzeug.utils import secure_filename
from flask import jsonify, make_response
import roundrobin
import storage_client
import traversal_client
import time
import urllib
app = Flask(__name__)
file_handler = logging.FileHandler('server.log')
app.logger.addHandler(file_handler)
app.logger.setLevel(logging.INFO)
PROJECT_HOME = os.path.dirname(os.path.realpath(__file__))
UPLOAD_FOLDER = '{}/uploads/'.format(PROJECT_HOME)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def create_new_folder(local_dir):
newpath = local_dir
if not os.path.exists(newpath):
os.makedirs(newpath)
return newpath
@app.route('/', methods = ['POST'])
def api_root():
app.logger.info(PROJECT_HOME)
if request.method == 'POST' and request.files['image']:
start = time.time()
app.logger.info(app.config['UPLOAD_FOLDER'])
img = request.files['image']
img_name = secure_filename(img.filename)
# client = storage_client.Client(roundrobin.getIpAddress())
client = storage_client.Client(roundrobin.getIpAddress()+":2750")
client.upload(img, img_name)
end = time.time()
print("File size in bytes==", 119669)
print("Total time with file im memory for upload= ",end - start)
return make_response(jsonify({"success":True}),200)
else:
return "Where is the file?"
@app.route('/getFile', methods = ['GET'])
def getFile():
if request.method == 'GET':
fileId = request.args.get('fileId')
if fileId is None :
return make_response(jsonify({"success":False,"error":"Content is Missing"}),501)
else:
# Get the file from grpc using file ID from request
fileId = urllib.parse.unquote(fileId)
# hashedFileId = secure_filename(fileId)
# print("FILE ID :: ",fileId,hashedFileId);
ipaddress = roundrobin.getIpAddress()
client = traversal_client.TraversalClient(ipaddress+":2750", ipaddress)
result = client.download(fileId)
if result is None:
return make_response(jsonify({"success":False,"error":"No such file Present"}),501)
print("===============result==============")
print(result)
#return file in bytes array to client in "content"
return make_response(jsonify({"success":True, "content": result}),200)
else:
return make_response(jsonify({"success":False,"error":"No File Present"}),501)
@app.route('/addFile', methods = ['POST'])
def addFile():
if request.method == 'POST':
try:
data = request.get_json()
fileId = data['fileId']
file = data['content']
fileSize = data['size']
if fileId is None or file is None:
print("NO CONTENT")
return make_response(jsonify({"success":False,"error":"Content is Missing"}),501)
else:
# hashedFileId = secure_filename(fileId)
# Send This to GRPC function
# client = storage_client.Client(roundrobin.getIpAddress())
# client.upload(file, hashedFileId)
#hashedFileId = secure_filename(fileId)
print("FILE ID :: ",fileId)
client = storage_client.Client(roundrobin.getIpAddress()+":2750")
success = client.upload(file, fileId, fileSize)
return make_response(jsonify({"success":True}),200)
except Exception as e:
print("EXCEPTION",e)
return make_response(jsonify({"success":False,"error":"Content is Missing"}),501)
else:
print("NO POST")
return make_response(jsonify({"success":False,"error":"No File Present"}),501)
@app.route('/getMessage', methods = ['GET'])
def getMessage():
if request.method == 'GET':
messageId = request.args.get('messageId')
print("messageId--get--==", messageId)
if messageId is None :
return make_response(jsonify({"success":False,"error":"Content is Missing"}),501)
else:
# client = storage_client.Client(roundrobin.getIpAddress())
client = storage_client.Client("127.0.0.1:2750")
result = client.getMessage(messageId)
if result is None:
return make_response(jsonify({"success":False,"error":"No Message with this messageId is present"}),501)
return make_response(jsonify({"success":True, "message": result, "messageId": messageId}),200)
else:
return make_response(jsonify({"success":False,"error":"No Message Present"}),501)
@app.route('/addMessage', methods = ['POST'])
def addMessage():
if request.method == 'POST':
try:
data = request.get_json()
messageId = data['messageId']
message = data['message']
if messageId is None or message is None:
return make_response(jsonify({"success":False,"error":"Content is Missing !"}),501)
else:
# client = storage_client.Client(roundrobin.getIpAddress())
client = storage_client.Client(roundrobin.getIpAddress()+":2750")
# messageBytes = bytearray(message,'utf-8')
# hashedMessageId = secure_filename(messageId)
# client = grpc_client.Client(roundrobin.getIpAddress())
# client.upload(messageBytes, hashedMessageId)
# client = grpc_client.Client(roundrobin.getIpAddress())
print("message==", message)
print("messageId==", messageId)
client.sendMessage(message, messageId)
return make_response(jsonify({"success":True}),200)
except Exception as e:
print("EXCEPTION",e)
return make_response(jsonify({"success":False,"error":"Content is Missing"}),501)
else:
print("NO POST")
return make_response(jsonify({"success":False,"error":"No Message Present"}),501)
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=False,threaded=True)