Skip to content

支持本地 TXT 小说上传#7

Open
Maple0517 wants to merge 5 commits into
givenge:masterfrom
Maple0517:Maple/local-txt-upload-upstream
Open

支持本地 TXT 小说上传#7
Maple0517 wants to merge 5 commits into
givenge:masterfrom
Maple0517:Maple/local-txt-upload-upstream

Conversation

@Maple0517

@Maple0517 Maple0517 commented Jun 8, 2026

Copy link
Copy Markdown

背景

当前项目主要通过远程书源搜索、加入书架、拉取目录和章节内容。对于用户本地已有的 .txt 小说,原流程需要额外找书源或手动转换,使用成本比较高。

这个 PR 增加“本地上传 TXT 小说”能力:用户可以直接在书架上传 .txt 文件,后端解析章节并保存到当前用户的本地存储目录,前端把它当成普通书籍进入阅读器。

说明:本 PR 由 Codex 辅助完成实现、测试和 PR 整理。

主要改动

后端

  • 新增 LocalTxtBookService
    • 校验上传文件类型和大小,只允许 .txt,最大 50MB。
    • 自动识别 UTF-8 / UTF-8 BOM / GB18030 文本。
    • 解析常见章节标题,生成本地章节目录。
    • 将原文和章节索引保存到 storage/data/<user>/local_books/<hash>/
  • 新增 /reader3/uploadTxtBook 接口:
    • 接收 multipart 上传。
    • 导入成功后自动保存到书架。
  • 在既有阅读 API 中接入本地 TXT:
    • /reader3/getBookInfo
    • /reader3/getChapterList
    • /reader3/getBookContent
    • /reader3/saveBookProgress
  • 本地 TXT 不再走远程书源解析,不依赖外部站点。

前端

  • 在书架顶部增加“上传 TXT”入口。
  • 上传成功后刷新书架,并自动打开刚导入的书。
  • 新增 isLocalTxtBook 工具函数,统一判断本地 TXT 书籍。
  • 本地 TXT 书籍隐藏服务端缓存 / 浏览器缓存标签,避免把源文件误展示成缓存章节。
  • 缓存管理中跳过本地 TXT,因为上传后的源文件已经保存在后端本地存储里。
  • 阅读器目录中不再给本地 TXT 展示“已缓存”状态。

本次附带修复 / 加固

在 review 后补了几处关键加固:

  • 修复 local-txt: URL 后缀未校验的问题:
    • 现在只接受上传时生成的 32 位 hex hash。
    • 防止通过 local-txt:/absolute/pathlocal-txt:../path 逃出 local_books 目录。
  • 删除本地 TXT 书籍时同步清理上传文件目录:
    • 避免删除书架后旧 local-txt:<hash> 仍可读取。
    • 避免长期残留无用 TXT 文件占用磁盘。
  • 修复章节解析对相邻空章节的边界处理。
  • 支持无空格章节标题,例如:
    • 第一章初见
    • 第2章重逢
  • 本地 TXT 在浏览器 navigator.onLine=false 时仍会尝试请求后端读取本地文件:
    • 避免本地服务可用但浏览器认为离线时无法阅读。

用户影响

  • 用户可以直接上传本地 .txt 小说并加入书架。
  • 上传后可以像普通书籍一样阅读、保存进度、查看目录。
  • 本地 TXT 不再显示误导性的缓存状态。
  • 删除本地 TXT 书籍后,对应上传文件会被清理。

验证

已在干净 PR 分支 Maple/local-txt-upload-upstream 上执行:

  • cargo test
  • cd frontend && npm test -- --run
  • cd frontend && npm run build

验证结果均通过。

说明:cargo test 期间仍会输出项目既有的 dead-code warning,本 PR 未新增这类 warning。

@Maple0517 Maple0517 changed the title [codex] Add local TXT book uploads [codex] 支持本地 TXT 小说上传 Jun 8, 2026
@Maple0517 Maple0517 changed the title [codex] 支持本地 TXT 小说上传 支持本地 TXT 小说上传 Jun 8, 2026
@Maple0517 Maple0517 marked this pull request as ready for review June 8, 2026 14:35
@Maple0517 Maple0517 marked this pull request as draft June 8, 2026 14:36
@Maple0517 Maple0517 marked this pull request as ready for review June 8, 2026 14:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant