Browse Source

Merge branch 'master' of http://git.choozmo.com:3000/choozmo/bhouse_api

deployer 3 years ago
parent
commit
6e3175cf8f
1 changed files with 46 additions and 0 deletions
  1. 46 0
      models/statics/routes.py

+ 46 - 0
models/statics/routes.py

@@ -0,0 +1,46 @@
+from flask import request, Blueprint
+from flask_restful import Resource, Api
+from os import path, remove, listdir
+import logging
+from models.config import STATIC_DIR
+
+statics_app = Blueprint('statics', __name__)
+api = Api(statics_app)
+logger = logging.getLogger(__name__)
+
+
+class StaticImg(Resource):
+    IMG_DIR = path.join(STATIC_DIR, 'img')
+
+    def post(self):
+        img_data = request.files['image']
+        img_dir = path.join(self.IMG_DIR,
+                            request.args.get('type', type=str),
+                            request.args.get('filename', type=str))
+        img_data.save(img_dir)
+        return {'filename': request.args.get('filename', type=str)}
+
+    def delete(self):
+        img_dir = path.join(self.IMG_DIR,
+                            request.args.get('type', type=str),
+                            request.args.get('filename', type=str))
+        remove(img_dir)
+        return {'filename': request.args.get('filename', type=str)} 
+
+
+def get_static_imgs_src(type_):
+    def allow_ext(filename):
+        return '.' in filename and \
+           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
+    result = []
+    ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'webp'}
+    TYPE_IMG_DIR = path.join(STATIC_DIR, 'img', type_)
+    base_src = path.join('/img', type_)
+    for f in listdir(TYPE_IMG_DIR):
+        if not allow_ext(f):
+            continue
+        result.append(path.join(base_src, f))
+    return result
+
+
+api.add_resource(StaticImg, '/api/statics/img')