【flask】针对UEditor编写api

By Heanny
2020-4-15
2255 read

自己在写的flask页面时,前端用的md,后端用的ue,官网上都是php、jsp、asp的接口,然而自己人生苦短,所以做了记录

from app.config.ueditor import Config
from app.service.uploader import Upload

Ueditor = Blueprint('Ueditor', __name__,
                    template_folder='templates')


def getFileList(config):
    reData = []
    files = os.listdir(config['path'])
    for file in files:
        file_n_path = os.path.join(config['path'], file)
        if os.path.isfile(file_n_path):
            fileType = file.split('.')[-1]
            if '.{}'.format(fileType) in config['allow']:
                reData.append(config['Prefix'].format(file=file))
        else:
            pass
    return reData

@Ueditor.route('/controller.do', methods=['get', 'post'])
# @_isLogin
def uediter_do():
    try:
        action = request.args.get('action')
        # action = request.args.get('action') if request.method == 'GET' else request.form.get('action')
        noCache = request.args.get('noCache') if request.method == 'GET' else request.form.get('noCache')
        print(action)
        if action == 'config':
            print(noCache)
            return Response(json.dumps(Config), content_type='application/json')
        elif action in ['uploadimage', 'uploadfile', 'uploadvideo']:
            if action == 'uploadimage':
                # 上传图片
                field_name = Config['imageFieldName']
                config = {
                    "pathFormat": Config['imagePathFormat'],  # 文件保存目录
                    "fileFormat": Config['imageFileFormat'],  # 文件名
                    "maxSize": Config['imageMaxSize'],  # 上传大小限制
                    "allowFiles": Config['imageAllowFiles'],  # 上传图片格式
                    "urlPrefix": Config['imageUrlPrefix']  # 图片访问路径
                }
            elif action == 'uploadvideo':
                field_name = Config['videoFieldName']
                config = {
                    "pathFormat": Config['videoPathFormat'],  # 文件保存目录
                    "fileFormat": Config['videoFileFormat'],  # 文件名
                    "urlPrefix": Config['videoUrlPrefix'],  # 图片访问路径
                    "maxSize": Config['videoMaxSize'],
                    "allowFiles": Config['videoAllowFiles']
                }
            else:
                field_name = Config['fileFieldName']
                config = {
                    "pathFormat": Config['filePathFormat'],
                    "fileFormat": Config['fileFileFormat'],  # 文件名
                    "urlPrefix": Config['fileUrlPrefix'],  # 图片访问路径
                    "maxSize": Config['fileMaxSize'],
                    "allowFiles": Config['fileAllowFiles']
                }
            # 判断文件是否真实
            if field_name in request.files:
                # 获取文件
                file = request.files.get(field_name)
                # 执行上传
                upload_obj = Upload(file, config, config['pathFormat'])
                result = upload_obj.get_file_info()
                return jsonify(result)
            else:
                return jsonify(state='文件错误')
        elif action == 'uploadscrawl':
            # 上传涂鸦
            config = {
                "pathFormat": Config['scrawlPathFormat'],
                "maxSize": Config['scrawlMaxSize'],
                # "allowFiles": CONFIG['scrawlAllowFiles'],
                "oriName": "scrawl.png",
                "fileFormat": Config['scrawlFileFormat'],  # 文件名
                "urlPrefix": Config['scrawlUrlPrefix'],  # 图片访问路径
            }
            field_name = Config['scrawlFieldName']
            if field_name in request.form:
                uploader = Upload(request.form[field_name], config, config['pathFormat'], 'base64')
                result = uploader.get_file_info()
                return jsonify(result)
            else:
                return jsonify(state='上传接口错误')

        elif action == 'catchimage':
            # 抓取远程文件
            # 抓取远程文件和截图没有写,截图工具需要下载,没必要
            start = request.args.get('start') if request.method == 'GET' else request.form.get('start')
            size = request.args.get('size') if request.method == 'GET' else request.form.get('size')
            return jsonify(list=[], start="{}".format(start), state="no match file", total=0)
        elif action in ['listfile', 'listimage']:
            if action == 'listfile':
                # 列出文件
                config = {'path': Config['fileManagerListPath'],
                          'Prefix': Config['fileManagerUrlPrefix'],
                          'allow': Config['fileManagerAllowFiles']}
            else:
                # 列出图片
                config = {'path': Config['imageManagerListPath'],
                          'Prefix': Config['imageManagerUrlPrefix'],
                          'allow': Config['imageManagerAllowFiles']}
            return jsonify(list=getFileList(config), start="0", state="no match file", total=0)
        else:
            return jsonify(state='请求地址出错')
    except Exception as e:
        print(e)
        return jsonify(state='error:{}'.format(e))

因为截图需要安插件,抓取远程文件没找到该功能,所以没写

参考了托小尼https://www.diandian100.cn的部分代码,进行了优化


更多代码详见https://gitee.com/heanny/codes/tm7w5al18qvdoehjngi2931


【flask】JinJa2模版渲染及其他小记
关于centos7使用php当中遇到的问题

Comments

暂无评论,还不快来坐沙发...

Leave a Reply