get_data_list.py
2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# coding=utf-8
# author: 4N
# createtime: 2020/9/4
# email: nheweijun@sina.com
import os
from app.util.component.ApiTemplate import ApiTemplate
from app.util.component.FileProcess import FileProcess
import datetime
class Api(ApiTemplate):
api_name = "本地数据list"
def process(self):
res = {}
try:
project_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))))
base_path = os.path.join(project_path,"tmp")
if self.para.get("data_path"):
base_path = os.path.normpath(self.para.get("data_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.endswith("shp"):
file_info["type"]="shp"
whole_size = file_info["size"][1]
dbf_path = ".".join([file_path.split(".")[0],"dbf"])
if os.path.exists(dbf_path):
whole_size += FileProcess.get_file_size(dbf_path)[1]
shx_path = ".".join([file_path.split(".")[0],"shx"])
if os.path.exists(shx_path):
whole_size += FileProcess.get_file_size(shx_path)[1]
whole_size_text = FileProcess.get_text_size(whole_size)
file_info["size"] = [whole_size_text,whole_size]
data_list.append(file_info)
elif file_path.endswith("gdb"):
file_info["type"]="gdb"
data_list.append(file_info)
elif file_path.endswith("zip"):
file_info["type"]="zip"
data_list.append(file_info)
elif os.path.isdir(file_path):
bn = os.path.basename(file_path)
if not len(bn.split("-"))==5:
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
res["result"]=True
except Exception as e:
raise e
return res
api_doc={
"tags":["IO接口"],
"parameters":[
{"name": "data_path",
"in": "formData",
"type": "string",
"description": "数据文件路径"}
],
"responses":{
200:{
"schema":{
"properties":{
}
}
}
}
}