|
7 | 7 | import uuid |
8 | 8 | import os |
9 | 9 |
|
| 10 | +ALLOWED_EXTENSIONS=set(['jpg','jpeg','bmp','png']) |
| 11 | + |
10 | 12 | mod_photos = Blueprint('photos',__name__,url_prefix='/photos/v1.0') |
11 | 13 |
|
| 14 | +def allowed_file(filename): |
| 15 | + return '.' in filename and filename.rsplit('.',1)[1] in ALLOWED_EXTENSIONS |
| 16 | + |
| 17 | + |
12 | 18 | @mod_photos.route('/photos',methods=['POST']) |
13 | 19 | def photos(): |
14 | 20 | app.logger.debug("Applying post photo...") |
15 | 21 |
|
16 | 22 | data = None |
17 | 23 | if request.method == 'POST': |
18 | 24 | file = request.files['file'] |
| 25 | + if file and not allowed_file(file.filename): |
| 26 | + resp = jsonify() |
| 27 | + resp.content_type='application/json' |
| 28 | + resp.status_code = 202 |
| 29 | + return resp |
| 30 | + |
19 | 31 | extension = os.path.splitext(file.filename)[1] |
20 | 32 | f_name=str(uuid.uuid4()) + extension |
21 | | - file.save(os.path.join(app.config['UPLOAD_FOLDER'], f_name)) |
| 33 | + filepath=os.path.join(app.config['UPLOAD_FOLDER'], f_name) |
| 34 | + photo = Photo(f_name,filepath) |
| 35 | + data ={'uuid':f_name, 'filepath': filepath} |
| 36 | + Photo.create(**data) |
| 37 | + file.save(filepath) |
22 | 38 | data = {'filename':f_name} |
23 | 39 | #TODO refactor to set up necessary methods to create responses |
24 | 40 | if data == None: |
25 | 41 | resp = jsonify() |
| 42 | + resp.status_code = 200 |
26 | 43 | else: |
27 | 44 | resp = jsonify(data) |
| 45 | + resp.status_code = 201 |
| 46 | + #TODO add diferent type of constructors |
28 | 47 | resp.content_type='application/json' |
29 | | - resp.status_code == 200 |
30 | 48 | return resp |
31 | 49 |
|
0 commit comments