data_list.py 4.5 KB
# coding=utf-8
#author:        4N
#createtime:    2021/7/19
#email:         nheweijun@sina.com


from app.util.component.ApiTemplate import ApiTemplate

import json
from app.util.component.FileProcess import FileProcess
import datetime
from app.modules.service.image.util.ThriftConnect import ThriftConnect
import os
from app.models import db
from app.modules.service.image.models import Image
from .util.ImageType import ImageType

class Api(ApiTemplate):

    api_name = "影像数据列表"

    def process(self):


        # 返回结果
        res = {}

        try:
            data_server = self.para.get("data_server")

            path = self.para.get("path")


            if data_server.__eq__("本地服务器"):
                project_path = (os.path.dirname(os.path.abspath(__file__)))

                base_path = os.path.join(project_path, "data")
                if path:
                    base_path = os.path.normpath(path)

                data_list: list = []
                for f in os.listdir(base_path):

                    file_path = os.path.normpath(os.path.join(base_path, f))
                    file_size, real_size = FileProcess.get_file_size(file_path)

                    fctime = datetime.datetime.fromtimestamp(os.path.getctime(file_path)).strftime('%Y-%m-%d %H:%M:%S')

                    file_info = {"name": f, "path": file_path, "size": file_size, "create_time": fctime,"real_size": real_size}

                    if file_path.lower().endswith("tiff") or file_path.lower().endswith("tif") or file_path.lower().endswith("img") or os.path.isdir(file_path):

                        exist_image: Image = Image.query.filter_by(path=os.path.normpath(file_info.get("path")),
                                                            size=file_info.get("real_size")).one_or_none()
                        file_info["exist"] = False
                        if exist_image:
                            if exist_image.server.__contains__(data_server):
                                file_info["exist"] = True

                        file_info["type"] = ImageType.get_type(file_path)
                        data_list.append(file_info)


                info_dir = []
                info_img = []
                for dat in data_list:

                    if dat["type"].__eq__("dir"):
                        info_dir.append(dat)
                    else:
                        info_img.append(dat)

                info_dir = sorted(info_dir,key = lambda x: x["name"])
                info_img = sorted(info_img,key = lambda x: x["name"])
                info_dir.extend(info_img)

                res["data"] = info_dir

            else:
                thrift_connect = ThriftConnect(data_server)
                info= json.loads(thrift_connect.client.getImageList(path))
                thrift_connect.close()


                info_dir = []
                info_img = []
                for inf in info:
                    if inf["path"].lower().endswith("tiff") or inf["path"].lower().endswith("tif") or inf["path"].lower().endswith("img"):

                        exist_image = Image.query.filter_by(path=inf.get("path"),
                                                            size=inf.get("real_size")).one_or_none()
                        inf["exist"] = False
                        if exist_image:
                            if exist_image.server.__contains__(data_server):
                                inf["exist"] = True

                    if inf["type"].__eq__("dir"):
                        info_dir.append(inf)
                    else:
                        info_img.append(inf)

                info_dir = sorted(info_dir, key = lambda x: x["name"])
                info_img = sorted(info_img, key = lambda x: x["name"])
                info_dir.extend(info_img)

                res["data"] = info_dir

            res["result"] = True

        except Exception as e:
            raise e

        return res

    api_doc = {
        "tags": ["影像接口"],
        "parameters": [
            {"name": "data_server",
             "in": "formData",
             "type": "string",
             "description": "data_server"},
            {"name": "path",
             "in": "formData",
             "type": "string",
             "description": "path"}
        ],
        "responses": {
            200: {
                "schema": {
                    "properties": {
                    }
                }
            }
        }
    }