-
Notifications
You must be signed in to change notification settings - Fork 2
/
filemanager.py
363 lines (294 loc) · 11.2 KB
/
filemanager.py
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import os
import ujson
import ubinascii
fm_500 = "HTTP/1.1 500 Internal Server Error\r\nContent-Type: text/plain\r\n\r\n"
fm_200_json = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n"
fm_200_text = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n"
def read_in_chunks(file_object, chunk_size=1024):
while True:
data = file_object.read(chunk_size)
if not data:
break
yield data
def urldecode(str):
dic = {"%21":"!","%22":'"',"%23":"#","%24":"$","%26":"&","%27":"'","%28":"(","%29":")","%2A":"*","%2B":"+","%2C":",","%2F":"/","%3A":":","%3B":";","%3D":"=","%3F":"?","%40":"@","%5B":"[","%5D":"]","%7B":"{","%7D":"}"}
for k,v in dic.items(): str=str.replace(k,v)
return str
def parse_query_string(query_string):
query = query_string.split('?')[1]
params = query.split('&')
param_dict = {}
for param in params:
key, value = param.split('=')
param_dict[key] = value
return param_dict
def is_directory(path):
try:
return os.stat(path)[0] & 0x4000 != 0
except OSError:
return False
def file_exists(path):
try:
os.stat(path)
return True
except:
return False
def path_exists(path):
try:
os.stat(path)
return True
except OSError:
return False
def list_directory_contents(base_path):
contents = []
try:
for entry in os.listdir(base_path):
if base_path == '/':
entry_path = '/' + entry
else:
entry_path = base_path + '/' + entry
#print(entry_path)
if is_directory(entry_path):
contents.append({
'name': entry,
'path': entry_path,
'isDirectory': True
})
else:
contents.append({
'name': entry,
'path': entry_path,
'isDirectory': False,
'size': os.stat(entry_path)[6]
})
except OSError as e:
print("OSError:", e)
return contents
def delete_path(path):
if not path_exists(path):
print(f"Path {path} does not exist.")
return
stack = [path]
while stack:
current_path = stack.pop()
if is_directory(current_path):
try:
entries = list(os.ilistdir(current_path))
if not entries:
# Directory is empty, we can delete it
os.rmdir(current_path)
#print(f"Deleted directory: {current_path}")
else:
# Add directory back to stack to try again later
stack.append(current_path)
# Add entries to stack
for entry in entries:
entry_path = current_path + '/' + entry[0]
stack.append(entry_path)
except Exception as e:
print(f"Error accessing directory {current_path}: {e}")
else:
try:
os.remove(current_path)
#print(f"Deleted file: {current_path}")
except Exception as e:
print(f"Error deleting file {current_path}: {e}")
def handle_contents(client, path, request):
try:
query_params = path.split('?path=')[1] if '?path=' in path else '/'
full_path = query_params
contents = list_directory_contents(full_path)
response = ujson.dumps({"contents": contents})
client.send(fm_200_json)
client.send(response)
except Exception as e:
print("Error:", e)
client.send(fm_500)
client.send("Internal Server Error")
def handle_upload(client, path, request):
try:
_, filepath, filesize = path.split(';')
filesize = int(filesize) * 2
data_read = 0
#print("Upload: " + str(filepath) + " size: " + str(filesize))
#print("0%")
with open(filepath, 'wb') as file:
data = request[request.find(b'\r\n\r\n') + 4:]
if data:
data_read = len(data)
if data_read % 2 == 0:
file.write(ubinascii.unhexlify(data))
else:
data = data + client.read(1)
file.write(ubinascii.unhexlify(data))
data_read = len(data)
while data_read < filesize:
try:
chunk_size = min(1024, filesize - data_read)
chunk = client.read(chunk_size)
file.write(ubinascii.unhexlify(chunk))
data_read = data_read + chunk_size
percentage = (data_read / filesize) * 100
#print(f"{percentage:.1f}%")
if not chunk:
break
except OSError as e:
if e.args[0] == 116: # ETIMEDOUT
break
else:
raise e
#print("File Saved")
client.send(fm_200_text)
client.send("Upload successful")
except Exception as e:
print("Error during file upload:", e)
client.send(fm_500)
client.send("Upload failed")
def handle_download(client, path, request):
try:
file_path = urldecode(path).split('?path=')[1]
file_name = file_path.split('/')[-1]
if file_exists(file_path):
client.send(f"HTTP/1.1 200 OK\r\nContent-Type: application/octet-stream\r\nContent-Disposition: attachment; filename=\"{file_name}\"\r\n\r\n")
with open(file_path, 'rb') as f:
for piece in read_in_chunks(f):
client.write(piece)
else:
client.send("HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\n\r\n")
client.send("File not found.")
except OSError as e:
print("OSError:", e)
client.send(fm_500)
client.send("Internal Server Error")
def handle_delete(client, path, request):
try:
files = ujson.loads(urldecode(path).split('?files=')[1])
for file_path in files:
delete_path(file_path)
client.send(fm_200_text)
client.send("Files deleted successfully.")
except OSError as e:
print("OSError:", e)
client.send(fm_500)
client.send("Internal Server Error")
def handle_rename(client, path, request):
try:
query_params = ujson.loads(urldecode(path).split('?data=')[1])
old_name = query_params['old_name']
new_name = query_params['new_name']
os.rename(old_name, new_name)
client.send(fm_200_text)
client.send("File renamed successfully.")
except OSError as e:
print("OSError:", e)
client.send(fm_500)
client.send("Internal Server Error")
def handle_newfolder(client, path, request):
try:
query_params = ujson.loads(urldecode(path).split('?data=')[1])
folderpath = query_params['foldername']
os.mkdir(folderpath)
client.send(fm_200_text)
client.send("New folder created successfully.")
except OSError as e:
print("OSError:", e)
client.send(fm_500)
client.send("Internal Server Error")
def handle_copy(client, path, request):
try:
query_params = ujson.loads(urldecode(path).split('?data=')[1])
src_files = query_params['src']
dest_path = query_params['dest']
stack = []
for src in src_files:
stack.append((src, dest_path))
while stack:
current_src, current_dest = stack.pop()
if is_directory(current_src):
new_dir_path = current_dest + '/' + current_src.split('/')[-1]
os.mkdir(new_dir_path)
for entry in os.listdir(current_src):
stack.append((current_src + '/' + entry, new_dir_path))
else:
with open(current_src, 'rb') as f_src:
with open(current_dest + '/' + current_src.split('/')[-1], 'wb') as f_dest:
for piece in read_in_chunks(f_src):
f_dest.write(piece)
client.send(fm_200_text)
client.send("Files copied successfully.")
except OSError as e:
print("OSError:", e)
client.send(fm_500)
client.send("Internal Server Error")
def handle_move(client, path, request):
try:
query_params = ujson.loads(urldecode(path).split('?data=')[1])
src_files = query_params['src']
dest_path = query_params['dest']
stack = []
for src in src_files:
stack.append((src, dest_path))
# Přesun souborů a adresářů
while stack:
current_src, current_dest = stack.pop()
if is_directory(current_src):
new_dir_path = current_dest + '/' + current_src.split('/')[-1]
if not path_exists(new_dir_path):
os.mkdir(new_dir_path)
for entry in os.listdir(current_src):
stack.append((current_src + '/' + entry, new_dir_path))
else:
os.rename(current_src, current_dest + '/' + current_src.split('/')[-1])
# Smazání prázdných původních adresářů
for src in src_files:
delete_path(src)
client.send(fm_200_text)
client.send("Files moved successfully.")
except OSError as e:
print("OSError:", e)
client.send(fm_500)
client.send("Internal Server Error")
def delete_path(path):
if not path_exists(path):
#print(f"Path {path} does not exist.")
return
stack = [path]
# Iterační průchod pro mazání souborů a podadresářů
while stack:
current_path = stack.pop()
if is_directory(current_path):
try:
entries = list(os.ilistdir(current_path))
if not entries:
os.rmdir(current_path)
else:
stack.append(current_path)
for entry in entries:
entry_path = current_path + '/' + entry[0]
stack.append(entry_path)
except Exception as e:
print(f"Error accessing directory {current_path}: {e}")
else:
try:
os.remove(current_path)
except Exception as e:
print(f"Error deleting file {current_path}: {e}")
def handle_status(client, path, request):
try:
s = os.statvfs('//')
flash100 = (s[0]*s[2])/1048576
flash = (s[0]*s[3])/1048576
P = flash/flash100*100
contents = ({
'progress': '{0:.1f}'.format(P),
'memoryFree': '{0:.1f}'.format(flash),
'memoryTotal': '{0:.1f}'.format(flash100)
})
response = ujson.dumps(contents)
client.send(fm_200_json)
client.send(response)
#print(response)
except Exception as e:
print("Error:", e)
client.send(fm_500)
client.send("Internal Server Error")