9
9
10
10
import uuid
11
11
import os
12
+ import base64
12
13
13
14
ALLOWED_EXTENSIONS = set (['jpg' ,'jpeg' ,'bmp' ,'png' ,'JPG' ,'JPEG' ,'BMP' ,'PNG' ])
14
15
@@ -20,15 +21,15 @@ def allowed_file(filename):
20
21
return '.' in filename and filename .rsplit ('.' ,1 )[1 ] in ALLOWED_EXTENSIONS
21
22
22
23
def timestamp ():
23
- import time
24
- return str (time .time ()).split ("." )[0 ]
24
+ import time
25
+ return str (time .time ()).split ("." )[0 ]
25
26
26
27
@mod_photos .route ('/photos' ,methods = ['POST' ])
27
28
def post_photos ():
28
29
app .logger .debug ("Applying post photo..." )
29
30
30
- data = None
31
- if request .method == 'POST' :
31
+ data = None
32
+ if request .method == 'POST' :
32
33
app .logger .debug ("Applying post image" )
33
34
file = request .files ['file' ]
34
35
@@ -47,15 +48,57 @@ def post_photos():
47
48
data_to_object = {'uuid' :identifier , 'filepath' : filepath , 'analysed' :False , 'info' :str ("empty" )}
48
49
new_photo = Photo .create (** data_to_object )
49
50
50
- data = {'filename' :f_name , 'id ' : new_photo .id }
51
+ data = {'filename' :f_name , 'id2 ' : new_photo .id }
51
52
#TODO refactor to set up necessary methods to create responses
52
53
#TODO is it necessary to return 200 command
53
54
resp = None
54
55
if data == None :
55
56
resp = responses .new200 ()
56
57
else :
57
58
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
+
59
102
60
103
@mod_photos .route ('/photos/<int:id>' ,methods = ['GET' ])
61
104
def get_photos_one (id ):
0 commit comments