From 4f26f94357b2734d856be0e9b47b4c602e3368f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9F=A9=E6=98=95=E7=9D=BF?= <22371298@buaa.edu.cn> Date: Fri, 6 Jun 2025 23:03:28 +0800 Subject: [PATCH 1/2] =?UTF-8?q?[fix]:=20=E4=BF=AE=E5=A4=8D=E6=96=87?= =?UTF-8?q?=E7=8C=AE=E5=BA=93=E6=96=87=E7=8C=AE=E8=BD=AC=E5=82=A8=E8=B7=AF?= =?UTF-8?q?=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/v1/endpoints/articleDB.py | 33 +++++++++++-------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/app/api/v1/endpoints/articleDB.py b/app/api/v1/endpoints/articleDB.py index 715ddbd..436b7e7 100644 --- a/app/api/v1/endpoints/articleDB.py +++ b/app/api/v1/endpoints/articleDB.py @@ -103,35 +103,24 @@ async def copy_article(folder_id: int, article_id: int, is_group: bool | None = Copy an article file by its ID to a specified directory. """ # 根据 ID 查询文章信息 - file_path, title = await get_article_info_in_db_by_id(db=db, article_id=article_id) - if not file_path: + old_file_path, title = await get_article_info_in_db_by_id(db=db, article_id=article_id) + if not old_file_path: raise HTTPException(status_code=404, detail="File not found") - old_file_path = file_path - - if is_group != None and is_group is True: - url = f"/lhcos-data/{uuid.uuid4()}.pdf" - with open(old_file_path, "rb") as source_file: - with open(url, "wb") as dest_file: - dest_file.write(source_file.read()) - # 用文件名(不带扩展名)作为 Article 名称 + # 复制PDF文件 + url = f"/lhcos-data/{uuid.uuid4()}.pdf" + with open(old_file_path, "rb") as source_file: + with open(url, "wb") as dest_file: + dest_file.write(source_file.read()) + # 转入组织 + if is_group: user_id = user.get("id") from app.curd.group import crud_new_article article_id = await crud_new_article(user_id, folder_id, title, url, db) return {"msg": "Article copied successfully", "new_article_id": article_id} - - new_article_id = await crud_upload_to_self_folder(name=title, folder_id=folder_id, url=old_file_path ,db=db) - - # 复制文件到新的目录 - new_file_path = os.path.join("/lhcos-data", f"{new_article_id}.pdf") - try: - with open(old_file_path, "rb") as source_file: - with open(new_file_path, "wb") as dest_file: - dest_file.write(source_file.read()) - except Exception as e: - raise HTTPException(status_code=500, detail=str(e)) + #转入个人 + new_article_id = await crud_upload_to_self_folder(name=title, folder_id=folder_id, url=url ,db=db) return {"msg": "Article copied successfully", "new_article_id": new_article_id} - @router.get("/recommend", response_model=dict) async def recommend_article(recommend_article: RecommendArticle = Depends(), db: AsyncSession = Depends(get_db)): articles = await recommend_article_in_db(db=db, recommend_article=recommend_article) From 13181eab118f16f98883ca7e84c998dfa4278e53 Mon Sep 17 00:00:00 2001 From: Fantasy lee <129943055+Fantasylee21@users.noreply.github.com> Date: Sat, 7 Jun 2025 09:33:09 +0800 Subject: [PATCH 2/2] =?UTF-8?q?[fix]:=20=E4=BF=AE=E5=A4=8D500=E6=8A=A5?= =?UTF-8?q?=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/v1/endpoints/articleDB.py | 32 ++++++++++++++++++++----------- app/curd/articleDB.py | 2 ++ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/app/api/v1/endpoints/articleDB.py b/app/api/v1/endpoints/articleDB.py index 436b7e7..b82d127 100644 --- a/app/api/v1/endpoints/articleDB.py +++ b/app/api/v1/endpoints/articleDB.py @@ -103,22 +103,32 @@ async def copy_article(folder_id: int, article_id: int, is_group: bool | None = Copy an article file by its ID to a specified directory. """ # 根据 ID 查询文章信息 - old_file_path, title = await get_article_info_in_db_by_id(db=db, article_id=article_id) - if not old_file_path: + file_path, title = await get_article_info_in_db_by_id(db=db, article_id=article_id) + if not file_path: raise HTTPException(status_code=404, detail="File not found") - # 复制PDF文件 - url = f"/lhcos-data/{uuid.uuid4()}.pdf" - with open(old_file_path, "rb") as source_file: - with open(url, "wb") as dest_file: - dest_file.write(source_file.read()) - # 转入组织 - if is_group: + old_file_path = file_path + + if is_group != None and is_group is True: + url = f"/lhcos-data/{uuid.uuid4()}.pdf" + with open(old_file_path, "rb") as source_file: + with open(url, "wb") as dest_file: + dest_file.write(source_file.read()) + # 用文件名(不带扩展名)作为 Article 名称 user_id = user.get("id") from app.curd.group import crud_new_article article_id = await crud_new_article(user_id, folder_id, title, url, db) return {"msg": "Article copied successfully", "new_article_id": article_id} - #转入个人 - new_article_id = await crud_upload_to_self_folder(name=title, folder_id=folder_id, url=url ,db=db) + + new_article_id = await crud_upload_to_self_folder(name=title, folder_id=folder_id, url=old_file_path ,db=db) + + # 复制文件到新的目录 + new_file_path = os.path.join("/lhcos-data", f"{new_article_id}.pdf") + try: + with open(old_file_path, "rb") as source_file: + with open(new_file_path, "wb") as dest_file: + dest_file.write(source_file.read()) + except Exception as e: + raise HTTPException(status_code=500, detail=str(e)) return {"msg": "Article copied successfully", "new_article_id": new_article_id} @router.get("/recommend", response_model=dict) diff --git a/app/curd/articleDB.py b/app/curd/articleDB.py index 1670657..b034b8d 100644 --- a/app/curd/articleDB.py +++ b/app/curd/articleDB.py @@ -77,6 +77,8 @@ async def get_article_info_in_db_by_id(db: AsyncSession, article_id: int): """ result = await db.execute(select(ArticleDB).where(ArticleDB.id == article_id)) article = result.scalars().first() + if not article: + return None, None return article.file_path, article.title async def recommend_article_in_db(db: AsyncSession, recommend_article: RecommendArticle):