-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontroller3.py
More file actions
165 lines (105 loc) · 3.84 KB
/
controller3.py
File metadata and controls
165 lines (105 loc) · 3.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
# import packages
from flask import Flask
from flask import render_template_string
from flask import redirect
from flask import request
import os
import subprocess
import shutil
# create web app instance
app = Flask(__name__)
@app.route('/')
def root():
search_query = request.args.get('search')
# Get the list of files and folders in the current directory
file_list = subprocess.check_output('ls -l ', shell=True).decode('utf-8').split('\n')
# Filter the file list based on the search query
if search_query:
if os.path.isdir(search_query):
find_command = f"find {os.path.abspath(search_query)} -type d -not -path '*/\.*' -exec basename {{}} \\;"
else:
find_command = f"find {os.path.abspath(os.path.curdir)} -type f -not -path '*/\.*' -iname '*{search_query}*' -exec basename {{}} \\;"
matching_files = subprocess.check_output(find_command, shell=True).decode('utf-8').split('\n')
matching_files.remove('')
else:
matching_files = []
return render_template_string('''
<html>
<head>
<title>File manager</title>
<link rel="stylesheet" href="./static/css/main.css">
</head>
<body>
<div align="center">
<h1>Local file system</h1>
<p><strong>CWD: </strong>{{ current_working_directory }}</p>
</div>
<form action="/" method="get">
<label for="search">Search:</label>
<input type="text" id="search" name="search" value="{{ search_query }}">
<input type="submit" value="Search">
</form>
{% if matching_files %}
<h2>Matching files and folders:</h2>
<ul>
{% for file in matching_files %}
{% if file.endswith('.txt') or file.endswith('.py') or file.endswith('.json') %}
<li><strong><a href="/view?file={{current_working_directory + '/' + file}}">{{file}}</a></strong></li>
{% else %}
<li><strong><a href="/select?file={{file}}">{{file}}</a></strong></li>
{% endif %}
{% endfor %}
</ul>
{% endif %}
<h2>Directory listing:</h2>
<li><a href="/cd?path=..">..</a></li>
<table>
<thead>
<tr>
<th>Dossier</th>
<th>Date de modification</th>
<th>Taille</th>
</tr>
</thead>
<tbody>
{% for item in file_list[1:-1] %}
<tr>
<td>
{% if '.' not in item.split()[-1] %}
<a href="/cd?path={{current_working_directory + '/' + item.split()[-1]}}" style="text-decoration: none">{{ item.split()[-1] }}</a>
{% elif '.txt' in item.split()[-1] or '.py' in item.split()[-1] or '.json' in item.split()[-1] %}
<a href="/view?file={{current_working_directory + '/' + item.split()[-1]}}" style="text-decoration: none">{{ item.split()[-1] }}</a>
{% else %}
{{ item.split()[-1] }}
{% endif %}
</td>
<td>{{ item.split()[5:8] | join(' ') }}</td>
<td>{{ item.split()[4] }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
''', current_working_directory=os.getcwd(), file_list=file_list, matching_files=matching_files, search_query=search_query,os=os)
# handle 'cd' command
@app.route('/cd')
def cd():
# run 'level up' command
os.chdir(request.args.get('path'))
# redirect to file manager
return redirect('/')
# view text files
@app.route('/view')
def view():
# get the file content
with open(request.args.get('file')) as f:
return f.read().replace('\n', '<br>')
@app.route('/select')
def select():
file_path = request.args.get('file')
os.chdir(os.path.join(os.getcwd(), file_path))
return redirect("/")
# run the HTTP server
if __name__ == '__main__':
app.run(debug=True, threaded=True)