FileProcess.py
3.4 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# coding=utf-8
#author: 4N
#createtime: 2021/5/17
#email: nheweijun@sina.com
from flask import request
import uuid
import os
import stat
import platform
class FileProcess:
@classmethod
def save(cls,parent):
'''
:return:
'''
# 保存文件
file = request.files['file']
uuid_ = uuid.uuid1().__str__()
dir_path = os.path.join(parent, "file_tmp", uuid_)
os.makedirs(dir_path)
#有时候文件最后会多一个"
filename = file.filename.split('"')[0]
store_file = os.path.join(dir_path, filename)
file.save(store_file)
if platform.system().lower() == 'linux':
#设置为444权限
os.chmod(store_file, stat.S_IRUSR + stat.S_IRGRP + stat.S_IROTH)
return dir_path, store_file
@classmethod
def get_spatial_file2(cls,store_path):
file_for_open = None
encoding = None
for root, dirs, files in os.walk(store_path):
for fn in files:
if fn.endswith("shp"):
file_for_open = os.path.join(root, fn)
if fn.endswith("cpg"):
with open(os.path.join(root, fn)) as fd:
encoding = fd.readline().strip()
if fn.endswith("gdb"):
file_for_open = root
return file_for_open, encoding
@classmethod
def get_spatial_file(cls,store_path):
spatial_file= []
for root, dirs, files in os.walk(store_path):
for fn in files:
if fn.endswith("shp"):
file_for_open=os.path.join(root, fn)
encoding = None
cpg_path = os.path.join(root,"{}.cpg".format(fn.split(".shp")[0]))
if os.path.exists(cpg_path):
with open(cpg_path) as fd:
encoding=fd.readline().strip()
spatial_file.append((file_for_open,encoding))
if fn.endswith("gdb"):
spatial_file.append((root,None))
return spatial_file
@classmethod
def get_file_size(cls,file_path):
if os.path.isdir(file_path):
fsize = cls.get_dir_size(file_path)
else:
fsize = os.path.getsize(file_path)
if fsize>1024**3:
text_size= "{}GB".format(round(fsize/float(1024 **3), 1))
elif fsize>1024**2:
text_size = "{}MB".format(round(fsize / float(1024 ** 2), 1))
elif fsize>1024:
text_size = "{}KB".format(round(fsize / float(1024), 1))
else:
text_size = "{}B".format(round(fsize / float(1), 1))
return text_size,fsize
@classmethod
def get_text_size(cls,fsize):
if fsize>1024**3:
text_size= "{}GB".format(round(fsize/float(1024 **3), 1))
elif fsize>1024**2:
text_size = "{}MB".format(round(fsize / float(1024 ** 2), 1))
elif fsize>1024:
text_size = "{}KB".format(round(fsize / float(1024), 1))
else:
text_size = "{}B".format(round(fsize / float(1), 1))
return text_size
@classmethod
def get_dir_size(cls,dir):
size = 0
for root, dirs, files in os.walk(dir):
size += sum([os.path.getsize(os.path.join(root, name)) for name in files])
return size