Update app.py
Browse files
app.py
CHANGED
|
@@ -14,7 +14,7 @@ import string
|
|
| 14 |
app = Flask(__name__)
|
| 15 |
app.secret_key = os.getenv("FLASK_SECRET_KEY", "supersecretkey")
|
| 16 |
DATA_FILE = 'cloud_data.json'
|
| 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 |
ADMIN_PASSWORD = "87132morflot"
|
|
@@ -86,13 +86,11 @@ def download_db_from_hf():
|
|
| 86 |
def periodic_backup():
|
| 87 |
while True:
|
| 88 |
upload_db_to_hf()
|
| 89 |
-
time.sleep(1800)
|
| 90 |
|
| 91 |
-
# Генерация 13-значного токена
|
| 92 |
def generate_token():
|
| 93 |
return ''.join(random.choices(string.ascii_letters + string.digits, k=13))
|
| 94 |
|
| 95 |
-
# Определение типа файла для предпросмотра
|
| 96 |
def get_file_type(filename):
|
| 97 |
video_extensions = ('.mp4', '.mov', '.avi')
|
| 98 |
image_extensions = ('.jpg', '.jpeg', '.png', '.gif')
|
|
@@ -102,7 +100,6 @@ def get_file_type(filename):
|
|
| 102 |
return 'image'
|
| 103 |
return 'other'
|
| 104 |
|
| 105 |
-
# Базовый стиль CSS
|
| 106 |
BASE_STYLE = '''
|
| 107 |
:root {
|
| 108 |
--primary: #ff4d6d;
|
|
@@ -203,6 +200,16 @@ input:focus, textarea:focus {
|
|
| 203 |
gap: 20px;
|
| 204 |
margin-top: 20px;
|
| 205 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
.file-item {
|
| 207 |
background: var(--card-bg);
|
| 208 |
padding: 15px;
|
|
@@ -255,9 +262,22 @@ body.dark .file-item {
|
|
| 255 |
border-radius: 20px;
|
| 256 |
box-shadow: var(--shadow);
|
| 257 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
'''
|
| 259 |
|
| 260 |
-
# Регистрация через /admhosto
|
| 261 |
@app.route('/admhosto', methods=['GET', 'POST'])
|
| 262 |
def register():
|
| 263 |
if request.method == 'POST':
|
|
@@ -305,7 +325,6 @@ def register():
|
|
| 305 |
'''
|
| 306 |
return render_template_string(html)
|
| 307 |
|
| 308 |
-
# Главная страница с вводом токена
|
| 309 |
@app.route('/', methods=['GET', 'POST'])
|
| 310 |
def login():
|
| 311 |
if request.method == 'POST':
|
|
@@ -348,7 +367,6 @@ def login():
|
|
| 348 |
'''
|
| 349 |
return render_template_string(html)
|
| 350 |
|
| 351 |
-
# Личный dashboard
|
| 352 |
@app.route('/dashboard', methods=['GET', 'POST'])
|
| 353 |
def dashboard():
|
| 354 |
if 'token' not in session:
|
|
@@ -365,35 +383,47 @@ def dashboard():
|
|
| 365 |
user_files = data['users'][token]['files']
|
| 366 |
|
| 367 |
if request.method == 'POST':
|
| 368 |
-
|
| 369 |
-
if
|
| 370 |
-
|
| 371 |
-
|
|
|
|
|
|
|
| 372 |
os.makedirs('uploads', exist_ok=True)
|
| 373 |
-
file.save(temp_path)
|
| 374 |
-
|
| 375 |
api = HfApi()
|
| 376 |
-
|
| 377 |
-
api.upload_file(
|
| 378 |
-
path_or_fileobj=temp_path,
|
| 379 |
-
path_in_repo=file_path,
|
| 380 |
-
repo_id=REPO_ID,
|
| 381 |
-
repo_type="dataset",
|
| 382 |
-
token=HF_TOKEN_WRITE,
|
| 383 |
-
commit_message=f"Загружен файл для {token}"
|
| 384 |
-
)
|
| 385 |
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
data['users'][token]['files'].append(file_info)
|
| 393 |
-
save_data(data)
|
| 394 |
|
| 395 |
-
|
| 396 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 397 |
|
| 398 |
return redirect(url_for('dashboard'))
|
| 399 |
|
|
@@ -411,10 +441,20 @@ def dashboard():
|
|
| 411 |
<div class="container">
|
| 412 |
<h1>Zues Cloud Dashboard</h1>
|
| 413 |
<p>Токен: {{ token }}</p>
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 417 |
</form>
|
|
|
|
|
|
|
|
|
|
| 418 |
<h2 style="margin-top: 30px;">Ваши файлы</h2>
|
| 419 |
<div class="file-grid">
|
| 420 |
{% for file in user_files %}
|
|
@@ -459,13 +499,59 @@ def dashboard():
|
|
| 459 |
modal.innerHTML = '<div id="modalContent"></div>';
|
| 460 |
}
|
| 461 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 462 |
</script>
|
| 463 |
</body>
|
| 464 |
</html>
|
| 465 |
'''
|
| 466 |
return render_template_string(html, token=token, user_files=user_files, repo_id=REPO_ID)
|
| 467 |
|
| 468 |
-
# Выход
|
| 469 |
@app.route('/logout')
|
| 470 |
def logout():
|
| 471 |
session.pop('token', None)
|
|
|
|
| 14 |
app = Flask(__name__)
|
| 15 |
app.secret_key = os.getenv("FLASK_SECRET_KEY", "supersecretkey")
|
| 16 |
DATA_FILE = 'cloud_data.json'
|
| 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 |
ADMIN_PASSWORD = "87132morflot"
|
|
|
|
| 86 |
def periodic_backup():
|
| 87 |
while True:
|
| 88 |
upload_db_to_hf()
|
| 89 |
+
time.sleep(1800)
|
| 90 |
|
|
|
|
| 91 |
def generate_token():
|
| 92 |
return ''.join(random.choices(string.ascii_letters + string.digits, k=13))
|
| 93 |
|
|
|
|
| 94 |
def get_file_type(filename):
|
| 95 |
video_extensions = ('.mp4', '.mov', '.avi')
|
| 96 |
image_extensions = ('.jpg', '.jpeg', '.png', '.gif')
|
|
|
|
| 100 |
return 'image'
|
| 101 |
return 'other'
|
| 102 |
|
|
|
|
| 103 |
BASE_STYLE = '''
|
| 104 |
:root {
|
| 105 |
--primary: #ff4d6d;
|
|
|
|
| 200 |
gap: 20px;
|
| 201 |
margin-top: 20px;
|
| 202 |
}
|
| 203 |
+
@media (max-width: 768px) {
|
| 204 |
+
.file-grid {
|
| 205 |
+
grid-template-columns: repeat(2, 1fr);
|
| 206 |
+
}
|
| 207 |
+
}
|
| 208 |
+
@media (max-width: 480px) {
|
| 209 |
+
.file-grid {
|
| 210 |
+
grid-template-columns: 1fr;
|
| 211 |
+
}
|
| 212 |
+
}
|
| 213 |
.file-item {
|
| 214 |
background: var(--card-bg);
|
| 215 |
padding: 15px;
|
|
|
|
| 262 |
border-radius: 20px;
|
| 263 |
box-shadow: var(--shadow);
|
| 264 |
}
|
| 265 |
+
#progress-container {
|
| 266 |
+
width: 100%;
|
| 267 |
+
background: var(--glass-bg);
|
| 268 |
+
border-radius: 10px;
|
| 269 |
+
margin: 15px 0;
|
| 270 |
+
display: none;
|
| 271 |
+
}
|
| 272 |
+
#progress-bar {
|
| 273 |
+
width: 0%;
|
| 274 |
+
height: 20px;
|
| 275 |
+
background: var(--primary);
|
| 276 |
+
border-radius: 10px;
|
| 277 |
+
transition: width 0.3s ease;
|
| 278 |
+
}
|
| 279 |
'''
|
| 280 |
|
|
|
|
| 281 |
@app.route('/admhosto', methods=['GET', 'POST'])
|
| 282 |
def register():
|
| 283 |
if request.method == 'POST':
|
|
|
|
| 325 |
'''
|
| 326 |
return render_template_string(html)
|
| 327 |
|
|
|
|
| 328 |
@app.route('/', methods=['GET', 'POST'])
|
| 329 |
def login():
|
| 330 |
if request.method == 'POST':
|
|
|
|
| 367 |
'''
|
| 368 |
return render_template_string(html)
|
| 369 |
|
|
|
|
| 370 |
@app.route('/dashboard', methods=['GET', 'POST'])
|
| 371 |
def dashboard():
|
| 372 |
if 'token' not in session:
|
|
|
|
| 383 |
user_files = data['users'][token]['files']
|
| 384 |
|
| 385 |
if request.method == 'POST':
|
| 386 |
+
files = request.files.getlist('files')
|
| 387 |
+
if files and len(files) > 20:
|
| 388 |
+
flash('Максимум 20 файлов за раз!')
|
| 389 |
+
return redirect(url_for('dashboard'))
|
| 390 |
+
|
| 391 |
+
if files:
|
| 392 |
os.makedirs('uploads', exist_ok=True)
|
|
|
|
|
|
|
| 393 |
api = HfApi()
|
| 394 |
+
temp_files = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 395 |
|
| 396 |
+
for file in files:
|
| 397 |
+
if file and file.filename:
|
| 398 |
+
filename = secure_filename(file.filename)
|
| 399 |
+
temp_path = os.path.join('uploads', filename)
|
| 400 |
+
file.save(temp_path)
|
| 401 |
+
temp_files.append((temp_path, filename))
|
|
|
|
|
|
|
| 402 |
|
| 403 |
+
for temp_path, filename in temp_files:
|
| 404 |
+
file_path = f"cloud_files/{token}/{filename}"
|
| 405 |
+
api.upload_file(
|
| 406 |
+
path_or_fileobj=temp_path,
|
| 407 |
+
path_in_repo=file_path,
|
| 408 |
+
repo_id=REPO_ID,
|
| 409 |
+
repo_type="dataset",
|
| 410 |
+
token=HF_TOKEN_WRITE,
|
| 411 |
+
commit_message=f"Загружен файл для {token}"
|
| 412 |
+
)
|
| 413 |
+
|
| 414 |
+
file_info = {
|
| 415 |
+
'filename': filename,
|
| 416 |
+
'path': file_path,
|
| 417 |
+
'type': get_file_type(filename),
|
| 418 |
+
'upload_date': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
| 419 |
+
}
|
| 420 |
+
data['users'][token]['files'].append(file_info)
|
| 421 |
+
|
| 422 |
+
if os.path.exists(temp_path):
|
| 423 |
+
os.remove(temp_path)
|
| 424 |
+
|
| 425 |
+
save_data(data)
|
| 426 |
+
flash('Файлы успешно загружены!')
|
| 427 |
|
| 428 |
return redirect(url_for('dashboard'))
|
| 429 |
|
|
|
|
| 441 |
<div class="container">
|
| 442 |
<h1>Zues Cloud Dashboard</h1>
|
| 443 |
<p>Токен: {{ token }}</p>
|
| 444 |
+
{% with messages = get_flashed_messages() %}
|
| 445 |
+
{% if messages %}
|
| 446 |
+
{% for message in messages %}
|
| 447 |
+
<div class="flash">{{ message }}</div>
|
| 448 |
+
{% endfor %}
|
| 449 |
+
{% endif %}
|
| 450 |
+
{% endwith %}
|
| 451 |
+
<form id="upload-form" method="POST" enctype="multipart/form-data">
|
| 452 |
+
<input type="file" name="files" multiple required accept="image/*,video/*">
|
| 453 |
+
<button type="submit" class="btn" id="upload-btn">Загрузить файлы</button>
|
| 454 |
</form>
|
| 455 |
+
<div id="progress-container">
|
| 456 |
+
<div id="progress-bar"></div>
|
| 457 |
+
</div>
|
| 458 |
<h2 style="margin-top: 30px;">Ваши файлы</h2>
|
| 459 |
<div class="file-grid">
|
| 460 |
{% for file in user_files %}
|
|
|
|
| 499 |
modal.innerHTML = '<div id="modalContent"></div>';
|
| 500 |
}
|
| 501 |
}
|
| 502 |
+
|
| 503 |
+
const form = document.getElementById('upload-form');
|
| 504 |
+
const progressBar = document.getElementById('progress-bar');
|
| 505 |
+
const progressContainer = document.getElementById('progress-container');
|
| 506 |
+
const uploadBtn = document.getElementById('upload-btn');
|
| 507 |
+
|
| 508 |
+
form.addEventListener('submit', function(e) {
|
| 509 |
+
e.preventDefault();
|
| 510 |
+
const files = form.querySelector('input[type="file"]').files;
|
| 511 |
+
if (files.length > 20) {
|
| 512 |
+
alert('Максимум 20 файлов за раз!');
|
| 513 |
+
return;
|
| 514 |
+
}
|
| 515 |
+
|
| 516 |
+
const formData = new FormData(form);
|
| 517 |
+
progressContainer.style.display = 'block';
|
| 518 |
+
progressBar.style.width = '0%';
|
| 519 |
+
uploadBtn.disabled = true;
|
| 520 |
+
|
| 521 |
+
const xhr = new XMLHttpRequest();
|
| 522 |
+
xhr.open('POST', '/dashboard', true);
|
| 523 |
+
|
| 524 |
+
xhr.upload.onprogress = function(event) {
|
| 525 |
+
if (event.lengthComputable) {
|
| 526 |
+
const percent = (event.loaded / event.total) * 100;
|
| 527 |
+
progressBar.style.width = percent + '%';
|
| 528 |
+
}
|
| 529 |
+
};
|
| 530 |
+
|
| 531 |
+
xhr.onload = function() {
|
| 532 |
+
if (xhr.status === 200) {
|
| 533 |
+
location.reload();
|
| 534 |
+
} else {
|
| 535 |
+
alert('Ошибка загрузки!');
|
| 536 |
+
progressContainer.style.display = 'none';
|
| 537 |
+
uploadBtn.disabled = false;
|
| 538 |
+
}
|
| 539 |
+
};
|
| 540 |
+
|
| 541 |
+
xhr.onerror = function() {
|
| 542 |
+
alert('Ошибка соединения!');
|
| 543 |
+
progressContainer.style.display = 'none';
|
| 544 |
+
uploadBtn.disabled = false;
|
| 545 |
+
};
|
| 546 |
+
|
| 547 |
+
xhr.send(formData);
|
| 548 |
+
});
|
| 549 |
</script>
|
| 550 |
</body>
|
| 551 |
</html>
|
| 552 |
'''
|
| 553 |
return render_template_string(html, token=token, user_files=user_files, repo_id=REPO_ID)
|
| 554 |
|
|
|
|
| 555 |
@app.route('/logout')
|
| 556 |
def logout():
|
| 557 |
session.pop('token', None)
|