-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfile.py
More file actions
234 lines (183 loc) · 5.84 KB
/
file.py
File metadata and controls
234 lines (183 loc) · 5.84 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
from enum import IntEnum
from pydantic import BaseModel
import os
class FileType(IntEnum):
FOLDER = 0
FILE = 1
class FileItem(BaseModel):
"""文件信息"""
name: str
"""文件名称"""
size: int
"""文件大小(单位: byte)"""
time: str
"""文件修改时间"""
mode: int
"""文件操作权限(仅适用于Linux)"""
type: FileType
"""文件类型"""
daemonId: str = ""
"""远程节点uuid"""
uuid: str = ""
"""实例的uiid"""
target: str = ""
"""文件所在路径"""
file_name: str
"""当前文件列表过滤条件"""
def rename(self, newName: str) -> bool:
"""
重命名该文件或文件夹。
**参数:**
- new_name (str): 源文件或文件夹的新名字。
**返回:**
- bool: 重命名成功后返回True。
"""
from mcsmapi.apis.file import File
return File.rename(
self.daemonId, self.uuid, os.path.join(self.target, self.name), newName
)
def delete(self) -> bool:
"""
删除该文件或文件夹。
**返回:**
- bool: 重命名成功后返回True。
"""
from mcsmapi.apis.file import File
return File.delete(
self.daemonId, self.uuid, [os.path.join(self.target, self.name)]
)
def copy(self, target: str) -> bool:
from mcsmapi.apis.file import File
return File.copyOne(
self.daemonId, self.uuid, os.path.join(self.target, self.name), target
)
def move(self, target: str) -> bool:
"""
移动该文件或文件夹到目标路径。
**参数:**
- target (str): 目标文件或文件夹的路径。
**返回:**
- bool: 移动成功后返回True。
"""
from mcsmapi.apis.file import File
return File.moveOne(
self.daemonId, self.uuid, os.path.join(self.target, self.name), target
)
def content(self):
"""
获取文件内容。
**返回:**
- str | bytes: 文件内容。
"""
from mcsmapi.apis.file import File
return File.content(
self.daemonId, self.uuid, os.path.join(self.target, self.name)
)
def zip(self, targets: list[str]) -> bool:
"""
压缩多个文件或文件夹到指定位置。
**参数:**
- targets (list): 要压缩到的目标文件的路径。
**返回:**
- bool: 压缩成功后返回True。
"""
from mcsmapi.apis.file import File
return File.zip(
self.daemonId, self.uuid, os.path.join(self.target, self.name), targets
)
def unzip(self, target: str, code: str = "utf-8") -> bool:
"""
解压缩该 zip 文件到目标位置。
**参数:**
- target (str): 解压到的目标路径。
- code (str, optional): 压缩文件的编码方式,默认为"utf-8"。
可选值: utf-8, gbk, big5
**返回:**
- bool: 解压成功后返回True。
"""
from mcsmapi.apis.file import File
return File.unzip(
self.daemonId, self.uuid, os.path.join(self.target, self.name), target, code
)
def update(self, text: str) -> bool:
"""
更新该文件内容。
**参数:**
- text (str): 文件内容。
**返回:**
- bool: 更新成功后返回True。
"""
from mcsmapi.apis.file import File
return File.update(
self.daemonId, self.uuid, os.path.join(self.target, self.name), text
)
def download(self) -> str:
"""
下载该文件。
**返回:**
- str: 文件下载的URL。
"""
from mcsmapi.apis.file import File
return File.download(
self.daemonId, self.uuid, os.path.join(self.target, self.name)
)
class FileList(BaseModel):
"""文件列表"""
items: list[FileItem]
"""文件信息列表"""
page: int
"""当前页数"""
pageSize: int
"""文件列表单页大小"""
total: int
"""总页数"""
absolutePath: str
"""当前路径在远程节点的绝对路径"""
daemonId: str
"""远程节点uuid"""
uuid: str
"""实例uuid"""
target: str
"""文件(名称或目录)路径"""
def __init__(self, **data: str):
super().__init__(**data)
for item in self.items:
item.daemonId = self.daemonId
item.uuid = self.uuid
item.target = self.target
async def upload(self, file: bytes, upload_dir: str) -> bool:
"""
上传文件到实例。
**参数:**
- file (bytes): 要上传的文件内容。
- upload_dir (str): 上传文件的目标目录(包含文件名)。
**返回:**
- bool: 返回操作结果,成功时返回True。
"""
from mcsmapi.apis.file import File
return await File.upload(self.daemonId, self.uuid, file, upload_dir)
def createFile(self, target: str) -> bool:
"""
创建文件。
**参数:**
- target (str): 目标文件的路径,包含文件名。
**返回:**
- bool: 创建成功后返回True。
"""
from mcsmapi.apis.file import File
return File.createFile(self.daemonId, self.uuid, target)
def createFolder(self, target: str) -> bool:
"""
创建文件夹
**参数:**
- target (str): 目标文件夹的路径。
**返回:**
- bool: 创建成功后返回True。
"""
from mcsmapi.apis.file import File
return File.createFolder(self.daemonId, self.uuid, target)
class FileDownloadConfig(BaseModel):
password: str
"""文件下载密码"""
addr: str
"""文件下载地址"""