from flask import Flask, request, Response, render_template
from werkzeug.utils import secure_filename
import os
import uuid,datetime
from PIL import Image, ExifTags

app = Flask(__name__)  # 实例Flask应用
# 设置允许上传的文件格式
ALLOW_EXTENSIONS = ['png', 'jpg', 'jpeg','JPG','pdf','zip']

# 设置图片保存文件夹
app.config['UPLOAD_BLOGFOLDER'] = 'D:/static/BlogImgs/editor_u/blogimage/'
app.config['UPLOAD_PARTNOFOLDER'] = 'D:/static/BlogImgs/editor_u/product/'

# 设置图片返回的域名前缀
image_url = "https://static.kynix.com/blogimgs/editor_u/blogimage/"
# 设置图片压缩尺寸
image_c = 1000


# 跨域支持
def after_request(resp):
    resp.headers['Access-Control-Allow-Origin'] = '*'
    return resp


app.after_request(after_request)


# 判断文件后缀是否在列表中
def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[-1] in ALLOW_EXTENSIONS


# 首页
@app.route('/')
def hello_world():
    return render_template('index.html')


# 心跳检测
@app.route("/check", methods=["GET"])
def check():
    return 'Im live'


# 图片获取地址 用于存放静态文件
@app.route("/image/<imageId>")
def get_frame(imageId):
    # 图片上传保存的路径
    try:
        with open(r'./static/blogimage/{}'.format(imageId), 'rb') as f:
            image = f.read()
            result = Response(image, mimetype="image/jpg")
            return result
    except BaseException as e:
        return {"code": '503', "data": str(e), "message": "图片不存在"}
@app.route("/image/<imageId>")
def get_framePartnO(imageId):
    # 图片上传保存的路径
    try:
        with open(r'./static/partnoimage/{}'.format(imageId), 'rb') as f:
            image = f.read()
            result = Response(image, mimetype="image/jpg")
            return result
    except BaseException as e:
        return {"code": '503', "data": str(e), "message": "图片不存在"}

# 上传图片
@app.route("/upload_image/", methods=['POST', "GET"])
def uploads_blog():
    if request.method == 'POST':
        # 获取文件
        try:
            PartNo = request.values.get("PartNo")
            keys = request.values.get("keys")
            if keys !="kynixuoload":
                return {"code": '443', "data": "", "message": "口令校验失败"}
            src_save = request.values.get("src")
            if src_save == "BLOG":
                today = datetime.datetime.today()
                today =  str(today.year) + '-' + str(today.month) + '-' + str(today.day)
                true_src = "D:/static/BlogImgs/editor_u/blogimage/{}/".format(today)
                is_src = os.path.exists(true_src)
                if is_src == False:
                    os.makedirs(true_src)
                app.config['UPLOAD_BLOGFOLDER'] = 'D:/static/BlogImgs/editor_u/blogimage/'
                src_save = true_src
                image_url = "https://static.kynix.com/blogigms/editor_u/blogimage/{}/".format(today)
            if src_save == "PRODUCT"  and PartNo != None:
                true_src = "D:/static/imgs/uploadfiles/images/{}/".format(PartNo[0])
                is_src = os.path.exists(true_src)
                if is_src == False:
                    os.makedirs(true_src)
                src_save = true_src
                image_url = "https://static.kynix.com/imgs/uploadfiles/images/{}/".format(PartNo[0])
            if src_save == "PDF"  :
                today = datetime.datetime.today()
                today = str(today.year) + '-' + str(today.month) + '-' + str(today.day)
                true_src = "D:/static/BlogImgs/editor_u/pdf/{}/".format(today)
                is_src = os.path.exists(true_src)
                if is_src == False:
                    os.makedirs(true_src)
                src_save = true_src
                image_url = "https://static.kynix.com/BlogImgs/editor_u/pdf/{}/".format(today)
            if src_save == "ZIP"  :
                today = datetime.datetime.today()
                today = str(today.year) + '-' + str(today.month) + '-' + str(today.day)
                true_src = "D:/static/BlogImgs/editor_u/zip/{}/".format(today)
                is_src = os.path.exists(true_src)
                if is_src == False:
                    os.makedirs(true_src)
                src_save = true_src
                image_url = "https://static.kynix.com/BlogImgs/editor_u/zip/{}/".format(today)
            file = request.files['file']
            # 检测文件格式
            if  src_save == "Blog" or src_save == "PRODUCT":
                if file and allowed_file(file.filename):
                    # secure_filename方法会去掉文件名中的中文，获取文件的后缀名
                    file_name_hz = secure_filename(file.filename).split('.')[-1]
                    # 使用uuid生成唯一图片名
                    first_name = str(uuid.uuid4())
                    # 将 uuid和后缀拼接为 完整的文件名
                    file_name = first_name + '.' + file_name_hz
                    # 保存原图
                    file.save(os.path.join(src_save, file_name))

                    # 以下是压缩图片的过程，在原图的基础上
                    file_min = Image.open(file)
                    # exif读取原始方位信息 防止图片压缩后发生旋转
                    try:
                        for orientation in ExifTags.TAGS.keys():
                            if ExifTags.TAGS[orientation] == 'Orientation': break
                        exif = dict(file_min._getexif().items())
                        if exif[orientation] == 3:
                            file_min = file_min.rotate(180, expand=True)
                        elif exif[orientation] == 6:
                            file_min = file_min.rotate(270, expand=True)
                        elif exif[orientation] == 8:
                            file_min = file_min.rotate(90, expand=True)
                    except:
                        pass
                    # 获取原图尺寸
                    w, h = file_min.size
                    # 计算压缩比
                    bili = int(w / image_c)
                    if bili == 0:
                        bili = 1
                    # 按比例对宽高压缩
                    file_min.thumbnail((w // bili, h // bili))
                    # 生成缩略图的完整文件名
                    file_name_min = first_name + '_min.' + file_name_hz
                # 保存缩略图
                    file_min.save(os.path.join(src_save, file_name_min))
                # 返回原本和缩略图的 完整浏览链接
                return {"code": '200', "url": image_url + file_name,
                        "message": "上传成功"}
            else:
                return "格式错误，仅支持jpg、png、jpeg格式文件"
        except:
            return {"code": '500', "data": "", "message": "校验值错误"}
    return {"code": '503', "data": "", "message": "仅支持post方法"}


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8002, debug=True)  #
