-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile_browser.py
279 lines (222 loc) · 8.68 KB
/
file_browser.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
import os
import magic
import sys
import hashlib
import npyscreen
import curses
from config import *
from utils import *
from controller import ActionController
from post_form import MaxTitleText
class DeleteFileForm(npyscreen.ActionPopup):
OK_BUTTON_TEXT = "Delete!"
CANCEL_BUTTON_TEXT = "Cancel"
CANCEL_BUTTON_BR_OFFSET = (2, 16)
DEFAULT_LINES = 10
DEFAULT_COLUMNS = 80
def create(self):
self.value = None
self.wgLabel = self.add(npyscreen.FixedText)
self.wgLabel.editable = False
self.wgPassword = self.add(MaxTitleText, name="Password:", max_length=MAX_CHARS_TITLE)
def edit(self):
if not self.before_editing_passed:
self.parentApp.switchFormPrevious()
else:
super(DeleteFileForm, self).edit()
def beforeEditing(self):
self.before_editing_passed = False
global_filename = self.parentApp.myFile
if not is_in_sftp(global_filename):
return
if not os.path.isfile(global_filename):
return
local_filename = global_filename[len(SFTP_ROOT_DIR):].split("/")
if len(local_filename) != 3:
return
m = re.match("(^[0-9]+)_",local_filename[2])
if not m:
return
pid = 0
try:
pid = int(m.group(1), 10)
except:
return
bid = self.parentApp.myDatabase.name_to_bid(local_filename[0])
if not bid:
return
tid = 0
bid = int(bid[0])
try:
tid = int(local_filename[1], 10)
except:
return
self.hash = self.parentApp.myDatabase.get_hash(bid, tid, pid)
if not self.hash:
return
self.wgLabel.value = "Delete File: /{}?".format(global_filename[len(SFTP_ROOT_DIR):])
self.before_editing_passed = True
def on_ok(self):
if not self.hash['hash'] or not self.hash['salt']:
self.parentApp.switchFormPrevious()
hashphrase = hashlib.pbkdf2_hmac('sha256', self.wgPassword.value.encode("utf-8"), self.hash['salt'], 100000)
if hashphrase != self.hash['hash']:
self.parentApp.switchFormPrevious()
os.remove(self.parentApp.myFile)
self.parentApp.switchFormPrevious()
def on_cancel(self):
self.wgPassword.value = ""
self.parentApp.switchFormPrevious()
class TextViewer(npyscreen.FormMuttActiveTraditional):
ACTION_CONTROLLER = ActionController
MAIN_WIDGET_CLASS = npyscreen.Pager
def __init__(self, *args, **keywords):
super(TextViewer, self).__init__(*args, **keywords)
def beforeEditing(self,):
self.add_handlers({
curses.KEY_BACKSPACE: self.parentApp.switchFormPrevious,
})
self.value = self.parentApp.myFile
if not os.path.isfile(self.value):
self.parentApp.switchFormPrevious()
with open(self.value,'r') as content:
wrapped_lines = []
for line in content.readlines():
if line == "\n":
wrapped_lines.append("")
wrapped_lines.extend(textwrap.wrap(line, self.wMain.width-8))
self.wMain.values = wrapped_lines
class FileGrid(npyscreen.SimpleGrid):
default_column_number = 1
def __init__(self, *args, **keywords):
super(FileGrid, self).__init__(*args, **keywords)
self.select_whole_line = True
def set_up_handlers(self):
super(FileGrid, self).set_up_handlers()
self.handlers.update ({
curses.ascii.NL: self.h_select_file,
curses.ascii.CR: self.h_select_file,
curses.ascii.SP: self.h_select_file,
curses.KEY_DC: self.delete_file,
})
def change_dir(self, select_file):
try:
os.listdir(select_file)
except OSError:
npyscreen.notify_wait(title="Error", message="Cannot enter directory.")
return False
self.parent.parentApp.myPath = select_file
self.parent.update_grid()
self.edit_cell = [0, 0]
self.begin_row_display_at = 0
self.begin_col_display_at = 0
return True
def h_select_file(self, *args, **keywrods):
try:
select_file = os.path.join(self.parent.value, self.values[self.edit_cell[0]][self.edit_cell[1]])
select_file = os.path.abspath(select_file)
except (TypeError, IndexError):
self.edit_cell = [0, 0]
return False
if os.path.isdir(select_file):
self.change_dir(select_file)
else:
mime = magic.from_file(select_file, mime=True)
if mime[:5] == "text/":
self.parent.parentApp.myFile = select_file
self.parent.parentApp.switchForm("TEXTVIEWER")
elif mime == 'image/png' or mime == "image/jpeg":
self.parent.parentApp.myFile = select_file
self.parent.parentApp.switchForm("IMGVIEWER")
def delete_file(self, key):
select_file = ""
try:
select_file = os.path.join(self.parent.value, self.values[self.edit_cell[0]][self.edit_cell[1]])
select_file = os.path.abspath(select_file)
except (TypeError, IndexError):
return
if not os.path.isfile(select_file):
return
self.parent.parentApp.myFile = select_file
self.parent.parentApp.switchForm("DELETEFILE")
def display_value(self, vl):
p = os.path.split(vl)
#File
if p[1]:
return p[1]
#one up
elif vl == ".." + os.sep:
return os.path.split(p[0])[1] + os.sep
#Subdirectory
else:
name = os.path.split(p[0])[1] + os.sep
padding = max(int(self.width/2) - len(name), 1)
f, d = directory_stats(vl)
return name + " "*padding + "{} files, {} directories".format(f,d)
class FileBrowser(npyscreen.FormMuttActiveTraditional):
ACTION_CONTROLLER = ActionController
MAIN_WIDGET_CLASS = FileGrid
# MAIN_WIDGET_CLASS_START_LINE = 30
# BLANK_LINES_BASE = 0
def __init__(self, *args, **keywords):
super(FileBrowser, self).__init__(*args, **keywords)
self.value = self.parentApp.myPath
if not os.path.isdir(self.value):
self.value = SFTP_ROOT_DIR
if not os.path.isdir(SFTP_ROOT_DIR):
os.mkdir(SFTP_ROOT_DIR)
self.sort_by_extension = False
def beforeEditing(self,):
self.keypress_timeout = 80
self.update_grid()
self.stats_update()
self.add_handlers({
curses.KEY_BACKSPACE: self.parentApp.switchFormPrevious,
})
def reset_cursor(self):
self.wMain.h_show_beginning('')
def stats_update(self):
self.wStatus2.value = "%s Bits connected at tick %s " % (
get_connected_users(), datetime.now())
def while_waiting(self):
self.stats_update()
self.update_grid()
self.display()
def update_grid(self,):
#if self.value:
# self.value = os.path.expanduser(self.value)
self.value = self.parentApp.myPath
if not self.value.startswith(SFTP_ROOT_DIR):
self.value = SFTP_ROOT_DIR
if not os.path.exists(self.value):
self.value = SFTP_ROOT_DIR
self.value = os.path.abspath(self.value)
if os.path.isdir(self.value):
working_dir = self.value + os.sep
else:
working_dir = os.path.dirname(self.value) + os.sep
self.wStatus1.value = working_dir[len(os.path.abspath(SFTP_ROOT_DIR)):]
#self.wStatus2.value = "sftp {}:{}".format(HOSTNAME,working_dir[len(os.path.abspath(SFTP_ROOT_DIR)):])
file_list = []
if not os.path.abspath(SFTP_ROOT_DIR) == os.path.abspath(self.value):
file_list.append("..")
try:
file_list.extend([os.path.join(working_dir, fn) for fn in os.listdir(working_dir)])
except OSError:
npyscreen.notify_wait(title="Error", message="Could not read specified directory.")
new_file_list = []
for f in file_list:
f = os.path.normpath(f)
if os.path.isdir(f):
new_file_list.append(f + os.sep)
else:
new_file_list.append(f) # + "*")
file_list = new_file_list
del new_file_list
# sort Filelist
file_list.sort()
if self.sort_by_extension:
file_list.sort(key=self.get_extension)
file_list.sort(key=os.path.isdir, reverse=True)
self.wMain.set_grid_values_from_flat_list(file_list, reset_cursor=False)
self.display()