Skip to content

Commit 405645b

Browse files
authored
Merge pull request #58 from BUAA-SE-coders007/feature/get_article_from_db
[feat]: 实现转存
2 parents 8fa47b7 + 1b67e6c commit 405645b

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

app/api/v1/endpoints/articleDB.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
from sqlalchemy.ext.asyncio import AsyncSession
33
from app.utils.get_db import get_db
44
from 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
66
from app.core.config import settings
77
import os
88
import uuid
99
from fastapi.responses import FileResponse
1010
from urllib.parse import quote
11+
from app.curd.article import crud_upload_to_self_folder
1112
router = 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+

app/curd/articleDB.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,12 @@ async def get_article_in_db_by_id(db: AsyncSession, article_id: int):
4444
"""
4545
result = await db.execute(select(ArticleDB).where(ArticleDB.id == article_id))
4646
article = result.scalars().first()
47-
return article
47+
return article
48+
49+
async def get_article_info_in_db_by_id(db: AsyncSession, article_id: int):
50+
"""
51+
Get an article by its ID.
52+
"""
53+
result = await db.execute(select(ArticleDB).where(ArticleDB.id == article_id))
54+
article = result.scalars().first()
55+
return article.file_path, article.title

0 commit comments

Comments
 (0)