Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from flask import Flask, render_template_string, request, redirect, url_for, session, flash,
|
| 2 |
from flask_caching import Cache
|
| 3 |
import json
|
| 4 |
import os
|
|
@@ -8,8 +8,6 @@ import time
|
|
| 8 |
from datetime import datetime
|
| 9 |
from huggingface_hub import HfApi, hf_hub_download
|
| 10 |
from werkzeug.utils import secure_filename
|
| 11 |
-
import random
|
| 12 |
-
import string
|
| 13 |
import requests
|
| 14 |
from io import BytesIO
|
| 15 |
|
|
@@ -19,7 +17,7 @@ DATA_FILE = 'cloud_data.json'
|
|
| 19 |
REPO_ID = "Eluza133/Z1e1u"
|
| 20 |
HF_TOKEN_WRITE = os.getenv("HF_TOKEN")
|
| 21 |
HF_TOKEN_READ = os.getenv("HF_TOKEN_READ") or HF_TOKEN_WRITE
|
| 22 |
-
|
| 23 |
|
| 24 |
cache = Cache(app, config={'CACHE_TYPE': 'simple'})
|
| 25 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -90,9 +88,6 @@ def periodic_backup():
|
|
| 90 |
upload_db_to_hf()
|
| 91 |
time.sleep(1800)
|
| 92 |
|
| 93 |
-
def generate_token():
|
| 94 |
-
return ''.join(random.choices(string.ascii_letters + string.digits, k=13))
|
| 95 |
-
|
| 96 |
def get_file_type(filename):
|
| 97 |
video_extensions = ('.mp4', '.mov', '.avi')
|
| 98 |
image_extensions = ('.jpg', '.jpeg', '.png', '.gif')
|
|
@@ -280,22 +275,35 @@ body.dark .file-item {
|
|
| 280 |
}
|
| 281 |
'''
|
| 282 |
|
| 283 |
-
@app.route('/
|
| 284 |
def register():
|
| 285 |
if request.method == 'POST':
|
|
|
|
| 286 |
password = request.form.get('password')
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
}
|
| 294 |
-
save_data(data)
|
| 295 |
-
flash(f'Ваш токен: {token}. Сохраните его!')
|
| 296 |
return redirect(url_for('register'))
|
| 297 |
-
|
| 298 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
|
| 300 |
html = '''
|
| 301 |
<!DOCTYPE html>
|
|
@@ -318,9 +326,12 @@ def register():
|
|
| 318 |
{% endif %}
|
| 319 |
{% endwith %}
|
| 320 |
<form method="POST">
|
|
|
|
| 321 |
<input type="password" name="password" placeholder="Введите пароль" required>
|
|
|
|
| 322 |
<button type="submit" class="btn">Зарегистрироваться</button>
|
| 323 |
</form>
|
|
|
|
| 324 |
</div>
|
| 325 |
</body>
|
| 326 |
</html>
|
|
@@ -330,13 +341,15 @@ def register():
|
|
| 330 |
@app.route('/', methods=['GET', 'POST'])
|
| 331 |
def login():
|
| 332 |
if request.method == 'POST':
|
| 333 |
-
|
|
|
|
| 334 |
data = load_data()
|
| 335 |
-
|
| 336 |
-
|
|
|
|
| 337 |
return redirect(url_for('dashboard'))
|
| 338 |
else:
|
| 339 |
-
flash('Неверный
|
| 340 |
|
| 341 |
html = '''
|
| 342 |
<!DOCTYPE html>
|
|
@@ -359,10 +372,11 @@ def login():
|
|
| 359 |
{% endif %}
|
| 360 |
{% endwith %}
|
| 361 |
<form method="POST">
|
| 362 |
-
<input type="text" name="
|
|
|
|
| 363 |
<button type="submit" class="btn">Войти</button>
|
| 364 |
</form>
|
| 365 |
-
<p style="margin-top: 20px;">Нет
|
| 366 |
</div>
|
| 367 |
</body>
|
| 368 |
</html>
|
|
@@ -371,18 +385,19 @@ def login():
|
|
| 371 |
|
| 372 |
@app.route('/dashboard', methods=['GET', 'POST'])
|
| 373 |
def dashboard():
|
| 374 |
-
if '
|
| 375 |
flash('Пожалуйста, войдите!')
|
| 376 |
return redirect(url_for('login'))
|
| 377 |
|
| 378 |
-
|
| 379 |
data = load_data()
|
| 380 |
-
if
|
| 381 |
-
session.pop('
|
| 382 |
-
flash('
|
| 383 |
return redirect(url_for('login'))
|
| 384 |
|
| 385 |
-
|
|
|
|
| 386 |
|
| 387 |
if request.method == 'POST':
|
| 388 |
files = request.files.getlist('files')
|
|
@@ -403,14 +418,14 @@ def dashboard():
|
|
| 403 |
temp_files.append((temp_path, filename))
|
| 404 |
|
| 405 |
for temp_path, filename in temp_files:
|
| 406 |
-
file_path = f"cloud_files/{
|
| 407 |
api.upload_file(
|
| 408 |
path_or_fileobj=temp_path,
|
| 409 |
path_in_repo=file_path,
|
| 410 |
repo_id=REPO_ID,
|
| 411 |
repo_type="dataset",
|
| 412 |
token=HF_TOKEN_WRITE,
|
| 413 |
-
commit_message=f"Загружен файл для {
|
| 414 |
)
|
| 415 |
|
| 416 |
file_info = {
|
|
@@ -419,7 +434,7 @@ def dashboard():
|
|
| 419 |
'type': get_file_type(filename),
|
| 420 |
'upload_date': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
| 421 |
}
|
| 422 |
-
data['users'][
|
| 423 |
|
| 424 |
if os.path.exists(temp_path):
|
| 425 |
os.remove(temp_path)
|
|
@@ -442,7 +457,7 @@ def dashboard():
|
|
| 442 |
<body>
|
| 443 |
<div class="container">
|
| 444 |
<h1>Zues Cloud Dashboard</h1>
|
| 445 |
-
<p
|
| 446 |
{% with messages = get_flashed_messages() %}
|
| 447 |
{% if messages %}
|
| 448 |
{% for message in messages %}
|
|
@@ -552,23 +567,22 @@ def dashboard():
|
|
| 552 |
</body>
|
| 553 |
</html>
|
| 554 |
'''
|
| 555 |
-
return render_template_string(html,
|
| 556 |
|
| 557 |
@app.route('/download/<path:file_path>/<filename>')
|
| 558 |
def download_file(file_path, filename):
|
| 559 |
-
if '
|
| 560 |
flash('Пожалуйста, войдите!')
|
| 561 |
return redirect(url_for('login'))
|
| 562 |
|
| 563 |
-
|
| 564 |
data = load_data()
|
| 565 |
-
if
|
| 566 |
-
session.pop('
|
| 567 |
-
flash('
|
| 568 |
return redirect(url_for('login'))
|
| 569 |
|
| 570 |
-
|
| 571 |
-
user_files = data['users'][token]['files']
|
| 572 |
if not any(file['path'] == file_path for file in user_files):
|
| 573 |
flash('У вас нет доступа к этому файлу!')
|
| 574 |
return redirect(url_for('dashboard'))
|
|
@@ -590,7 +604,7 @@ def download_file(file_path, filename):
|
|
| 590 |
|
| 591 |
@app.route('/logout')
|
| 592 |
def logout():
|
| 593 |
-
session.pop('
|
| 594 |
return redirect(url_for('login'))
|
| 595 |
|
| 596 |
if __name__ == '__main__':
|
|
|
|
| 1 |
+
from flask import Flask, render_template_string, request, redirect, url_for, session, flash, send_file
|
| 2 |
from flask_caching import Cache
|
| 3 |
import json
|
| 4 |
import os
|
|
|
|
| 8 |
from datetime import datetime
|
| 9 |
from huggingface_hub import HfApi, hf_hub_download
|
| 10 |
from werkzeug.utils import secure_filename
|
|
|
|
|
|
|
| 11 |
import requests
|
| 12 |
from io import BytesIO
|
| 13 |
|
|
|
|
| 17 |
REPO_ID = "Eluza133/Z1e1u"
|
| 18 |
HF_TOKEN_WRITE = os.getenv("HF_TOKEN")
|
| 19 |
HF_TOKEN_READ = os.getenv("HF_TOKEN_READ") or HF_TOKEN_WRITE
|
| 20 |
+
REGISTRATION_CODE = "morshenalphacl"
|
| 21 |
|
| 22 |
cache = Cache(app, config={'CACHE_TYPE': 'simple'})
|
| 23 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 88 |
upload_db_to_hf()
|
| 89 |
time.sleep(1800)
|
| 90 |
|
|
|
|
|
|
|
|
|
|
| 91 |
def get_file_type(filename):
|
| 92 |
video_extensions = ('.mp4', '.mov', '.avi')
|
| 93 |
image_extensions = ('.jpg', '.jpeg', '.png', '.gif')
|
|
|
|
| 275 |
}
|
| 276 |
'''
|
| 277 |
|
| 278 |
+
@app.route('/register', methods=['GET', 'POST'])
|
| 279 |
def register():
|
| 280 |
if request.method == 'POST':
|
| 281 |
+
username = request.form.get('username')
|
| 282 |
password = request.form.get('password')
|
| 283 |
+
code = request.form.get('code')
|
| 284 |
+
|
| 285 |
+
data = load_data()
|
| 286 |
+
|
| 287 |
+
if username in data['users']:
|
| 288 |
+
flash('Пользователь с таким логином уже существует!')
|
|
|
|
|
|
|
|
|
|
| 289 |
return redirect(url_for('register'))
|
| 290 |
+
|
| 291 |
+
if code != REGISTRATION_CODE:
|
| 292 |
+
flash('Неверный регистрационный код!')
|
| 293 |
+
return redirect(url_for('register'))
|
| 294 |
+
|
| 295 |
+
if not username or not password:
|
| 296 |
+
flash('Логин и пароль обязательны!')
|
| 297 |
+
return redirect(url_for('register'))
|
| 298 |
+
|
| 299 |
+
data['users'][username] = {
|
| 300 |
+
'password': password,
|
| 301 |
+
'created_at': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
|
| 302 |
+
'files': []
|
| 303 |
+
}
|
| 304 |
+
save_data(data)
|
| 305 |
+
flash('Регистрация успешна! Войдите в систему.')
|
| 306 |
+
return redirect(url_for('login'))
|
| 307 |
|
| 308 |
html = '''
|
| 309 |
<!DOCTYPE html>
|
|
|
|
| 326 |
{% endif %}
|
| 327 |
{% endwith %}
|
| 328 |
<form method="POST">
|
| 329 |
+
<input type="text" name="username" placeholder="Введите логин" required>
|
| 330 |
<input type="password" name="password" placeholder="Введите пароль" required>
|
| 331 |
+
<input type="text" name="code" placeholder="Введите регистрационный код" required>
|
| 332 |
<button type="submit" class="btn">Зарегистрироваться</button>
|
| 333 |
</form>
|
| 334 |
+
<p style="margin-top: 20px;">Уже есть аккаунт? <a href="{{ url_for('login') }}">Войдите</a></p>
|
| 335 |
</div>
|
| 336 |
</body>
|
| 337 |
</html>
|
|
|
|
| 341 |
@app.route('/', methods=['GET', 'POST'])
|
| 342 |
def login():
|
| 343 |
if request.method == 'POST':
|
| 344 |
+
username = request.form.get('username')
|
| 345 |
+
password = request.form.get('password')
|
| 346 |
data = load_data()
|
| 347 |
+
|
| 348 |
+
if username in data['users'] and data['users'][username]['password'] == password:
|
| 349 |
+
session['username'] = username
|
| 350 |
return redirect(url_for('dashboard'))
|
| 351 |
else:
|
| 352 |
+
flash('Неверный логин или пароль!')
|
| 353 |
|
| 354 |
html = '''
|
| 355 |
<!DOCTYPE html>
|
|
|
|
| 372 |
{% endif %}
|
| 373 |
{% endwith %}
|
| 374 |
<form method="POST">
|
| 375 |
+
<input type="text" name="username" placeholder="Введите логин" required>
|
| 376 |
+
<input type="password" name="password" placeholder="Введите пароль" required>
|
| 377 |
<button type="submit" class="btn">Войти</button>
|
| 378 |
</form>
|
| 379 |
+
<p style="margin-top: 20px;">Нет аккаунта? <a href="{{ url_for('register') }}">Зарегистрируйтесь</a></p>
|
| 380 |
</div>
|
| 381 |
</body>
|
| 382 |
</html>
|
|
|
|
| 385 |
|
| 386 |
@app.route('/dashboard', methods=['GET', 'POST'])
|
| 387 |
def dashboard():
|
| 388 |
+
if 'username' not in session:
|
| 389 |
flash('Пожалуйста, войдите!')
|
| 390 |
return redirect(url_for('login'))
|
| 391 |
|
| 392 |
+
username = session['username']
|
| 393 |
data = load_data()
|
| 394 |
+
if username not in data['users']:
|
| 395 |
+
session.pop('username', None)
|
| 396 |
+
flash('Пользователь не найден!')
|
| 397 |
return redirect(url_for('login'))
|
| 398 |
|
| 399 |
+
# Сортировка файлов по убыванию даты загрузки
|
| 400 |
+
user_files = sorted(data['users'][username]['files'], key=lambda x: x['upload_date'], reverse=True)
|
| 401 |
|
| 402 |
if request.method == 'POST':
|
| 403 |
files = request.files.getlist('files')
|
|
|
|
| 418 |
temp_files.append((temp_path, filename))
|
| 419 |
|
| 420 |
for temp_path, filename in temp_files:
|
| 421 |
+
file_path = f"cloud_files/{username}/{filename}"
|
| 422 |
api.upload_file(
|
| 423 |
path_or_fileobj=temp_path,
|
| 424 |
path_in_repo=file_path,
|
| 425 |
repo_id=REPO_ID,
|
| 426 |
repo_type="dataset",
|
| 427 |
token=HF_TOKEN_WRITE,
|
| 428 |
+
commit_message=f"Загружен файл для {username}"
|
| 429 |
)
|
| 430 |
|
| 431 |
file_info = {
|
|
|
|
| 434 |
'type': get_file_type(filename),
|
| 435 |
'upload_date': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
| 436 |
}
|
| 437 |
+
data['users'][username]['files'].append(file_info)
|
| 438 |
|
| 439 |
if os.path.exists(temp_path):
|
| 440 |
os.remove(temp_path)
|
|
|
|
| 457 |
<body>
|
| 458 |
<div class="container">
|
| 459 |
<h1>Zues Cloud Dashboard</h1>
|
| 460 |
+
<p>Пользователь: {{ username }}</p>
|
| 461 |
{% with messages = get_flashed_messages() %}
|
| 462 |
{% if messages %}
|
| 463 |
{% for message in messages %}
|
|
|
|
| 567 |
</body>
|
| 568 |
</html>
|
| 569 |
'''
|
| 570 |
+
return render_template_string(html, username=username, user_files=user_files, repo_id=REPO_ID)
|
| 571 |
|
| 572 |
@app.route('/download/<path:file_path>/<filename>')
|
| 573 |
def download_file(file_path, filename):
|
| 574 |
+
if 'username' not in session:
|
| 575 |
flash('Пожалуйста, войдите!')
|
| 576 |
return redirect(url_for('login'))
|
| 577 |
|
| 578 |
+
username = session['username']
|
| 579 |
data = load_data()
|
| 580 |
+
if username not in data['users']:
|
| 581 |
+
session.pop('username', None)
|
| 582 |
+
flash('Пользователь не найден!')
|
| 583 |
return redirect(url_for('login'))
|
| 584 |
|
| 585 |
+
user_files = data['users'][username]['files']
|
|
|
|
| 586 |
if not any(file['path'] == file_path for file in user_files):
|
| 587 |
flash('У вас нет доступа к этому файлу!')
|
| 588 |
return redirect(url_for('dashboard'))
|
|
|
|
| 604 |
|
| 605 |
@app.route('/logout')
|
| 606 |
def logout():
|
| 607 |
+
session.pop('username', None)
|
| 608 |
return redirect(url_for('login'))
|
| 609 |
|
| 610 |
if __name__ == '__main__':
|