file-manager.py 5.84 KB
Newer Older
wanli's avatar
wanli committed
1
import pprint
wanli's avatar
wanli committed
2
import base64
wanli's avatar
wanli committed
3
from pathlib import Path
wanli's avatar
wanli committed
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
import json

disk_root = "D:\\projects\\scriptiot\\evm_app_store_files"

class FileManager(object):
    def __init__(self) -> None:
        pass

    '''
    @description: 根据前端传递的路径参数,获取该路径的目录以及文件信息,文件类别,可以看做是一个盘符
    @param {*} self
    @return {*}
    '''
    def initialize(self):
        '''
        disks: {
            files: {driver: "local"},
            images: {driver: "local"}
        }
        lang: "en"
        leftDisk: null
        rightDisk: null
        windowsConfig: 2
        '''

        result = {
            "disks": {},
            "lang": "zh",
            "leftDisk": None,
            "rightDisk": None,
            "windowsConfig": 1
        }

        # 这里需要过滤,有些目录只能管理员才能查看
wanli's avatar
wanli committed
38 39 40
        p = Path(disk_root)
        for child in p.iterdir():
            if child.is_dir():
wanli's avatar
wanli committed
41
                result["disks"].update({
wanli's avatar
wanli committed
42
                    child.name: { "driver": "local" }
wanli's avatar
wanli committed
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
                })

        return result

    '''
    @description: 获取当前类别的目录信息
    @param {*} self
    @return {*}
    '''
    def content(self, disk, target_path='/'):
        '''
        目录信息结构体:
        {
            basename: "docs"
            dirname: ""
            path: "docs"
            timestamp: 1556821372
            type: "dir"
        },
        {
            basename: "cars"
            dirname: "wallpapers"
            path: "wallpapers/cars"
            timestamp: 1544277291
            type: "dir"
        }

        文件信息结构体:
        {
            basename: "alfa.sql"
            dirname: "code"
            extension: "sql"
            filename: "alfa"
            path: "code/alfa.sql"
            size: 3208
            timestamp: 1544277274
            type: "file"
        }
        '''
wanli's avatar
wanli committed
82

wanli's avatar
wanli committed
83
        target_path = Path(target_path)
wanli's avatar
wanli committed
84 85 86 87 88
        result = {
            "directories": [],
            "files": []
        }

wanli's avatar
wanli committed
89 90
        disk_path = Path(disk_root).joinpath(disk)
        if not disk_path.exists():
wanli's avatar
wanli committed
91
            return result
wanli's avatar
wanli committed
92 93 94
            
        target_path = disk_path.joinpath(target_path)
        if not target_path.exists():
wanli's avatar
wanli committed
95 96
            return result

wanli's avatar
wanli committed
97 98 99 100 101 102 103
        for child in target_path.iterdir():
            if child.is_dir():
                result["directories"].append({
                    "basename": child.name,
                    "dirname": child.parent.relative_to(disk_path).as_posix(),
                    "path": child.resolve().relative_to(disk_path).as_posix(),
                    "timestamp": int(child.stat().st_mtime),
wanli's avatar
wanli committed
104
                    "type": "dir"
wanli's avatar
wanli committed
105
                })
wanli's avatar
wanli committed
106
            else:
wanli's avatar
wanli committed
107 108 109 110 111 112 113 114
                result["files"].append({
                    "basename": child.name,
                    "dirname": child.parent,
                    "extension": child.suffix[1:],
                    "filename": child.stem,
                    "path": child.resolve().relative_to(disk_path).as_posix(),
                    "size": child.stat().st_size,
                    "timestamp": int(child.stat().st_mtime),
wanli's avatar
wanli committed
115
                    "type": "file"
wanli's avatar
wanli committed
116 117
                })

wanli's avatar
wanli committed
118 119
        with open("result.json", "w") as f:
            json.dump(result, f)
wanli's avatar
wanli committed
120 121
            f.seek(0)
            f.truncate()
wanli's avatar
wanli committed
122 123 124 125 126 127 128 129 130 131
            f.write(json.dumps(result, ensure_ascii=True))

        pprint.pprint(result)
        return result

    '''
    @description: 获取目录结构树
    @param {*} self
    @return {*}
    '''
wanli's avatar
wanli committed
132
    def tree(self, disk, target_path="/"):
wanli's avatar
wanli committed
133 134 135 136 137 138 139 140 141 142 143 144 145
        '''
        {
            basename: "trees"
            dirname: "wallpapers/nature"
            path: "wallpapers/nature/trees"
            props: {
                hasSubdirectories: false
            }
            timestamp: 1544277291
            type: "dir"
        }
        '''

wanli's avatar
wanli committed
146
        target_path = Path(target_path)
wanli's avatar
wanli committed
147
        result = []
wanli's avatar
wanli committed
148 149 150
        rp = Path(disk_root)
        disk_path = rp / disk
        if not disk_path.exists():
wanli's avatar
wanli committed
151 152
            return result

wanli's avatar
wanli committed
153 154 155
        temp_path = disk_path.joinpath(target_path)
        if not temp_path.exists():
            return result
wanli's avatar
wanli committed
156

wanli's avatar
wanli committed
157 158
        p = Path(disk_path)
        for child in p.iterdir():
wanli's avatar
wanli committed
159 160 161 162 163 164
            if child.is_dir():
                result.append({
                    "basename": child.name,
                    "dirname": child.parent.relative_to(rp).as_posix(),
                    "path": child.relative_to(rp).as_posix(),
                    "props": {
wanli's avatar
wanli committed
165
                        "hasSubdirectories": True if child.iterdir() else False
wanli's avatar
wanli committed
166 167 168 169 170
                    },
                    "timestamp": int(child.stat().st_mtime),
                    "type": "dir"
                })
            # print("//////////", child.is_dir(), child.resolve(), child.name, child.parent, child.root, child.relative_to(rp))
wanli's avatar
wanli committed
171 172 173 174
        pprint.pprint(result)

        return result

wanli's avatar
wanli committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
    def selectDisk(self, disk):
        print(disk)
        return True

    def download(self, disk):
        print(disk)
        return True

    def preview(self, disk, target_file):
        # 预览图片
        img_stream = None
        if not target_file:
            target_file = './evue_photo.png'
        with open(target_file, 'rb') as img_f:
            img_stream = img_f.read()
            img_stream = base64.b64encode(img_stream).decode()
        print(disk, target_file)
        return img_stream

wanli's avatar
wanli committed
194 195 196 197 198 199 200 201 202 203 204
fileManager = FileManager()

if __name__ == "__main__":
    '''
    python -m memory_profiler example.py
    '''
    # from memory_profiler import profile
    # @profile
    # def test():
    #     pass

wanli's avatar
wanli committed
205 206
    result = fileManager.initialize()
    print("----->", result)
wanli's avatar
wanli committed
207
    
wanli's avatar
wanli committed
208 209
    result = fileManager.content("uploads", "evueapps/evm")
    print(">>>>>>", result)
wanli's avatar
wanli committed
210 211

    result = fileManager.tree("uploads", "evueapps/evm")
wanli's avatar
wanli committed
212 213 214 215
    print("=====>", result)

    result = fileManager.preview("uploads", "evueapps/evm")
    print("$$$$$>", result)