@@ -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 }
0 commit comments