Update app.py
Browse files
app.py
CHANGED
|
@@ -724,31 +724,28 @@ def download_tma(file_id):
|
|
| 724 |
file_node = get_file_node_for_user(file_id)
|
| 725 |
if not file_node:
|
| 726 |
return Response("Файл не найден или доступ запрещен!", status=404)
|
| 727 |
-
|
| 728 |
hf_path = file_node.get('path')
|
| 729 |
original_filename = file_node.get('original_filename', 'downloaded_file')
|
| 730 |
if not hf_path:
|
| 731 |
return Response("Ошибка: Путь к файлу не найден.", status=500)
|
| 732 |
|
| 733 |
-
initial_url = f"https://huggingface.co/datasets/{REPO_ID}/resolve/main/{quote(hf_path)}?download=true"
|
| 734 |
try:
|
| 735 |
-
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
|
| 739 |
-
|
| 740 |
-
|
| 741 |
-
|
| 742 |
-
|
| 743 |
-
|
| 744 |
-
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
|
| 748 |
-
resp.headers['Content-Disposition'] = f"attachment; filename=\"{sanitized_filename}\"; filename*=UTF-8''{encoded_filename}"
|
| 749 |
-
return resp
|
| 750 |
except Exception as e:
|
| 751 |
-
logging.error(f"Error downloading {file_id} from HF: {e}")
|
| 752 |
return Response(f'Ошибка скачивания файла: {e}', status=502)
|
| 753 |
|
| 754 |
@app.route('/batch_download_tma')
|
|
@@ -760,34 +757,33 @@ def batch_download_tma():
|
|
| 760 |
file_ids = file_ids_str.split(',')
|
| 761 |
|
| 762 |
temp_zip_file = tempfile.NamedTemporaryFile(delete=False, suffix=".zip")
|
| 763 |
-
req_headers = {}
|
| 764 |
-
if HF_TOKEN_READ: req_headers["authorization"] = f"Bearer {HF_TOKEN_READ}"
|
| 765 |
|
| 766 |
try:
|
| 767 |
-
with zipfile.ZipFile(temp_zip_file, 'w', zipfile.ZIP_DEFLATED) as zf:
|
| 768 |
for file_id in file_ids:
|
| 769 |
file_node = get_file_node_for_user(file_id)
|
| 770 |
if file_node and file_node.get('path'):
|
| 771 |
hf_path = file_node['path']
|
| 772 |
original_filename = file_node.get('original_filename', file_id)
|
| 773 |
-
initial_url = f"https://huggingface.co/datasets/{REPO_ID}/resolve/main/{quote(hf_path)}?download=true"
|
| 774 |
try:
|
| 775 |
-
|
| 776 |
-
|
| 777 |
-
|
| 778 |
-
|
| 779 |
-
|
| 780 |
-
|
| 781 |
-
|
|
|
|
|
|
|
| 782 |
except Exception as e:
|
| 783 |
logging.error(f"Failed to download and add {original_filename} to zip: {e}")
|
| 784 |
|
| 785 |
-
temp_zip_file.close()
|
| 786 |
return send_file(temp_zip_file.name, as_attachment=True, download_name='archive.zip', mimetype='application/zip')
|
| 787 |
finally:
|
| 788 |
if os.path.exists(temp_zip_file.name):
|
| 789 |
os.unlink(temp_zip_file.name)
|
| 790 |
|
|
|
|
| 791 |
@app.route('/delete_file_tma/<file_id>', methods=['POST'])
|
| 792 |
def delete_file_tma(file_id):
|
| 793 |
if 'telegram_user_id' not in session: return redirect(url_for('tma_entry_page'))
|
|
|
|
| 724 |
file_node = get_file_node_for_user(file_id)
|
| 725 |
if not file_node:
|
| 726 |
return Response("Файл не найден или доступ запрещен!", status=404)
|
| 727 |
+
|
| 728 |
hf_path = file_node.get('path')
|
| 729 |
original_filename = file_node.get('original_filename', 'downloaded_file')
|
| 730 |
if not hf_path:
|
| 731 |
return Response("Ошибка: Путь к файлу не найден.", status=500)
|
| 732 |
|
|
|
|
| 733 |
try:
|
| 734 |
+
local_path = hf_hub_download(
|
| 735 |
+
repo_id=REPO_ID,
|
| 736 |
+
filename=hf_path,
|
| 737 |
+
repo_type="dataset",
|
| 738 |
+
token=HF_TOKEN_READ,
|
| 739 |
+
local_dir=UPLOAD_FOLDER,
|
| 740 |
+
local_dir_use_symlinks=False
|
| 741 |
+
)
|
| 742 |
+
return send_file(
|
| 743 |
+
local_path,
|
| 744 |
+
as_attachment=True,
|
| 745 |
+
download_name=original_filename
|
| 746 |
+
)
|
|
|
|
|
|
|
| 747 |
except Exception as e:
|
| 748 |
+
logging.error(f"Error downloading {file_id} from HF using hf_hub_download: {e}")
|
| 749 |
return Response(f'Ошибка скачивания файла: {e}', status=502)
|
| 750 |
|
| 751 |
@app.route('/batch_download_tma')
|
|
|
|
| 757 |
file_ids = file_ids_str.split(',')
|
| 758 |
|
| 759 |
temp_zip_file = tempfile.NamedTemporaryFile(delete=False, suffix=".zip")
|
|
|
|
|
|
|
| 760 |
|
| 761 |
try:
|
| 762 |
+
with zipfile.ZipFile(temp_zip_file.name, 'w', zipfile.ZIP_DEFLATED) as zf:
|
| 763 |
for file_id in file_ids:
|
| 764 |
file_node = get_file_node_for_user(file_id)
|
| 765 |
if file_node and file_node.get('path'):
|
| 766 |
hf_path = file_node['path']
|
| 767 |
original_filename = file_node.get('original_filename', file_id)
|
|
|
|
| 768 |
try:
|
| 769 |
+
local_path = hf_hub_download(
|
| 770 |
+
repo_id=REPO_ID,
|
| 771 |
+
filename=hf_path,
|
| 772 |
+
repo_type="dataset",
|
| 773 |
+
token=HF_TOKEN_READ,
|
| 774 |
+
local_dir=UPLOAD_FOLDER,
|
| 775 |
+
local_dir_use_symlinks=False
|
| 776 |
+
)
|
| 777 |
+
zf.write(local_path, arcname=original_filename)
|
| 778 |
except Exception as e:
|
| 779 |
logging.error(f"Failed to download and add {original_filename} to zip: {e}")
|
| 780 |
|
|
|
|
| 781 |
return send_file(temp_zip_file.name, as_attachment=True, download_name='archive.zip', mimetype='application/zip')
|
| 782 |
finally:
|
| 783 |
if os.path.exists(temp_zip_file.name):
|
| 784 |
os.unlink(temp_zip_file.name)
|
| 785 |
|
| 786 |
+
|
| 787 |
@app.route('/delete_file_tma/<file_id>', methods=['POST'])
|
| 788 |
def delete_file_tma(file_id):
|
| 789 |
if 'telegram_user_id' not in session: return redirect(url_for('tma_entry_page'))
|