* Добавлено кликабельное название вверху

* Добавлено логирование
* Проверка на авторизацию сразу в /login
This commit is contained in:
justuser-31 2026-01-31 15:07:00 +03:00
parent 1805846241
commit d7e0efee42

62
main.py
View File

@ -23,6 +23,11 @@ STORAGE_ROOT = ""
# Global DB connection
DB_CONN = None
# Logging utility
def log(message):
if CONFIG.get("logging", {}).get("enabled", True):
print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] {message}")
# Load config
def load_config():
global CONFIG, STORAGE_ROOT
@ -156,7 +161,7 @@ class FileServerHandler(BaseHTTPRequestHandler):
def log_message(self, format, *args):
# Override to avoid logging to stderr
pass
log(format % args)
def send_html(self, content, status=200):
self.send_response(status)
@ -185,25 +190,28 @@ class FileServerHandler(BaseHTTPRequestHandler):
<!DOCTYPE html>
<html>
<head>
<title>{CONFIG['ui']['title']}</title>
<title>SimpliestFS</title>
<style>
body {{ font-family: sans-serif; margin: 40px; }}
.btn {{ display: inline-block; padding: 10px 20px; margin: 10px; background: #eee; border: 1px solid #ccc; text-decoration: none; color: #333; }}
.btn:hover {{ background: #ddd; }}
.disclaimer {{ margin-top: 30px; font-size: 0.9em; color: #666; }}
h1 a {{ text-decoration: none; color: inherit; }}
</style>
</head>
<body>
<h1>{CONFIG['ui']['title']}</h1>
<p>{CONFIG['ui']['disclaimer']}</p>
<h1><a href="/">SimpliestFS</a></h1>
<p>EN: {CONFIG['ui']['disclaimer']}</p>
<p>RU: {CONFIG['ui']['disclaimer_ru']}</p>
<p>Contact: <a href="mailto:{CONFIG['ui']['contact_email']}">{CONFIG['ui']['contact_email']}</a></p>
<p>Contact (prefered): <a href="https://t.me/justuser_31">@justuser_31</a></p>
<p>
<a class="btn" href="/register">Register</a>
<a class="btn" href="/login">Login</a>
<a class="btn" href="/explore">Explore Files</a>
</p>
<div class="disclaimer">
Note: All files are stored locally. No encryption. No external databases.
Note: NO ENCRYPTION, no external databases. As simple as possible.
</div>
</body>
</html>
@ -215,14 +223,16 @@ class FileServerHandler(BaseHTTPRequestHandler):
<!DOCTYPE html>
<html>
<head>
<title>Register - {CONFIG['ui']['title']}</title>
<title>Register - SimpliestFS</title>
<style>
body {{ font-family: sans-serif; margin: 40px; }}
.back-btn {{ display: inline-block; padding: 5px 10px; background: #eee; border: 1px solid #ccc; text-decoration: none; color: #333; }}
h1 a {{ text-decoration: none; color: inherit; }}
</style>
</head>
<body>
<h1>Register</h1>
<h1><a href="/">SimpliestFS</a></h1>
<h2>Register</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p><a class="back-btn" href="/">Back to Home</a></p>
</body>
@ -260,6 +270,11 @@ class FileServerHandler(BaseHTTPRequestHandler):
self.send_html(content, 401)
return
# If already authorized
username = self.get_session_user()
if username:
self.send_redirect("/files")
return
# GET /login
content = self.render_login_form()
self.send_html(content)
@ -269,17 +284,19 @@ class FileServerHandler(BaseHTTPRequestHandler):
<!DOCTYPE html>
<html>
<head>
<title>Login - {CONFIG['ui']['title']}</title>
<title>Login - SimpliestFS</title>
<style>
body {{ font-family: sans-serif; margin: 40px; }}
form {{ max-width: 300px; }}
input, button {{ width: 100%; padding: 8px; margin: 5px 0; }}
.error {{ color: red; }}
.back-btn {{ display: inline-block; padding: 5px 10px; background: #eee; border: 1px solid #ccc; text-decoration: none; color: #333; }}
h1 a {{ text-decoration: none; color: inherit; }}
</style>
</head>
<body>
<h1>Login</h1>
<h1><a href="/">SimpliestFS</a></h1>
<h2>Login</h2>
{f'<p class="error">{error}</p>' if error else ''}
<form method="POST">
<input type="text" name="username" placeholder="Username" required>
@ -327,16 +344,18 @@ class FileServerHandler(BaseHTTPRequestHandler):
<!DOCTYPE html>
<html>
<head>
<title>My Files - {CONFIG['ui']['title']}</title>
<title>My Files - SimpliestFS</title>
<style>
body {{ font-family: sans-serif; margin: 40px; }}
.upload-btn {{ display: inline-block; padding: 5px 10px; background: #eee; border: 1px solid #ccc; text-decoration: none; color: #333; }}
.back-btn {{ display: inline-block; padding: 5px 10px; background: #eee; border: 1px solid #ccc; text-decoration: none; color: #333; }}
.quota-info {{ margin: 20px 0; font-size: 0.9em; color: #666; }}
h1 a {{ text-decoration: none; color: inherit; }}
</style>
</head>
<body>
<h1>My Files</h1>
<h1><a href="/">SimpliestFS</a></h1>
<h2>My Files</h2>
<div class="quota-info">
Quota: {CONFIG['storage']['default_quota_mb']} MB | Used: {used_mb:.2f} MB
</div>
@ -346,7 +365,10 @@ class FileServerHandler(BaseHTTPRequestHandler):
</form>
<hr>
{files_html if files_html else "<p>No files yet.</p>"}
<p><a class="back-btn" href="/logout">Logout</a></p>
<p>
<a class="back-btn" href="/logout">Logout</a>
<a class="back-btn" href="/">Back to Home</a>
</p>
</body>
</html>
"""
@ -495,14 +517,16 @@ class FileServerHandler(BaseHTTPRequestHandler):
<!DOCTYPE html>
<html>
<head>
<title>Explore Files - {CONFIG['ui']['title']}</title>
<title>Explore Files - SimpliestFS</title>
<style>
body {{ font-family: sans-serif; margin: 40px; }}
.back-btn {{ display: inline-block; padding: 5px 10px; background: #eee; border: 1px solid #ccc; text-decoration: none; color: #333; }}
h1 a {{ text-decoration: none; color: inherit; }}
</style>
</head>
<body>
<h1>Explore Files</h1>
<h1><a href="/">SimpliestFS</a></h1>
<h2>Explore Files</h2>
<p>All users:</p>
{users_html if users_html else "<p>No users yet.</p>"}
<p><a class="back-btn" href="/">Back to Home</a></p>
@ -536,14 +560,16 @@ class FileServerHandler(BaseHTTPRequestHandler):
<!DOCTYPE html>
<html>
<head>
<title>{username}'s Files - {CONFIG['ui']['title']}</title>
<title>{username}'s Files - SimpliestFS</title>
<style>
body {{ font-family: sans-serif; margin: 40px; }}
.back-btn {{ display: inline-block; padding: 5px 10px; background: #eee; border: 1px solid #ccc; text-decoration: none; color: #333; }}
h1 a {{ text-decoration: none; color: inherit; }}
</style>
</head>
<body>
<h1>{username}'s Files</h1>
<h1><a href="/">SimpliestFS</a></h1>
<h2>{username}'s Files</h2>
{files_html if files_html else "<p>No files.</p>"}
<p><a class="back-btn" href="/explore">Back to All Users</a></p>
</body>
@ -724,6 +750,9 @@ def main():
load_config()
init_db()
# Log startup
log("Starting SimpliestFS server...")
# Start session cleanup thread
cleanup_thread = threading.Thread(target=session_cleanup, daemon=True)
cleanup_thread.start()
@ -731,6 +760,7 @@ def main():
# Start server
server_address = (CONFIG["server"]["host"], CONFIG["server"]["port"])
httpd = HTTPServer(server_address, FileServerHandler)
log(f"SimpliestFS server running on {CONFIG['server']['host']}:{CONFIG['server']['port']}")
httpd.serve_forever()
if __name__ == "__main__":