Skip to content

Commit 09f648b

Browse files
committed
Post communication done.
1 parent 912b9d6 commit 09f648b

File tree

7 files changed

+55
-8
lines changed

7 files changed

+55
-8
lines changed

api-rest-python-deeplearning.ini

-21 Bytes
Binary file not shown.

app/mod_photos/controllers.py

+49-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import uuid
1111
import os
12+
import base64
1213

1314
ALLOWED_EXTENSIONS=set(['jpg','jpeg','bmp','png','JPG','JPEG','BMP','PNG'])
1415

@@ -20,15 +21,15 @@ def allowed_file(filename):
2021
return '.' in filename and filename.rsplit('.',1)[1] in ALLOWED_EXTENSIONS
2122

2223
def timestamp():
23-
import time
24-
return str(time.time()).split(".")[0]
24+
import time
25+
return str(time.time()).split(".")[0]
2526

2627
@mod_photos.route('/photos',methods=['POST'])
2728
def post_photos():
2829
app.logger.debug("Applying post photo...")
2930

30-
data = None
31-
if request.method == 'POST':
31+
data = None
32+
if request.method == 'POST':
3233
app.logger.debug("Applying post image")
3334
file = request.files['file']
3435

@@ -47,15 +48,57 @@ def post_photos():
4748
data_to_object ={'uuid':identifier, 'filepath': filepath, 'analysed':False, 'info':str("empty")}
4849
new_photo = Photo.create(**data_to_object)
4950

50-
data = {'filename':f_name, 'id': new_photo.id}
51+
data = {'filename':f_name, 'id2': new_photo.id}
5152
#TODO refactor to set up necessary methods to create responses
5253
#TODO is it necessary to return 200 command
5354
resp = None
5455
if data == None:
5556
resp = responses.new200()
5657
else:
5758
resp = responses.new201(data)
58-
return resp
59+
return resp
60+
61+
@mod_photos.route('/photoStr',methods=['POST'])
62+
def post_photosStr():
63+
app.logger.debug("Applying post photo...")
64+
65+
data = None
66+
if request.method == 'POST':
67+
68+
content = request.get_json()
69+
if content is not None:
70+
filename = content['filename']
71+
extension = os.path.splitext(filename)[1]
72+
73+
imageEncoded = content['image']
74+
image = base64.b64decode(imageEncoded)
75+
#image = image.decode('base64')
76+
77+
app.logger.debug(data)
78+
identifier=timestamp()+str(uuid.uuid4())
79+
80+
f_name=identifier + extension
81+
app.logger.debug("f_name="+f_name)
82+
app.logger.debug("identifier="+identifier)
83+
filepath=os.path.join(app.config['UPLOAD_FOLDER'], f_name)
84+
# or, more concisely using with statement
85+
with open(filepath, "wb") as fh:
86+
fh.write(image)
87+
#First result
88+
89+
data_to_object ={'uuid':identifier, 'filepath': filepath, 'analysed':False, 'info':str("empty")}
90+
new_photo = Photo.create(**data_to_object)
91+
92+
data = {'filename':f_name, 'id2': new_photo.id }
93+
#TODO refactor to set up necessary methods to create responses
94+
#TODO is it necessary to return 200 command
95+
resp = None
96+
if data == None:
97+
resp = responses.new200()
98+
else:
99+
resp = responses.new201(data)
100+
return resp
101+
59102

60103
@mod_photos.route('/photos/<int:id>',methods=['GET'])
61104
def get_photos_one(id):

app/templates/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ <h3>{{responseid}}</h3>
4444
<img id="image1">
4545
</div>
4646

47-
<button ng-click="uploadFile()"> upload me</button>
47+
<button ng-click="uploadFile()"> upload me</button>
4848
</form>
4949
<div id="result"></div>
5050
</body>

celerybeat-schedule

0 Bytes
Binary file not shown.

config.py

31 Bytes
Binary file not shown.

test_curl.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
curl -F "userid=1"-F "filecomment=This is an image file" -F "file=@/home/carlos/Desktop/caffenet/cat.jpg" localhost:5000/photos/v1.0/photos
1+
curl -F "file=@/home/aberenguel/Downloads/rick_morty.jpg" 158.109.9.235/photos/v1.0/photos

test_curlSmall.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
curl -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -H "Postman-Token: c28ca6db-7c36-906c-fa8a-e7f4a8cee5e4" -d '{
2+
"filename":"test.png",
3+
"image":""
4+
}' "http://158.109.9.235:5000/photos/v1.0/photoStr"

0 commit comments

Comments
 (0)