diff --git a/app/api/v1/endpoints/group.py b/app/api/v1/endpoints/group.py index ad93b33..e536ae1 100644 --- a/app/api/v1/endpoints/group.py +++ b/app/api/v1/endpoints/group.py @@ -264,4 +264,42 @@ async def disband(group_id: int, db: AsyncSession = Depends(get_db), user: dict os.remove(article_url) if avatar_url != "/lhcos-data/group-avatar/default.png": os.remove(avatar_url) - return {"msg": "Group disbanded successfully"} \ No newline at end of file + return {"msg": "Group disbanded successfully"} + + +from app.curd.article import get_article_info_in_db_by_id, crud_upload_to_self_folder +@router.put("/copy", response_model=dict) +async def copy_article(folder_id: int, article_id: int, is_group: Optional[bool] = None, db : AsyncSession = Depends(get_db), user: dict = Depends(get_current_user)): + """ + 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: + raise HTTPException(status_code=404, detail="File not found") + + old_file_path = file_path + new_file_path = f"/lhcos-data/{uuid.uuid4()}.pdf" + + with open(old_file_path, "rb") as source_file: + with open(new_file_path, "wb") as dest_file: + dest_file.write(source_file.read()) + + if is_group is not None and is_group is True: + # 表示从群组转存到个人目录 + new_article_id = await crud_new_article( + user_id= user.get("id"), + folder_id=folder_id, + article_name=title, + url=new_file_path, + db=db + ) + return {"msg": "Article copied successfully", "new_article_id": new_article_id} + else: + new_article_id = await crud_upload_to_self_folder( + name=title, + folder_id=folder_id, + url=new_file_path, + db=db + ) + return {"msg": "Article copied successfully", "new_article_id": new_article_id} diff --git a/app/curd/article.py b/app/curd/article.py index b5ebd87..d3bb78f 100644 --- a/app/curd/article.py +++ b/app/curd/article.py @@ -473,4 +473,14 @@ async def crud_recover(type: int, id: int, db: AsyncSession): folder.visible = True await db.commit() await db.refresh(folder) - return {"info": "Folder recovered successfully"} \ No newline at end of file + return {"info": "Folder recovered successfully"} + +async def get_article_info_in_db_by_id(db: AsyncSession, article_id: int): + """ + Get an article by its ID. + """ + result = await db.execute(select(Article).where(Article.id == article_id)) + article = result.scalars().first() + if not article: + return None, None + return article.url, article.name \ No newline at end of file