22from sqlalchemy .ext .asyncio import AsyncSession
33from app .utils .get_db import get_db
44from app .schemas .articleDB import UploadArticle , GetArticle , DeLArticle , GetResponse
5- from app .curd .articleDB import create_article_in_db , get_article_in_db , get_article_in_db_by_id
5+ from app .curd .articleDB import create_article_in_db , get_article_in_db , get_article_in_db_by_id , get_article_info_in_db_by_id
66from app .core .config import settings
77import os
88import uuid
99from fastapi .responses import FileResponse
1010from urllib .parse import quote
11+ from app .curd .article import crud_upload_to_self_folder
1112router = APIRouter ()
1213
1314@router .put ("/upload" , response_model = dict )
@@ -77,4 +78,31 @@ async def download_article(article_id: int, db: AsyncSession = Depends(get_db)):
7778 path = article .file_path ,
7879 filename = quote (download_filename ),
7980 media_type = "application/pdf"
80- )
81+ )
82+
83+ @router .put ("/copy" , response_model = dict )
84+ async def copy_article (folder_id : int , article_id : int , db : AsyncSession = Depends (get_db )):
85+ """
86+ Copy an article file by its ID to a specified directory.
87+ """
88+ # 根据 ID 查询文章信息
89+ file_path , title = await get_article_info_in_db_by_id (db = db , article_id = article_id )
90+ if not file_path :
91+ raise HTTPException (status_code = 404 , detail = "File not found" )
92+
93+ new_article_id = await crud_upload_to_self_folder (name = title , folder_id = folder_id , db = db )
94+
95+ # 复制文件到新的目录
96+ old_file_path = file_path
97+ new_file_path = os .path .join ("/lhcos-data" , f"{ new_article_id } .pdf" )
98+ try :
99+ with open (old_file_path , "rb" ) as source_file :
100+ with open (new_file_path , "wb" ) as dest_file :
101+ dest_file .write (source_file .read ())
102+ except Exception as e :
103+ raise HTTPException (status_code = 500 , detail = str (e ))
104+ return {"msg" : "Article copied successfully" , "new_article_id" : new_article_id }
105+
106+
107+
108+
0 commit comments