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, jsonify
|
| 2 |
from flask_caching import Cache
|
| 3 |
import json
|
| 4 |
import os
|
|
@@ -10,6 +10,8 @@ from huggingface_hub import HfApi, hf_hub_download
|
|
| 10 |
from werkzeug.utils import secure_filename
|
| 11 |
import random
|
| 12 |
import string
|
|
|
|
|
|
|
| 13 |
|
| 14 |
app = Flask(__name__)
|
| 15 |
app.secret_key = os.getenv("FLASK_SECRET_KEY", "supersecretkey")
|
|
@@ -466,10 +468,10 @@ def dashboard():
|
|
| 466 |
{% elif file['type'] == 'image' %}
|
| 467 |
<img class="file-preview" src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ file['path'] }}" alt="{{ file['filename'] }}" loading="lazy" onclick="openModal(this.src, false)">
|
| 468 |
{% else %}
|
| 469 |
-
<p>
|
| 470 |
{% endif %}
|
| 471 |
<p>{{ file['upload_date'] }}</p>
|
| 472 |
-
<a href="
|
| 473 |
</div>
|
| 474 |
{% endfor %}
|
| 475 |
{% if not user_files %}
|
|
@@ -552,6 +554,40 @@ def dashboard():
|
|
| 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)
|
|
|
|
| 1 |
+
from flask import Flask, render_template_string, request, redirect, url_for, session, flash, jsonify, send_file
|
| 2 |
from flask_caching import Cache
|
| 3 |
import json
|
| 4 |
import os
|
|
|
|
| 10 |
from werkzeug.utils import secure_filename
|
| 11 |
import random
|
| 12 |
import string
|
| 13 |
+
import requests
|
| 14 |
+
from io import BytesIO
|
| 15 |
|
| 16 |
app = Flask(__name__)
|
| 17 |
app.secret_key = os.getenv("FLASK_SECRET_KEY", "supersecretkey")
|
|
|
|
| 468 |
{% elif file['type'] == 'image' %}
|
| 469 |
<img class="file-preview" src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ file['path'] }}" alt="{{ file['filename'] }}" loading="lazy" onclick="openModal(this.src, false)">
|
| 470 |
{% else %}
|
| 471 |
+
<p>{{ file['filename'] }}</p>
|
| 472 |
{% endif %}
|
| 473 |
<p>{{ file['upload_date'] }}</p>
|
| 474 |
+
<a href="{{ url_for('download_file', file_path=file['path'], filename=file['filename']) }}" class="btn download-btn">Скачать</a>
|
| 475 |
</div>
|
| 476 |
{% endfor %}
|
| 477 |
{% if not user_files %}
|
|
|
|
| 554 |
'''
|
| 555 |
return render_template_string(html, token=token, user_files=user_files, repo_id=REPO_ID)
|
| 556 |
|
| 557 |
+
@app.route('/download/<path:file_path>/<filename>')
|
| 558 |
+
def download_file(file_path, filename):
|
| 559 |
+
if 'token' not in session:
|
| 560 |
+
flash('Пожалуйста, войдите!')
|
| 561 |
+
return redirect(url_for('login'))
|
| 562 |
+
|
| 563 |
+
token = session['token']
|
| 564 |
+
data = load_data()
|
| 565 |
+
if token not in data['users']:
|
| 566 |
+
session.pop('token', None)
|
| 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'))
|
| 575 |
+
|
| 576 |
+
file_url = f"https://huggingface.co/datasets/{REPO_ID}/resolve/main/{file_path}"
|
| 577 |
+
response = requests.get(file_url)
|
| 578 |
+
|
| 579 |
+
if response.status_code == 200:
|
| 580 |
+
file_content = BytesIO(response.content)
|
| 581 |
+
return send_file(
|
| 582 |
+
file_content,
|
| 583 |
+
as_attachment=True,
|
| 584 |
+
download_name=filename,
|
| 585 |
+
mimetype='application/octet-stream'
|
| 586 |
+
)
|
| 587 |
+
else:
|
| 588 |
+
flash('Ошибка при скачивании файла!')
|
| 589 |
+
return redirect(url_for('dashboard'))
|
| 590 |
+
|
| 591 |
@app.route('/logout')
|
| 592 |
def logout():
|
| 593 |
session.pop('token', None)
|