-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
444 lines (362 loc) · 15 KB
/
Copy pathapp.py
File metadata and controls
444 lines (362 loc) · 15 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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import os
import yaml
import uuid
from datetime import datetime
from flask import Flask, request, jsonify, render_template, redirect, url_for, send_from_directory, abort
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user
from werkzeug.utils import secure_filename
import logging
from logging.handlers import RotatingFileHandler
import importlib.util
import sys
import json
from flask_socketio import SocketIO
import psutil
# Fonction pour charger dynamiquement tous les modules dans un répertoire et exécuter une fonction spécifique
def load_modules_from_directory(directory):
directory = os.path.abspath(directory)
if not os.path.exists(directory):
os.makedirs(directory)
for filename in os.listdir(directory):
if filename.endswith(".py"):
module_name = filename[:-3]
module_path = os.path.join(directory, filename)
spec = importlib.util.spec_from_file_location(module_name, module_path)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)
# Exécuter automatiquement une fonction appelée "main" si elle existe
if hasattr(module, "main"):
try:
module.main() # Appelle la fonction "main" du module
if LOGGING_ENABLED:
app.logger.info(f"Executed 'main' in module: {module_name}")
except Exception as e:
if LOGGING_ENABLED:
app.logger.error(f"Error executing 'main' in module {module_name}: {e}")
else:
if LOGGING_ENABLED:
app.logger.info(f"Module loaded but no 'main' function found: {module_name}")
# Chargement de la configuration depuis config.yml
with open("config.yml", "r") as config_file:
config = yaml.safe_load(config_file)
# Configuration de l'application Flask
app = Flask(__name__)
app.secret_key = config["secret_key"]
# Configuration du gestionnaire de connexions
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "login"
# Répertoire de base pour les uploads
BASE_UPLOAD_FOLDER = "uploads"
os.makedirs(BASE_UPLOAD_FOLDER, exist_ok=True)
# Extensions de fichiers autorisées
ALLOWED_EXTENSIONS = {"txt", "pdf", "png", "jpg", "jpeg", "gif", "py", "java"}
# Gestion des utilisateurs et des clés API
users = config["users"] # Exemple : {"admin": "password"}
api_keys = config["api_keys"] # Exemple : {"user1": "api_key_12345"}
# Chargement de la configuration des logs
LOGGING_ENABLED = config.get("logging", {}).get("enabled", True)
LOG_FILE = config.get("logging", {}).get("log_file", "logs/app.log")
# Configuration des logs
if LOGGING_ENABLED:
os.makedirs(os.path.dirname(LOG_FILE), exist_ok=True)
log_handler = RotatingFileHandler(LOG_FILE, maxBytes=10 * 1024 * 1024, backupCount=5)
log_handler.setLevel(logging.INFO)
log_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
log_handler.setFormatter(log_formatter)
app.logger.addHandler(log_handler)
app.logger.setLevel(logging.INFO)
# Classe utilisateur pour Flask-Login
class User(UserMixin):
def __init__(self, id):
self.id = id
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'),
'favicon.ico', mimetype='image/vnd.microsoft.icon')
@app.route("/delete/<path:subpath>", methods=["POST"])
@login_required
def delete_item(subpath):
item_path = os.path.join(BASE_UPLOAD_FOLDER, subpath)
item_path = os.path.abspath(item_path)
if not item_path.startswith(os.path.abspath(BASE_UPLOAD_FOLDER)):
abort(404)
try:
if os.path.isfile(item_path):
os.remove(item_path)
elif os.path.isdir(item_path):
import shutil
shutil.rmtree(item_path)
if LOGGING_ENABLED:
app.logger.info(f"User {current_user.id} deleted: {item_path}")
return jsonify({"message": "Item deleted successfully"})
except Exception as e:
if LOGGING_ENABLED:
app.logger.error(f"Error deleting item: {e}")
return jsonify({"error": "Failed to delete item"}), 500
@login_manager.user_loader
def load_user(user_id):
if user_id in users:
return User(user_id)
return None
# Add a ShareLink model
class ShareLink:
def __init__(self, file_path, token):
self.file_path = file_path
self.token = token
self.created_at = datetime.now()
# Store share links in memory (you might want to use a database in production)
share_links = {}
socketio = SocketIO(app)
connected_users = set()
# Helper function to generate share token
def generate_share_token():
return str(uuid.uuid4())
# Vérification des clés API
def verify_api_key(api_key):
return api_key in api_keys.values()
# Vérifie si un fichier a une extension autorisée
def allowed_file(filename):
return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
# Routes de l'application
@app.route("/login", methods=["GET", "POST"])
def login():
if request.method == "POST":
username = request.form["username"]
password = request.form["password"]
if username in users and users[username] == password:
user = User(username)
login_user(user)
if LOGGING_ENABLED:
app.logger.info(f"Authentication success: {username}")
return redirect(url_for("browse"))
else:
if LOGGING_ENABLED:
app.logger.warning(f"Authentication failed: {username}")
return "Invalid credentials", 401
return render_template("login.html")
@socketio.on('connect')
def handle_connect():
connected_users.add(request.sid)
update_server_status()
@socketio.on('disconnect')
def handle_disconnect():
connected_users.discard(request.sid)
update_server_status()
def get_network_stats():
stats = psutil.net_io_counters()
return {
"bytes_sent": stats.bytes_sent,
"bytes_recv": stats.bytes_recv,
"packets_sent": stats.packets_sent,
"packets_recv": stats.packets_recv,
}
def get_system_stats():
return {
"cpu_percent": psutil.cpu_percent(interval=1),
"memory_usage": psutil.virtual_memory().percent,
"disk_usage": psutil.disk_usage(BASE_UPLOAD_FOLDER).percent,
}
def update_server_status():
server_status = {
"connected_users": len(connected_users),
"network_stats": get_network_stats(),
"system_stats": get_system_stats(),
}
app.logger.info(f"Server Status: {json.dumps(server_status, indent=4)}")
print(f"\n--- Server Status ---\n{json.dumps(server_status, indent=4)}\n")
@app.route("/create_folder/<path:subpath>", methods=["POST"])
@login_required
def create_folder(subpath):
# Log pour vérifier le chemin reçu
if LOGGING_ENABLED:
app.logger.info(f"Attempting to create folder in path: {subpath}")
# Calcul du chemin absolu du dossier
current_path = os.path.join(BASE_UPLOAD_FOLDER, subpath)
current_path = os.path.abspath(current_path)
# Vérification si le chemin est valide et sous le répertoire de base
if not current_path.startswith(os.path.abspath(BASE_UPLOAD_FOLDER)):
if LOGGING_ENABLED:
app.logger.error(f"Invalid path: {current_path}. Path is outside the allowed directory.")
abort(404)
# Récupérer le nom du nouveau dossier depuis le formulaire
folder_name = request.form.get('folder_name')
if not folder_name:
return jsonify({"error": "Folder name is required"}), 400
# Sécuriser le nom du dossier
folder_name = secure_filename(folder_name)
# Créer le chemin complet pour le nouveau dossier
new_folder_path = os.path.join(current_path, folder_name)
# Vérifier si le dossier existe déjà
if os.path.exists(new_folder_path):
return jsonify({"error": "Folder already exists"}), 400
try:
# Créer le dossier
os.makedirs(new_folder_path)
if LOGGING_ENABLED:
app.logger.info(f"Created new folder: {new_folder_path}")
return jsonify({"message": "Folder created successfully", "folder_name": folder_name})
except Exception as e:
if LOGGING_ENABLED:
app.logger.error(f"Error creating folder: {e}")
return jsonify({"error": "Failed to create folder"}), 500
@app.route("/create_folder/", methods=["POST"])
@login_required
def create_folder_root():
return create_folder("")
@app.route("/logout")
@login_required
def logout():
logout_user()
return redirect(url_for("login"))
@app.route("/")
@login_required
def home():
return redirect(url_for("browse"))
@app.route("/browse/", defaults={"subpath": ""})
@app.route("/browse/<path:subpath>")
@login_required
def browse(subpath):
current_path = os.path.join(BASE_UPLOAD_FOLDER, subpath)
current_path = os.path.abspath(current_path)
if not current_path.startswith(os.path.abspath(BASE_UPLOAD_FOLDER)):
abort(404)
if os.path.isdir(current_path):
if LOGGING_ENABLED:
app.logger.info(f"User {current_user.id} navigated to directory: {current_path}")
files = os.listdir(current_path)
return render_template("index.html", files=files, current_path=subpath)
elif os.path.isfile(current_path):
if "download" in request.args:
if LOGGING_ENABLED:
app.logger.info(f"User {current_user.id} downloaded file: {current_path}")
return send_from_directory(os.path.dirname(current_path),
os.path.basename(current_path),
as_attachment=True)
else:
# Serve the file for viewing
return send_from_directory(os.path.dirname(current_path),
os.path.basename(current_path))
abort(404)
@app.route("/share/<path:subpath>", methods=["POST"])
@login_required
def share_file(subpath):
file_path = os.path.join(BASE_UPLOAD_FOLDER, subpath)
if not os.path.isfile(file_path):
return jsonify({"error": "File not found"}), 404
# Generate a unique token for this share
token = generate_share_token()
share_links[token] = ShareLink(file_path, token)
share_url = url_for('shared_file', token=token, _external=True)
return jsonify({
"message": "Share link created successfully",
"share_url": share_url
})
@app.route("/shared/<token>")
def shared_file(token):
if token not in share_links:
abort(404)
share_link = share_links[token]
filepath = share_link.file_path
filename = os.path.basename(filepath)
# Vérification si un fichier est demandé directement pour téléchargement
if "download" in request.args:
return send_from_directory(
os.path.dirname(filepath),
filename,
as_attachment=True
)
# Lien direct pour voir le fichier (si c'est un type compatible)
if "view" in request.args:
return send_from_directory(
os.path.dirname(filepath),
filename,
as_attachment=False
)
file_url = url_for('shared_file', token=token)
return render_template(
"shared.html",
filename=filename,
file_url=file_url,
download_url=f"{file_url}?download=true",
view_url=f"{file_url}?view=true"
)
@app.route("/upload/<path:subpath>", methods=["POST"])
@login_required
def upload(subpath):
current_path = os.path.join(BASE_UPLOAD_FOLDER, subpath)
current_path = os.path.abspath(current_path)
if not current_path.startswith(os.path.abspath(BASE_UPLOAD_FOLDER)):
abort(404)
if "file" not in request.files:
return "No file part", 400
file = request.files["file"]
if file.filename == "":
return "No selected file", 400
filename = secure_filename(file.filename)
save_path = os.path.join(current_path, filename)
file.save(save_path)
if LOGGING_ENABLED:
app.logger.info(f"User {current_user.id} uploaded file: {save_path}")
return redirect(url_for("browse", subpath=subpath))
@app.route("/create/<path:subpath>", methods=["POST"])
@login_required
def create_file(subpath):
current_path = os.path.join(BASE_UPLOAD_FOLDER, subpath)
if not os.path.exists(current_path) or not os.path.isdir(current_path):
abort(404)
filename = request.form.get("filename")
content = request.form.get("content")
if not filename:
return "Filename is required", 400
filepath = os.path.join(current_path, secure_filename(filename))
with open(filepath, "w") as f:
f.write(content)
return jsonify({"message": "File created successfully", "filename": filename})
@app.route("/api/upload", methods=["POST"])
def api_upload():
api_key = request.headers.get("X-API-KEY")
if not verify_api_key(api_key):
return jsonify({"error": "Invalid API key"}), 403
subpath = request.args.get("path", "")
current_path = os.path.join(BASE_UPLOAD_FOLDER, subpath)
if not os.path.exists(current_path) or not os.path.isdir(current_path):
return jsonify({"error": "Invalid path"}), 404
if "file" not in request.files:
return jsonify({"error": "No file part"}), 400
file = request.files["file"]
if file.filename == "":
return jsonify({"error": "No selected file"}), 400
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
filepath = os.path.join(current_path, filename)
file.save(filepath)
if LOGGING_ENABLED:
app.logger.info(f"File uploaded via API: {filepath}")
return jsonify({"message": "File uploaded successfully", "filename": filename})
return jsonify({"error": "Invalid file type"}), 400
@app.route("/api/download/<path:subpath>")
def api_download(subpath):
api_key = request.headers.get("X-API-KEY")
if not verify_api_key(api_key):
return jsonify({"error": "Invalid API key"}), 403
filepath = os.path.join(BASE_UPLOAD_FOLDER, subpath)
if os.path.exists(filepath) and os.path.isfile(filepath):
if LOGGING_ENABLED:
app.logger.info(f"File downloaded via API: {filepath}")
return send_from_directory(os.path.dirname(filepath), os.path.basename(filepath), as_attachment=True)
return jsonify({"error": "File not found"}), 404
@app.route("/upload/", methods=["POST"])
@login_required
def upload_default():
return upload("")
if __name__ == "__main__":
load_modules_from_directory("modules")
print("Starting server...")
app.logger.info("Server is starting...")
# Afficher les statistiques en temps réel
if config.get("debug", False):
update_server_status()
# Démarrer le serveur avec socketio
socketio.run(app, debug=config.get("debug", False), host="0.0.0.0", port=config.get("port", 5000))