Skip to content

Commit f845f70

Browse files
authored
Merge pull request #101 from BUAA-SE-coders007/fix/100
Fix/100
2 parents 2ab444a + 1bf936c commit f845f70

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

app/api/v1/endpoints/group.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,4 +264,42 @@ async def disband(group_id: int, db: AsyncSession = Depends(get_db), user: dict
264264
os.remove(article_url)
265265
if avatar_url != "/lhcos-data/group-avatar/default.png":
266266
os.remove(avatar_url)
267-
return {"msg": "Group disbanded successfully"}
267+
return {"msg": "Group disbanded successfully"}
268+
269+
270+
from app.curd.article import get_article_info_in_db_by_id, crud_upload_to_self_folder
271+
@router.put("/copy", response_model=dict)
272+
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)):
273+
"""
274+
Copy an article file by its ID to a specified directory.
275+
"""
276+
# 根据 ID 查询文章信息
277+
file_path, title = await get_article_info_in_db_by_id(db=db, article_id=article_id)
278+
if not file_path:
279+
raise HTTPException(status_code=404, detail="File not found")
280+
281+
old_file_path = file_path
282+
new_file_path = f"/lhcos-data/{uuid.uuid4()}.pdf"
283+
284+
with open(old_file_path, "rb") as source_file:
285+
with open(new_file_path, "wb") as dest_file:
286+
dest_file.write(source_file.read())
287+
288+
if is_group is not None and is_group is True:
289+
# 表示从群组转存到个人目录
290+
new_article_id = await crud_new_article(
291+
user_id= user.get("id"),
292+
folder_id=folder_id,
293+
article_name=title,
294+
url=new_file_path,
295+
db=db
296+
)
297+
return {"msg": "Article copied successfully", "new_article_id": new_article_id}
298+
else:
299+
new_article_id = await crud_upload_to_self_folder(
300+
name=title,
301+
folder_id=folder_id,
302+
url=new_file_path,
303+
db=db
304+
)
305+
return {"msg": "Article copied successfully", "new_article_id": new_article_id}

app/curd/article.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,4 +473,14 @@ async def crud_recover(type: int, id: int, db: AsyncSession):
473473
folder.visible = True
474474
await db.commit()
475475
await db.refresh(folder)
476-
return {"info": "Folder recovered successfully"}
476+
return {"info": "Folder recovered successfully"}
477+
478+
async def get_article_info_in_db_by_id(db: AsyncSession, article_id: int):
479+
"""
480+
Get an article by its ID.
481+
"""
482+
result = await db.execute(select(Article).where(Article.id == article_id))
483+
article = result.scalars().first()
484+
if not article:
485+
return None, None
486+
return article.url, article.name

0 commit comments

Comments
 (0)