From a8166b4874eaa9863f8b2f023c70f10358f0471c Mon Sep 17 00:00:00 2001 From: admin Date: Tue, 7 Apr 2026 10:03:46 +1000 Subject: [PATCH] =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B1=D1=83=D0=B5=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/main.py b/main.py index 41c5e0b..c854f66 100644 --- a/main.py +++ b/main.py @@ -215,7 +215,6 @@ def gpt_response_message(content, ist_number=1): # else: # contentGPT = Promts[1].prompt.replace('{content}', content) - if ist_number == 1: url_ist = "http://epaper.hljnews.cn/hljrb/pc/layout" else: @@ -465,11 +464,10 @@ class DownloadRange(BaseModel): data_finish: str @app.post("/download_all", summary="Скачать все файлы за период") -async def download_all(dates: DownloadRange): - +async def download_all(dates: DownloadRange, background_tasks: BackgroundTasks): date_start = dates.data_start date_finish = dates.data_finish - # Парсим даты + try: start_date = datetime.strptime(date_start, "%Y-%m-%d") finish_date = datetime.strptime(date_finish, "%Y-%m-%d") @@ -481,7 +479,6 @@ async def download_all(dates: DownloadRange): all_files = [] - # Собираем файлы за каждый день current_date = start_date while current_date <= finish_date: date_path = current_date.strftime("%Y/%m/%d") @@ -497,11 +494,10 @@ async def download_all(dates: DownloadRange): logger.info(f"Найдено файлов: {len(all_files)}") - # print(all_files) if not all_files: return {"error": "Файлы не найдены за указанный период", "date_start": date_start, "date_finish": date_finish} - # Создаём архив в директории DOCUMENTS_DIR + # Создаём архив archive_name = f"documents_{date_start}_{date_finish}.zip" archive_path = os.path.join(DOCUMENTS_DIR, archive_name) @@ -515,6 +511,15 @@ async def download_all(dates: DownloadRange): logger.info(f"Архив создан: {archive_path}") + # Функция для удаления архива после отдачи + def cleanup_archive(): + try: + if os.path.exists(archive_path): + os.remove(archive_path) + logger.info(f"Архив удалён: {archive_path}") + except Exception as e: + logger.warning(f"Не удалось удалить архив: {e}") + # Возвращаем архив response = FileResponse( path=archive_path, @@ -522,23 +527,15 @@ async def download_all(dates: DownloadRange): media_type="application/zip" ) response.headers["Access-Control-Allow-Origin"] = "*" - response.headers["Access-Control-Allow-Methods"] = "GET, OPTIONS" - response.headers["Access-Control-Allow-Headers"] = "Content-Type" + response.headers["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS" + response.headers["Access-Control-Allow-Headers"] = "Content-Type, Authorization" + response.headers["Access-Control-Expose-Headers"] = "Content-Disposition" - # Удаляем архив после отправки - # Примечание: FileResponse сам отдаёт файл, удаление нужно делать после - @response.streaming_callback - async def cleanup(): - await response.body_send() - try: - if os.path.exists(archive_path): - os.remove(archive_path) - logger.info(f"Архив удалён: {archive_path}") - except Exception as e: - logger.warning(f"Не удалось удалить архив: {e}") + # Удаляем архив после отправки через background task + background_tasks.add_task(cleanup_archive) return response - + @app.get("/logs") def get_logs(): with open("app.log", "r") as file: