diff --git a/app/api/v1/endpoints/article.py b/app/api/v1/endpoints/article.py index 886661e..8ee6a8d 100644 --- a/app/api/v1/endpoints/article.py +++ b/app/api/v1/endpoints/article.py @@ -11,7 +11,7 @@ from app.utils.get_db import get_db from app.utils.auth import get_current_user -from app.curd.article import crud_upload_to_self_folder, crud_get_self_folders, crud_get_articles_in_folder, crud_self_create_folder, crud_self_article_to_recycle_bin, crud_self_folder_to_recycle_bin, crud_read_article, crud_import_self_folder, crud_export_self_folder,crud_create_tag, crud_delete_tag, crud_get_article_tags, crud_all_tags_order, crud_change_folder_name, crud_change_article_name +from app.curd.article import crud_upload_to_self_folder, crud_get_self_folders, crud_get_articles_in_folder, crud_self_create_folder, crud_self_article_to_recycle_bin, crud_self_folder_to_recycle_bin, crud_read_article, crud_import_self_folder, crud_export_self_folder,crud_create_tag, crud_delete_tag, crud_get_article_tags, crud_all_tags_order, crud_change_folder_name, crud_change_article_name, crud_article_statistic from app.schemas.article import SelfCreateFolder router = APIRouter() @@ -25,10 +25,9 @@ async def upload_to_self_folder(folder_id: int = Query(...), article: UploadFile # 新建 Article 记录 article_id = await crud_upload_to_self_folder(name, folder_id, db) - # 存储文件,暂时存储到本地 - save_dir = "articles" - os.makedirs(save_dir, exist_ok=True) # 如果目录不存在则创建 - save_path = os.path.join(save_dir, f"{article_id}.pdf") + # 存储到云存储位置 + os.makedirs("/lhcos-data", exist_ok=True) + save_path = os.path.join("/lhcos-data", f"{article_id}.pdf") with open(save_path, "wb") as f: content = await article.read() f.write(content) @@ -82,10 +81,9 @@ async def self_folder_to_recycle_bin(folder_id: int = Query(...), db: AsyncSessi @router.post("/annotateSelfArticle", response_model="dict") async def annotate_self_article(article_id: int = Query(...), article: UploadFile = File(...)): - # 存储文件,将新文件暂时存储到本地 - save_dir = "articles" - os.makedirs(save_dir, exist_ok=True) # 如果目录不存在则创建 - save_path = os.path.join(save_dir, f"{article_id}.pdf") + # 将新文件存储到云存储位置 + os.makedirs("/lhcos-data", exist_ok=True) + save_path = os.path.join("/lhcos-data", f"{article_id}.pdf") with open(save_path, "wb") as f: content = await article.read() f.write(content) @@ -95,7 +93,7 @@ async def annotate_self_article(article_id: int = Query(...), article: UploadFil @router.get("/readArticle", response_class=FileResponse) async def read_article(article_id: int = Query(...), db: AsyncSession = Depends(get_db)): - file_path = f"articles/{article_id}.pdf" + file_path = f"/lhcos-data/{article_id}.pdf" # 查询文件名 article_name = await crud_read_article(article_id, db) @@ -119,14 +117,14 @@ async def import_self_folder(folder_name: str = Query(...), zip: UploadFile = Fi # 记入数据库 result = await crud_import_self_folder(folder_name, article_names, user_id, db) - # 存储文献,暂时存储到本地 - os.makedirs("articles", exist_ok=True) + # 存储文献到云存储 + os.makedirs("/lhcos-data", exist_ok=True) for i in range(0, len(result), 2): article_id = result[i] article_name = result[i + 1] pdf_filename_in_zip = f"{article_name}.pdf" with zip_file.open(pdf_filename_in_zip) as source_file: - target_path = os.path.join("articles", f"{article_id}.pdf") + target_path = os.path.join("/lhcos-data", f"{article_id}.pdf") with open(target_path, "wb") as out_file: out_file.write(source_file.read()) @@ -141,7 +139,7 @@ async def export_self_folder(background_tasks: BackgroundTasks, folder_id: int = with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zipf: for article_id, article_name in zip(article_ids, article_names): - pdf_path = os.path.join("articles", f"{article_id}.pdf") + pdf_path = os.path.join("/lhcos-data", f"{article_id}.pdf") arcname = f"{article_name}.pdf" # 压缩包内的文件名 zipf.write(pdf_path, arcname=arcname) diff --git a/app/api/v1/endpoints/auth.py b/app/api/v1/endpoints/auth.py index 9d4c66a..f393a42 100644 --- a/app/api/v1/endpoints/auth.py +++ b/app/api/v1/endpoints/auth.py @@ -13,7 +13,7 @@ from app.schemas.auth import UserCreate, UserLogin, UserSendCode, ReFreshToken from app.core.config import settings from app.curd.user import get_user_by_email, create_user -from app.curd.article import crud_self_create_folder +from app.curd.article import crud_self_create_folder, crud_article_statistic from app.utils.get_db import get_db from app.utils.redis import get_redis_client @@ -146,4 +146,9 @@ async def send_code(user_send_code: UserSendCode): return {"msg": "Verification code sent"} except aiosmtplib.SMTPException as e: - raise HTTPException(status_code=500, detail=f"Failed to send email: {str(e)}") \ No newline at end of file + raise HTTPException(status_code=500, detail=f"Failed to send email: {str(e)}") + +@router.get("/articleStatistic", response_model="dict") +async def article_statistic(db: AsyncSession = Depends(get_db)): + articles = await crud_article_statistic(db) + return {"articles": articles} \ No newline at end of file diff --git a/app/core/config.py b/app/core/config.py index acef1dd..b881797 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -9,7 +9,7 @@ class Settings: SQLALCHEMY_DATABASE_URL = "mysql+asyncmy://root:oneapi@47.93.172.156:3306/JieNote" # 替换为实际的用户名、密码和数据库名称 SECRET_KEY: str = os.getenv("SECRET_KEY", "default_secret_key") # JWT密钥 ALGORITHM: str = "HS256" # JWT算法 - ACCESS_TOKEN_EXPIRE_MINUTES: int = 5 # token过期时间 + ACCESS_TOKEN_EXPIRE_MINUTES: int = 1440 # token过期时间 REFRESH_TOKEN_EXPIRE_DAYS: int = 7 # 刷新token过期时间7天 SMTP_SERVER: str = "smtp.163.com" # SMTP服务器 SMTP_PORT: int = 465 # SMTP端口 diff --git a/app/curd/article.py b/app/curd/article.py index 7d882fe..d3c6b90 100644 --- a/app/curd/article.py +++ b/app/curd/article.py @@ -1,5 +1,7 @@ from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy import select, delete +from sqlalchemy import func, cast, Date +from datetime import datetime, timedelta from app.models.model import User, Group, Folder, Article, Note, Tag, user_group async def crud_upload_to_self_folder(name: str, folder_id: int, db: AsyncSession): @@ -148,4 +150,37 @@ async def crud_change_article_name(article_id: int, article_name: str, db: Async article.name = article_name await db.commit() - await db.refresh(article) \ No newline at end of file + await db.refresh(article) + +async def crud_article_statistic(db: AsyncSession): + # 获取明天日期和7天前的日期 + tomorrow = datetime.now().date() + timedelta(days=1) + seven_days_ago = datetime.now().date() - timedelta(days=6) + + # 查询近7天内的笔记数目,按日期分组 + query = ( + select( + cast(Article.create_time, Date).label("date"), # 按日期分组 + func.count(Article.id).label("count") # 统计每日期的笔记数 + ) + .where( + Article.create_time >= seven_days_ago, # 大于等于7天前的0点 + Article.create_time < tomorrow # 小于明天0点 + ) + .group_by(cast(Article.create_time, Date)) # 按日期分组 + .order_by(cast(Article.create_time, Date)) # 按日期排序 + ) + + # 执行查询 + result = await db.execute(query) + data = result.fetchall() + + # 格式化结果为字典列表 + articles = [{"date": row.date, "count": row.count} for row in data] + + # 若某日期没有记录,则为0 + for i in range(0, 7): + if i == len(articles) or articles[i].get("date") != seven_days_ago + timedelta(days=i): + articles.insert(i, {"date": seven_days_ago + timedelta(days=i), "count": 0}) + + return articles \ No newline at end of file