FileProcess.py 3.4 KB
# 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