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


from app.util.component.ApiTemplate import ApiTemplate
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
import json
from app.modules.image.ImageDataService import ImageDataService
from app.util.component.FileProcess import FileProcess
import datetime

import os
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 = 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}

                    if file_path.lower().endswith("tiff") or file_path.lower().endswith("tif"):
                        file_info["type"] = "tif"
                        data_list.append(file_info)
                    elif file_path.lower().endswith("img"):
                        file_info["type"] = "img"
                        data_list.append(file_info)
                    elif os.path.isdir(file_path):
                        file_info["type"] = "dir"
                        data_list.append(file_info)
                data_list_sorted = sorted(data_list, key=lambda x: x["name"])
                res["data"] = data_list_sorted

            else:
                host = data_server.split(":")[0]
                port = int(data_server.split(":")[1])

                transport = TSocket.TSocket(host, port)
                transport = TTransport.TBufferedTransport(transport)
                protocol = TBinaryProtocol.TBinaryProtocol(transport)
                client = ImageDataService.Client(protocol)
                transport.open()
                info= json.loads(client.getImageList(path))
                transport.close()
                res["data"] = info

            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": {
                    }
                }
            }
        }
    }