"""
  @author: LinTao
  @time: 2022/10/23 10:06
  @desc:
"""
import time

from flask import Blueprint, request, render_template, jsonify
import os

from common.utility import compress_image

ueditor = Blueprint("ueditor", __name__)


@ueditor.route('/uedit', methods=['GET', 'POST'])
def uedit():
    """根据UEditor的接口定义规则, 如果前端参数为action=config，则表示试图请求后台的config.json文件，请求成功则说明后台接口正常工作"""
    param = request.args.get('action')
    if request.method == 'GET' and param == 'config':
        return render_template('config.json')
    elif request.method == 'POST' and request.args.get('action') == 'uploadimage':
        f = request.files['upfile']  # 获取前端图片文件数据
        filename = f.filename

        # 为上传来的文件生成统一的文件名
        suffix = filename.split('.')[-1]
        newname = time.strftime('%Y%m%d_%H%M%S.' + suffix)
        f.save('./resource/upload/' + newname)  # 保存图片

        # 对图片进行压缩，按照1200像素宽度为准，并覆盖原始图片
        source = dest = './resource/upload/' + newname
        compress_image(source, dest, 1200)

        result = {}  # 构造响应数据
        result['state'] = 'SUCCESS'
        result['url'] = f"/upload/{newname}"
        result['title'] = filename
        result['original'] = filename
        return jsonify(result)  # 以JSON数据格式返回响应，供前端编辑器使用
    elif request.method == 'GET' and param == 'listimage':
        list = []
        filelist = os.listdir('./resource/upload')
        # 将所有图片构建成可访问的URL地址并添加到列表中
        for filename in filelist:
            if filename.lower().endswith('.png') or filename.lower().endswith('.jpg'):
                list.append({'url': '/upload/%s' % filename})

        result = {}  # 构造响应数据
        result['state'] = 'SUCCESS'
        result['list'] = list
        result['start'] = 0
        result['total'] = 50
        return jsonify(result)  # 以JSON数据格式返回响应，供前端编辑器使用
