-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
executable file
Β·284 lines (230 loc) Β· 9.42 KB
/
Copy pathapp.py
File metadata and controls
executable file
Β·284 lines (230 loc) Β· 9.42 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
from flask import Flask, render_template, send_from_directory, send_file, abort, redirect, url_for, request, jsonify
import os
from flask import session
app = Flask(__name__)
app.secret_key = 'hardikfileexplorer_1a8d3f90e0b74e22b6f9d87ac4fcd134'
# Actual SSD path
FILE_ROOT = "/workspaces/" # <-- Update this to your mount path #my ssd is plugged into my router for ease/
# Allowed file extensions
VIDEO_EXTENSIONS = ('.mp4', '.mkv', '.avi', '.mov', '.webm')
PDF_EXTENSION = '.pdf'
IMAGE_EXTENSIONS = ('.jpg', '.jpeg', '.png', '.gif', '.webp')
# Folders to hide
HIDDEN_FOLDERS = {'server', 'System Volume Information', '$RECYCLE.BIN', 'server2'}
#For DropX
UPLOAD_FOLDER = 'dropx_storage'
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
@app.route('/')
def root():
default_tab = session.get('default_tab', 'files')
if default_tab == 'videos':
return redirect(url_for('all_videos'))
elif default_tab == 'images':
return redirect(url_for('all_images'))
elif default_tab == 'pdfs':
return redirect(url_for('all_pdfs'))
elif default_tab == 'dropx':
return redirect(url_for('dropx'))
else:
return redirect(url_for('browse', subpath=''))
@app.route('/browse/', defaults={'subpath': ''})
@app.route('/browse/<path:subpath>')
def browse(subpath):
full_path = os.path.join(FILE_ROOT, subpath)
if not os.path.exists(full_path):
return abort(404)
try:
entries = os.listdir(full_path)
except PermissionError:
return "Access denied to this folder."
folders = [e for e in entries
if os.path.isdir(os.path.join(full_path, e))
and not e.startswith('.')
and e not in HIDDEN_FOLDERS]
videos = [e for e in entries
if e.lower().endswith(VIDEO_EXTENSIONS)
and not e.startswith('.')]
pdfs = [e for e in entries
if e.lower().endswith(PDF_EXTENSION)
and not e.startswith('.')]
# β
Only show other_files if enabled in session
other_files = []
if session.get('show_other_files', False):
other_files = [e for e in entries
if os.path.isfile(os.path.join(full_path, e))
and not e.lower().endswith(VIDEO_EXTENSIONS)
and not e.lower().endswith(PDF_EXTENSION)
and not e.startswith('.')]
return render_template("index.html",
folders=folders,
videos=videos,
pdfs=pdfs,
other_files=other_files,
subpath=subpath)
@app.route('/video/<path:filepath>')
def stream_video(filepath):
dir_path = os.path.join(FILE_ROOT, os.path.dirname(filepath))
filename = os.path.basename(filepath)
return send_from_directory(dir_path, filename)
@app.route('/static/<path:filename>')
def static_files(filename):
return send_from_directory('static', filename)
@app.route('/play/<path:filepath>')
def play_video(filepath):
return render_template("player.html", filepath=filepath)
@app.route('/viewpdf')
def view_pdf():
pdf_path = request.args.get('pdf')
return render_template('pdfviewer.html', pdf_path=pdf_path)
def get_all_pdfs(base_dir):
pdf_list = []
for root, dirs, files in os.walk(base_dir):
for file in files:
if file.startswith('.'):
continue
if file.lower().endswith(PDF_EXTENSION):
full_path = os.path.join(root, file)
rel_path = os.path.relpath(full_path, base_dir)
mtime = os.path.getmtime(full_path)
pdf_list.append((rel_path.replace("\\", "/"), mtime))
return sorted(pdf_list, key=lambda x: x[1], reverse=True)
@app.route('/all_pdfs')
def all_pdfs():
base_dir = FILE_ROOT # adjust as needed #GoldernHaze
pdfs = get_all_pdfs(base_dir)
return render_template("pdfs.html", pdfs=[p[0] for p in pdfs])
@app.route('/file/<path:path>')
def serve_file(path):
full_path = os.path.join(FILE_ROOT, path)
if os.path.isfile(full_path):
return send_file(full_path)
else:
return "File not found", 404
def get_all_videos(base_dir):
video_list = []
for root, dirs, files in os.walk(base_dir):
for file in files:
if file.startswith("._"): # β Skip macOS metadata files
continue
if file.lower().endswith(VIDEO_EXTENSIONS):
full_path = os.path.join(root, file)
rel_path = os.path.relpath(full_path, base_dir)
mtime = os.path.getmtime(full_path)
video_list.append((rel_path.replace("\\", "/"), mtime))
# Sort by modified time (descending)
return sorted(video_list, key=lambda x: x[1], reverse=True)
@app.route('/all_videos')
def all_videos():
base_dir = FILE_ROOT # <-- change this if needed #GoldernHaze
videos = get_all_videos(base_dir)
return render_template("videos.html", videos=[v[0] for v in videos])
def get_all_images(base_dir):
image_list = []
for root, dirs, files in os.walk(base_dir):
for file in files:
if file.startswith("._") or file.startswith("."):
continue
if file.lower().endswith(IMAGE_EXTENSIONS):
full_path = os.path.join(root, file)
rel_path = os.path.relpath(full_path, base_dir).replace("\\", "/")
mtime = os.path.getmtime(full_path)
image_list.append((rel_path, mtime))
return sorted(image_list, key=lambda x: x[1], reverse=True)
@app.route('/all_images')
def all_images():
base_dir = FILE_ROOT
images = get_all_images(base_dir)
return render_template("images.html", images=[img[0] for img in images])
@app.route("/search")
def search():
query = request.args.get("q", "").lower()
content_type = request.args.get("type", "files")
base_dir = FILE_ROOT
matched = []
for root, dirs, files in os.walk(base_dir):
for file in files:
if file.startswith("._"):
continue # skip dot files
full_path = os.path.join(root, file)
rel_path = os.path.relpath(full_path, base_dir).replace("\\", "/")
if query in file.lower():
if content_type == "videos" and file.lower().endswith(VIDEO_EXTENSIONS):
matched.append(rel_path)
elif content_type == "pdfs" and file.lower().endswith(".pdf"):
matched.append(rel_path)
elif content_type == "files":
matched.append(rel_path)
return render_template("search_results.html", query=query, results=matched, tab=content_type)
@app.route('/settings', methods=['GET', 'POST'])
def settings():
if request.method == 'POST':
# Save checkbox setting to session
show_other_files = request.form.get('show_other_files') == 'on'
session['show_other_files'] = show_other_files
# Optional: you can also add more settings here
default_tab = request.form.get('default_tab', 'files')
session['default_tab'] = default_tab
return redirect(url_for('settings'))
return render_template(
'settings.html',
show_other_files=session.get('show_other_files', False),
default_tab=session.get('default_tab', 'files')
)
@app.route('/dropx')
def dropx():
files = sorted(os.listdir(UPLOAD_FOLDER), reverse=True)
return render_template('dropx.html', files=files, tab='dropx')
@app.route('/upload-dropx', methods=['POST'])
def upload_dropx():
file = request.files['file']
name = request.form.get('rename') or file.filename
file.save(os.path.join(UPLOAD_FOLDER, name))
return '', 204
@app.route('/list-dropx')
def list_dropx():
files = os.listdir(UPLOAD_FOLDER)
return jsonify(sorted(files, reverse=True))
@app.route('/dropx_storage/<filename>')
def download_dropx_file(filename):
return send_from_directory(UPLOAD_FOLDER, filename)
@app.route('/delete-dropx/<filename>', methods=['POST'])
def delete_dropx(filename):
os.remove(os.path.join(UPLOAD_FOLDER, filename))
return '', 204
@app.route('/delete-dropx-file/<filename>')
def delete_dropx_file(filename):
os.remove(os.path.join(UPLOAD_FOLDER, filename))
return '', 204
@app.route('/delete-all-dropx', methods=['POST'])
def delete_all_dropx():
for file in os.listdir(UPLOAD_FOLDER):
os.remove(os.path.join(UPLOAD_FOLDER, file))
return '', 204
@app.route('/edit/<path:filename>')
def edit_file(filename):
file_path = os.path.join(FILE_ROOT, filename)
if not os.path.isfile(file_path):
return f"File not found: {filename}", 404
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
return render_template('text_editor.html', filename=filename, content=content)
@app.route('/save', methods=['POST'])
def save_file():
filename = request.form['filename']
content = request.form['content']
file_path = os.path.join(FILE_ROOT, filename)
try:
with open(file_path, 'w', encoding='utf-8') as f:
f.write(content)
flash('File saved successfully!', 'success')
except Exception as e:
flash(f'Error saving file: {e}', 'error')
return redirect(url_for('edit_file', filename=filename))
if __name__ == '__main__':
app.run(
host='0.0.0.0',
#port=80, #global
port=3000,
#ssl_context=('cert/game.com.pem', 'cert/game.com-key.pem'),
debug=True
)