12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- 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')
|