This commit is contained in:
13
main.py
13
main.py
@@ -8,7 +8,8 @@ from datetime import datetime as dt
|
||||
from datetime import datetime, timedelta
|
||||
import random
|
||||
|
||||
from rarfile import RarFile
|
||||
import zipfile
|
||||
# from rarfile import RarFile
|
||||
import tempfile
|
||||
|
||||
# Сторонние библиотеки (third-party)
|
||||
@@ -501,24 +502,24 @@ async def download_all(dates: DownloadRange):
|
||||
return {"error": "Файлы не найдены за указанный период", "date_start": date_start, "date_finish": date_finish}
|
||||
|
||||
# Создаём архив во временной директории
|
||||
archive_name = f"documents.rar"
|
||||
archive_name = f"documents.zip"
|
||||
archive_path = os.path.join(tempfile.gettempdir(), archive_name)
|
||||
|
||||
try:
|
||||
with RarFile(archive_path, 'w') as rar:
|
||||
with zipfile.ZipFile(archive_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
||||
for file_path in all_files:
|
||||
rar.write(file_path, os.path.basename(file_path))
|
||||
zipf.write(file_path, os.path.basename(file_path))
|
||||
except Exception as e:
|
||||
logger.error(f"Ошибка создания архива: {e}")
|
||||
return {"error": f"Ошибка создания архива: {e}"}
|
||||
|
||||
|
||||
logger.info(f"Архив создан: {archive_path}")
|
||||
|
||||
# Возвращаем архив
|
||||
response = FileResponse(
|
||||
path=archive_path,
|
||||
filename=archive_name,
|
||||
media_type="application/x-rar-compressed"
|
||||
media_type="application/zip"
|
||||
)
|
||||
response.headers["Access-Control-Allow-Origin"] = "*"
|
||||
response.headers["Access-Control-Allow-Methods"] = "GET, OPTIONS"
|
||||
|
||||
Reference in New Issue
Block a user