feat: 新增 PDF 转换服务(epub->pdf,cookie 用户隔离,软删/硬删两级删除)
纯 Python 转换(ebooklib 解析 epub + weasyprint 渲染 HTML/CSS 为 PDF), 无需 calibre/xvfb 系统依赖。复用 UploadedFile 存储(原始文件与产物 PDF)。 - model/dao/schema:PdfJob 记录转换任务(owner_cookie/source/output/进度/状态) - pdf_converter:epub->PDF 转换器(按 spine 顺序拼 HTML,OPF 目录解析相对资源) - pdf_service:上传落盘 + asyncio 后台转换(独立 Session)+ 进度追踪 + 两级删除 · 用户软删(user_deleted,管理页仍可见)· 管理员硬删(真正删磁盘+记录) - pdf_controller:/api/pdf/*(用户,cookie)+ /api/admin/pdf/*(Basic Auth) - /pdf、/pdf-admin 页面(原生 JS,复用 common.css/js) - 配置 pdf 段(max_size 250MB、转换超时、cookie) - pytest 7 项(SQLite→MySQL 隔离测试库):上传/转换/下载/软删/硬删/超限/越权
This commit is contained in:
68
app/dao/pdf_job_dao.py
Normal file
68
app/dao/pdf_job_dao.py
Normal file
@@ -0,0 +1,68 @@
|
||||
"""PdfJob 的 DAO。
|
||||
|
||||
所有写操作均在该层 commit,service 不直接操作 session。
|
||||
用户/管理两条查询路径分别按 user_deleted 过滤。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..models.pdf_job import PdfJob
|
||||
|
||||
|
||||
class PdfJobDAO:
|
||||
def __init__(self, db: Session) -> None:
|
||||
self.db = db
|
||||
|
||||
def create(self, job: PdfJob) -> PdfJob:
|
||||
self.db.add(job)
|
||||
self.db.commit()
|
||||
self.db.refresh(job)
|
||||
return job
|
||||
|
||||
def get(self, job_id: int) -> PdfJob | None:
|
||||
return self.db.get(PdfJob, job_id)
|
||||
|
||||
def update(self, job: PdfJob) -> PdfJob:
|
||||
"""提交对 job 的就地修改并刷新。"""
|
||||
self.db.commit()
|
||||
self.db.refresh(job)
|
||||
return job
|
||||
|
||||
def list_for_user(self, owner_cookie: str, limit: int = 100, offset: int = 0) -> list[PdfJob]:
|
||||
"""用户视角:仅未软删的任务,按创建时间倒序。"""
|
||||
stmt = (
|
||||
select(PdfJob)
|
||||
.where(PdfJob.owner_cookie == owner_cookie)
|
||||
.where(PdfJob.user_deleted == False) # noqa: E712
|
||||
.order_by(PdfJob.created_at.desc())
|
||||
.limit(limit)
|
||||
.offset(offset)
|
||||
)
|
||||
return list(self.db.scalars(stmt).all())
|
||||
|
||||
def count_for_user(self, owner_cookie: str) -> int:
|
||||
stmt = (
|
||||
select(func.count())
|
||||
.select_from(PdfJob)
|
||||
.where(PdfJob.owner_cookie == owner_cookie)
|
||||
.where(PdfJob.user_deleted == False) # noqa: E712
|
||||
)
|
||||
return self.db.scalar(stmt) or 0
|
||||
|
||||
def list_all(self, limit: int = 100, offset: int = 0) -> list[PdfJob]:
|
||||
"""管理视角:全部任务(含已软删),按创建时间倒序。"""
|
||||
stmt = (
|
||||
select(PdfJob)
|
||||
.order_by(PdfJob.created_at.desc())
|
||||
.limit(limit)
|
||||
.offset(offset)
|
||||
)
|
||||
return list(self.db.scalars(stmt).all())
|
||||
|
||||
def count_all(self) -> int:
|
||||
return self.db.scalar(select(func.count()).select_from(PdfJob)) or 0
|
||||
Reference in New Issue
Block a user