From 1b67e6c8309943aa82df1f05a6373b7b05d9acc9 Mon Sep 17 00:00:00 2001 From: Fantasy lee <129943055+Fantasylee21@users.noreply.github.com> Date: Mon, 5 May 2025 22:13:23 +0800 Subject: [PATCH] =?UTF-8?q?[feat]:=20=E5=AE=9E=E7=8E=B0=E8=BD=AC=E5=AD=98?= 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 | 10 +++++++++- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/app/api/v1/endpoints/articleDB.py b/app/api/v1/endpoints/articleDB.py index 1db5986..c502986 100644 --- a/app/api/v1/endpoints/articleDB.py +++ b/app/api/v1/endpoints/articleDB.py @@ -2,12 +2,13 @@ from sqlalchemy.ext.asyncio import AsyncSession from app.utils.get_db import get_db from app.schemas.articleDB import UploadArticle, GetArticle, DeLArticle, GetResponse -from app.curd.articleDB import create_article_in_db, get_article_in_db, get_article_in_db_by_id +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 from app.core.config import settings import os import uuid from fastapi.responses import FileResponse from urllib.parse import quote +from app.curd.article import crud_upload_to_self_folder router = APIRouter() @router.put("/upload", response_model=dict) @@ -77,4 +78,31 @@ async def download_article(article_id: int, db: AsyncSession = Depends(get_db)): path=article.file_path, filename=quote(download_filename), media_type="application/pdf" - ) \ No newline at end of file + ) + +@router.put("/copy", response_model=dict) +async def copy_article(folder_id: int, article_id: int, db: AsyncSession = Depends(get_db)): + """ + 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") + + new_article_id = await crud_upload_to_self_folder(name=title, folder_id=folder_id, db=db) + + # 复制文件到新的目录 + old_file_path = file_path + 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} + + + + diff --git a/app/curd/articleDB.py b/app/curd/articleDB.py index 10ad694..9af3af5 100644 --- a/app/curd/articleDB.py +++ b/app/curd/articleDB.py @@ -44,4 +44,12 @@ async def get_article_in_db_by_id(db: AsyncSession, article_id: int): """ result = await db.execute(select(ArticleDB).where(ArticleDB.id == article_id)) article = result.scalars().first() - return article \ No newline at end of file + return article + +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(ArticleDB).where(ArticleDB.id == article_id)) + article = result.scalars().first() + return article.file_path, article.title \ No newline at end of file