-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfile.py
More file actions
333 lines (286 loc) · 10.7 KB
/
file.py
File metadata and controls
333 lines (286 loc) · 10.7 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
from mcsmapi.pool import ApiPool
from mcsmapi.request import Request, send, upload
from mcsmapi.models.file import FileDownloadConfig, FileList
import urllib.parse
import os
class File:
@staticmethod
def show(
daemonId: str,
uuid: str,
target: str = "",
page: int = 0,
page_size: int = 100,
file_name: str = "",
) -> FileList:
"""
获取文件列表
**参数:**
- daemonId (str): 守护进程的唯一标识符。
- uuid (str): 文件实例的唯一标识符。
- target (str, 可选): 用于文件过滤的目标路径。默认为空字符串,表示不按路径过滤。
- page (int, 可选): 指定分页的页码。默认为0。
- page_size (int, 可选): 指定每页的文件数量。默认为100。
- file_name (str, 可选): 用于在文件列表中过滤出名称包含指定字符串的文件或文件夹
**返回:**
- FileList: 包含文件列表信息和分页详情的FileList模型。
"""
result = send(
"GET",
f"{ApiPool.FILE}/list",
params={
"daemonId": daemonId,
"uuid": uuid,
"target": target,
"page": page,
"page_size": page_size,
"file_name": file_name,
},
)
return FileList(**result, daemonId=daemonId, uuid=uuid)
@staticmethod
def content(daemonId: str, uuid: str, target: str) -> str | bytes:
"""
获取文件内容
**参数:**
- daemonId (str): 守护进程的唯一标识符。
- uuid (str): 文件实例的唯一标识符。
- target (str): 文件的目标路径。
**返回:**
- str: 文件的内容信息。
"""
return send(
"PUT",
f"{ApiPool.FILE}",
params={"daemonId": daemonId, "uuid": uuid},
data={"target": target},
)
@staticmethod
def update(daemonId: str, uuid: str, target: str, text: str) -> bool:
"""
更新文件内容
**参数:**
- daemonId (str): 守护进程的唯一标识符。
- uuid (str): 文件实例的唯一标识符。
- target (str): 文件的目标路径。
- text (str): 新的文件内容。
**返回:**
- bool: 更新成功后返回True。
"""
return send(
"PUT",
f"{ApiPool.FILE}",
params={"daemonId": daemonId, "uuid": uuid},
data={"target": target, "text": text},
)
@staticmethod
def download(daemonId: str, uuid: str, file_name: str) -> str:
"""
下载文件
**参数:**
- daemonId (str): 守护进程的唯一标识符。
- uuid (str): 文件实例的唯一标识符。
- file_name (str): 要下载的文件名。路径+名字, 示例: /backup/world.zip
**返回:**
- str: 文件下载URL。
"""
result = send(
"POST",
f"{ApiPool.FILE}/download",
params={"daemonId": daemonId, "uuid": uuid, "file_name": file_name},
)
result = FileDownloadConfig(**result)
protocol = Request.mcsm_url.split("://")[0]
base_url = urllib.parse.urljoin(f"{protocol}://{result.addr}", "download")
return urllib.parse.urljoin(base_url, f"{result.password}/{file_name}")
@staticmethod
async def upload(daemonId: str, uuid: str, file: bytes, upload_dir: str) -> bool:
"""
上传文件
**参数:**
- daemonId (str): 守护进程的唯一标识符。
- uuid (str): 文件实例的唯一标识符。
- file (bytes): 要上传的文件内容。
- upload_dir (str): 文件上传到的目标路径。
**返回:**
- bool: 上传成功后返回True。
"""
result = send(
"POST",
f"{ApiPool.FILE}/upload",
params={"daemonId": daemonId, "uuid": uuid, "upload_dir": upload_dir},
)
result = FileDownloadConfig(**result)
protocol = Request.mcsm_url.split("://")[0]
base_url = urllib.parse.urljoin(f"{protocol}://{result.addr}", "upload")
final_url = urllib.parse.urljoin(base_url, result.password)
await upload(final_url, file)
return True
@staticmethod
def copy(daemonId: str, uuid: str, copy_map: dict[str, str]) -> bool:
"""
复制多个文件夹或文件到指定位置。
**参数:**
- daemonId (str): 守护进程的唯一标识符。
- uuid (str): 文件实例的唯一标识符。
- copy_map (dict): 复制映射,格式为 {源路径: 目标路径}
**返回:**
- bool: 上传成功后返回True。
"""
targets = [[source, target] for source, target in copy_map.items()]
return send(
"POST",
f"{ApiPool.FILE}/copy",
params={"daemonId": daemonId, "uuid": uuid},
data={"targets": targets},
)
@staticmethod
def copyOne(daemonId: str, uuid: str, source: str, target: str) -> bool:
"""
复制单个文件或文件夹到指定位置。
**参数:**
- daemonId (str): 守护进程的唯一标识符。
- uuid (str): 实例的唯一标识符。
- source (str): 源文件或文件夹的路径。
- target (str): 目标文件或文件夹的路径。
**返回:**
- bool: 移动成功后返回True。
"""
return File.copy(daemonId, uuid, {source: target})
@staticmethod
def move(daemonId: str, uuid: str, copy_map: dict[str, str]) -> bool:
"""
移动多个文件或文件夹到指定位置。
参数:
- daemonId (str): 守护进程的唯一标识符。
- uuid (str): 实例的唯一标识符。
- copy_map (dict): 移动映射,格式为 {源路径: 目标路径}
返回:
- bool: 移动成功后返回True。
"""
targets = [[source, target] for source, target in copy_map.items()]
return send(
"PUT",
f"{ApiPool.FILE}/move",
params={"daemonId": daemonId, "uuid": uuid},
data={"targets": targets},
)
@staticmethod
def moveOne(daemonId: str, uuid: str, source: str, target: str) -> bool:
"""
从源路径移动单个文件或文件夹到目标路径。
参数:
- daemonId (str): 守护进程的唯一标识符。
- uuid (str): 实例的唯一标识符。
- source (str): 源文件或文件夹的路径。
- target (str): 目标文件或文件夹的路径。
返回:
- bool: 移动成功后返回True。
"""
return File.move(daemonId, uuid, {source: target})
@staticmethod
def rename(daemonId: str, uuid: str, source: str, new_name: str) -> bool:
"""
重命名单个文件或文件夹。
**参数:**
- daemonId (str): 守护进程的唯一标识符。
- uuid (str): 实例的唯一标识符。
- source (str): 源文件或文件夹的路径。
- new_name (str): 源文件或文件夹的新名字。
**返回:**
- bool: 重命名成功后返回True。
"""
directory = os.path.dirname(source)
target = os.path.join(directory, new_name)
return File.moveOne(daemonId, uuid, source, target)
@staticmethod
def zip(daemonId: str, uuid: str, source: str, targets: list[str]) -> bool:
"""
压缩多个文件或文件夹到指定位置。
**参数:**
- daemonId (str): 守护进程的唯一标识符。
- uuid (str): 实例的唯一标识符。
- source (str): 需要压缩的文件路径。
- targets (list): 要压缩到的目标文件的路径。
**返回:**
- bool: 压缩成功后返回True。
"""
return send(
"POST",
f"{ApiPool.FILE}/compress",
params={"daemonId": daemonId, "uuid": uuid},
data={"type": 1, "code": "utf-8", "source": source, "targets": targets},
)
@staticmethod
def unzip(
daemonId: str, uuid: str, source: str, target: str, code: str = "utf-8"
) -> bool:
"""
解压缩指定的zip文件到目标位置。
**参数:**
- daemonId (str): 守护进程的唯一标识符。
- uuid (str): 实例的唯一标识符。
- source (str): 需要解压的zip文件路径。
- target (str): 解压到的目标路径。
- code (str, optional): 压缩文件的编码方式,默认为"utf-8"。
可选值: utf-8, gbk, big5
**返回:**
- bool: 解压成功后返回True。
"""
return send(
"POST",
f"{ApiPool.FILE}/compress",
params={"daemonId": daemonId, "uuid": uuid},
data={"type": 2, "code": code, "source": source, "targets": target},
)
@staticmethod
def delete(daemonId: str, uuid: str, targets: list[str]) -> bool:
"""
删除多个文件或文件夹。
**参数:**
- daemonId (str): 守护进程的唯一标识符。
- uuid (str): 实例的唯一标识符。
- targets (list): 要删除的文件或文件夹的路径。
**返回:**
- bool: 删除成功后返回True。
"""
return send(
"DELETE",
ApiPool.FILE,
params={"daemonId": daemonId, "uuid": uuid},
data={"targets": targets},
)
@staticmethod
def createFile(daemonId: str, uuid: str, target: str) -> bool:
"""
创建文件。
**参数:**
- daemonId (str): 守护进程的唯一标识符。
- uuid (str): 实例的唯一标识符。
- target (str): 目标文件的路径,包含文件名。
**返回:**
- bool: 创建成功后返回True。
"""
return send(
"POST",
f"{ApiPool.FILE}/touch",
params={"daemonId": daemonId, "uuid": uuid},
data={"target": target},
)
@staticmethod
def createFolder(daemonId: str, uuid: str, target: str) -> bool:
"""
创建文件夹
**参数:**
- daemonId (str): 守护进程的唯一标识符。
- uuid (str): 实例的唯一标识符。
- target (str): 目标文件夹的路径。
**返回:**
- bool: 创建成功后返回True。
"""
return send(
"POST",
f"{ApiPool.FILE}/mkdir",
params={"daemonId": daemonId, "uuid": uuid},
data={"target": target},
)