Compare commits

..
32 Commits
Author SHA1 Message Date
github-actions[bot] 42b2c29ccb chore(master): release 2.9.0 (#562)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2026-11-16 07:45:26 +00:00
vastsa f9cc16c805 feat: expose background in public config for 2024 theme (#546) 2026-11-16 15:44:56 +08:00
github-actions[bot] 8d3e4ad904 chore(master): release 2.8.5 (#558)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2026-10-27 08:09:02 +00:00
vastsa 381a539c92 fix: resolve OSS chunk cleanup and legacy download issues
Use compatible S3 checksum settings, retry failed cleanup without reusing completed uploads, and include legacy attachment filenames. Correct admin-page URLs in both documentation languages.

Refs #556, #531, #502
2026-10-27 16:08:35 +08:00
github-actions[bot] 6f88eae854 chore(master): release 2.8.4 (#555)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2026-10-25 15:44:31 +00:00
Lan 5da611c5de Merge pull request #554 from jcbl1/upstream/07-runtime
perf(runtime): enable uvloop/httptools and tune container memory/keep…
2026-10-25 23:44:26 +08:00
Lan a578fc690c Merge pull request #551 from jcbl1/upstream/04-db-indexes
perf(db): index expiry, recency and chunk lookup columns
2026-10-25 23:44:20 +08:00
Lan 2073f9ba4b Merge pull request #550 from jcbl1/upstream/03-sqlite-pragmas
perf(db): tune SQLite connection pragmas for small VPS
2026-10-25 23:44:12 +08:00
Lan 0a1f08fd6e Merge pull request #549 from jcbl1/upstream/01-web-startup
Optimize performance with caching and startup improvements
2026-10-25 23:44:06 +08:00
Lan 9bf03bc427 Merge pull request #522 from vastsa/dependabot/github_actions/docker/setup-qemu-action-4
chore(deps): bump docker/setup-qemu-action from 3 to 4
2026-10-25 23:39:01 +08:00
Lan 264eb4f986 Merge pull request #521 from vastsa/dependabot/github_actions/actions/upload-pages-artifact-5
chore(deps): bump actions/upload-pages-artifact from 3 to 5
2026-10-25 23:38:52 +08:00
Lan c973d28256 Merge pull request #520 from vastsa/dependabot/github_actions/actions/configure-pages-6
chore(deps): bump actions/configure-pages from 4 to 6
2026-10-25 23:38:46 +08:00
Lan e7f761c35f Merge pull request #519 from vastsa/dependabot/github_actions/googleapis/release-please-action-5
chore(deps): bump googleapis/release-please-action from 4 to 5
2026-10-25 23:38:39 +08:00
Lan 8f7a1e1594 Merge pull request #517 from vastsa/dependabot/github_actions/actions/setup-node-7
chore(deps): bump actions/setup-node from 4 to 7
2026-10-25 23:38:32 +08:00
jcbl1 4f141eeb49 perf(startup): drop duplicate Tortoise init, keep ORM exception handlers 2026-10-24 08:22:41 +00:00
jcbl1 496a7139bd perf(rate-limit): bound tracked IPs and use monotonic clock 2026-10-24 08:22:41 +00:00
jcbl1 5b5d70c3b7 perf(web): cache rendered index.html and add theme asset cache headers 2026-10-24 08:22:41 +00:00
jcbl1 ec714dcd6f perf(db): tune SQLite connection pragmas for small VPS
- synchronous=NORMAL: WAL keeps committed data safe against app crashes
  while skipping an fsync per commit.
- temp_store=MEMORY: temp tables and sorts stay in RAM.
- cache_size=-8000: cap page cache at ~8MB.

Tortoise's SqliteClient applies every non-file_path credential as a PRAGMA
on its single connection, so this is the only place that needs changing.
2026-10-24 08:17:06 +00:00
jcbl1 588af7b6c3 perf(runtime): enable uvloop/httptools and tune container memory/keep-alive
- deps: pin uvloop 0.23.0 (non-Windows marker) and httptools 0.9.0; uvicorn's
  --loop/--http auto picks both up. Lockfile regenerated with constraints so
  only these two packages are added (no transitive version drift).
- Dockerfile: expose TIMEOUT_KEEP_ALIVE (default 5, passed to uvicorn) and
  MALLOC_TRIM_THRESHOLD_=65536 so glibc returns freed heap to the OS after
  large transfers. Defaults keep previous behaviour.
- ci: allow `; marker` pins in the lockfile drift check, otherwise uvloop
  would be silently skipped and never verified.
- docs: document both environment variables (zh/en).
- tests: pin the Dockerfile/requirements/lockfile runtime contract.
2026-10-24 08:16:54 +00:00
jcbl1 080fecf653 perf(db): index expiry, recency and chunk lookup columns
Migration 008 adds:
- filecodes(expired_at) for expiry scans and admin expiry stats
- filecodes(created_at) for the default recency ordering and today/yesterday
  counters
- uploadchunk(upload_id, chunk_index) for chunk status checks and merges;
  its prefix also covers upload_id-only deletes (previously unindexed)

Models mirror the same metadata for the generate_schemas test path.
Upgrade benchmark: 50k filecodes + 20k chunks -> 30ms index build; scans
become index searches.

Refs NOTES batch 4.
2026-10-24 08:16:54 +00:00
vastsa 3d2c8ec709 docs: document MCP support and remove BokeBox link 2026-10-12 18:37:48 +08:00
github-actions[bot] f1fee1edff chore(master): release 2.8.3 (#545)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2026-10-10 13:08:55 +00:00
vastsa 65f4929d40 fix: remove HTML preview endpoint 2026-10-10 21:08:32 +08:00
github-actions[bot] 40f22de7f8 chore(master): release 2.8.2 (#544)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2026-10-10 11:04:19 +00:00
vastsa da374b4595 fix: type MCP protocol results 2026-10-10 19:03:51 +08:00
github-actions[bot] d3f9e7902c chore(master): release 2.8.1 (#543)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2026-10-10 10:57:39 +00:00
vastsa 2f5eab18b1 fix: restore lint and Docker theme build 2026-10-10 18:57:15 +08:00
dependabot[bot] fda470b076 chore(deps): bump docker/setup-qemu-action from 3 to 4
Bumps [docker/setup-qemu-action](https://github.com/docker/setup-qemu-action) from 3 to 4.
- [Release notes](https://github.com/docker/setup-qemu-action/releases)
- [Commits](https://github.com/docker/setup-qemu-action/compare/v3...v4)

---
updated-dependencies:
- dependency-name: docker/setup-qemu-action
  dependency-version: '4'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2026-09-14 13:47:26 +00:00
dependabot[bot] 41e3ce1b9c chore(deps): bump actions/upload-pages-artifact from 3 to 5
Bumps [actions/upload-pages-artifact](https://github.com/actions/upload-pages-artifact) from 3 to 5.
- [Release notes](https://github.com/actions/upload-pages-artifact/releases)
- [Commits](https://github.com/actions/upload-pages-artifact/compare/v3...v5)

---
updated-dependencies:
- dependency-name: actions/upload-pages-artifact
  dependency-version: '5'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2026-09-14 13:47:25 +00:00
dependabot[bot] 31027e6eea chore(deps): bump actions/configure-pages from 4 to 6
Bumps [actions/configure-pages](https://github.com/actions/configure-pages) from 4 to 6.
- [Release notes](https://github.com/actions/configure-pages/releases)
- [Commits](https://github.com/actions/configure-pages/compare/v4...v6)

---
updated-dependencies:
- dependency-name: actions/configure-pages
  dependency-version: '6'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2026-09-14 13:47:21 +00:00
dependabot[bot] d0f08a7be5 chore(deps): bump googleapis/release-please-action from 4 to 5
Bumps [googleapis/release-please-action](https://github.com/googleapis/release-please-action) from 4 to 5.
- [Release notes](https://github.com/googleapis/release-please-action/releases)
- [Changelog](https://github.com/googleapis/release-please-action/blob/main/CHANGELOG.md)
- [Commits](https://github.com/googleapis/release-please-action/compare/v4...v5)

---
updated-dependencies:
- dependency-name: googleapis/release-please-action
  dependency-version: '5'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2026-09-14 13:47:18 +00:00
dependabot[bot] 5646f41255 chore(deps): bump actions/setup-node from 4 to 7
Bumps [actions/setup-node](https://github.com/actions/setup-node) from 4 to 7.
- [Release notes](https://github.com/actions/setup-node/releases)
- [Commits](https://github.com/actions/setup-node/compare/v4...v7)

---
updated-dependencies:
- dependency-name: actions/setup-node
  dependency-version: '7'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2026-09-14 13:47:13 +00:00
59 changed files with 1510 additions and 345 deletions

No files matched your search

+1 -1
View File
@@ -51,7 +51,7 @@ jobs:
run: |
python - <<'PY'
import re, sys
pins = dict(re.findall(r'^([\w-]+)==([\w.]+)$', open('requirements.txt').read(), re.M))
pins = dict(re.findall(r'^([\w-]+)==([\w.]+)(?:\s*;[^#\n]*)?$', open('requirements.txt').read(), re.M))
lock = open('requirements.lock.txt').read()
stale = []
for name, ver in pins.items():
+1 -1
View File
@@ -92,7 +92,7 @@ jobs:
fi
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
uses: docker/setup-qemu-action@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
+3 -3
View File
@@ -37,13 +37,13 @@ jobs:
version: 9
# - uses: oven-sh/setup-bun@v1 # 如果使用 Bun,请取消注释
- name: Setup Node
uses: actions/setup-node@v4
uses: actions/setup-node@v7
with:
node-version: 20
cache: 'pnpm'
cache-dependency-path: 'docs/pnpm-lock.yaml'
- name: Setup Pages
uses: actions/configure-pages@v4
uses: actions/configure-pages@v6
- name: Install dependencies
working-directory: docs
run: pnpm install
@@ -51,7 +51,7 @@ jobs:
working-directory: docs
run: pnpm run docs:build
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
uses: actions/upload-pages-artifact@v5
with:
path: docs/.vitepress/dist
+1 -1
View File
@@ -23,7 +23,7 @@ jobs:
steps:
- name: Create or update release
id: release
uses: googleapis/release-please-action@v4
uses: googleapis/release-please-action@v5
with:
config-file: release-please-config.json
manifest-file: .release-please-manifest.json
+1 -1
View File
@@ -1,3 +1,3 @@
{
".": "2.8.0"
".": "2.9.0"
}
+5 -3
View File
@@ -54,7 +54,7 @@ RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
echo 'Asia/Shanghai' > /etc/timezone
# 从构建阶段复制编译好的前端主题
COPY --from=frontend-builder /build/fronted-2024/dist ./themes/2024
COPY --from=frontend-builder /build/FileCodeBox/themes/2024 ./themes/2024
COPY --from=frontend-builder /build/fronted-2023/dist ./themes/2023
# 安装系统安全更新 + Python 依赖
@@ -73,7 +73,9 @@ ENV HOST="0.0.0.0" \
APP_ENV="production" \
LOG_LEVEL="warning" \
ACCESS_LOG="false" \
FORWARDED_ALLOW_IPS=""
FORWARDED_ALLOW_IPS="" \
TIMEOUT_KEEP_ALIVE=5 \
MALLOC_TRIM_THRESHOLD_=65536
EXPOSE 12345
@@ -81,4 +83,4 @@ EXPOSE 12345
# FORWARDED_ALLOW_IPS 默认为空:仅信任直连 IP,避免任意客户端伪造 X-Forwarded-*。
# 若前面有反向代理,请显式设置为代理网段,例如 "10.0.0.0/8,172.16.0.0/12"。
# 容器以 root 运行:兼容存量 root 属主的数据卷,以及 NAS/只读 bind mount。
CMD ["sh", "-c", "access_log_arg=--no-access-log; if [ \"${APP_ENV:-development}\" != \"production\" ] || [ \"${ACCESS_LOG:-false}\" = \"true\" ]; then access_log_arg=--access-log; fi; exec uvicorn main:app --host \"$HOST\" --port \"$PORT\" --workers \"$WORKERS\" --log-level \"$LOG_LEVEL\" \"$access_log_arg\" --proxy-headers --forwarded-allow-ips \"${FORWARDED_ALLOW_IPS:-}\""]
CMD ["sh", "-c", "access_log_arg=--no-access-log; if [ \"${APP_ENV:-development}\" != \"production\" ] || [ \"${ACCESS_LOG:-false}\" = \"true\" ]; then access_log_arg=--access-log; fi; exec uvicorn main:app --host \"$HOST\" --port \"$PORT\" --workers \"$WORKERS\" --log-level \"$LOG_LEVEL\" \"$access_log_arg\" --timeout-keep-alive \"${TIMEOUT_KEEP_ALIVE:-5}\" --proxy-headers --forwarded-allow-ips \"${FORWARDED_ALLOW_IPS:-}\""]
+1 -1
View File
@@ -1 +1 @@
2.8.0
2.9.0
+51 -25
View File
@@ -1,6 +1,7 @@
import time
from collections import OrderedDict
from ipaddress import ip_address, ip_network
from typing import Dict, Iterable, TypedDict
from datetime import datetime, timedelta
from typing import Iterable, TypedDict
from fastapi import HTTPException, Request
from core.settings import settings
@@ -67,41 +68,66 @@ def get_client_ip(request: Request) -> str:
class _IPRecord(TypedDict):
count: int
time: datetime
last_seen: float
# Upper bound for tracked source IPs per limiter. Without it a scan using
# spoofed/rotating addresses grows the dict forever on a small VPS.
DEFAULT_MAX_TRACKED_IPS = 10_000
class IPRateLimit:
def __init__(self, count: int, minutes: int):
self.ips: Dict[str, _IPRecord] = {}
def __init__(
self,
count: int,
minutes: int,
max_ips: int = DEFAULT_MAX_TRACKED_IPS,
):
# Ordered by last activity (oldest first) so capacity eviction and the
# periodic cleanup are both O(1)/O(expired) instead of a full rebuild.
self.ips: OrderedDict[str, _IPRecord] = OrderedDict()
self.count = count
self.minutes = minutes
self.max_ips = max(1, max_ips)
def _window_seconds(self) -> float:
# minutes is mutable at runtime via apps.base.config._sync_ip_limits.
return float(self.minutes) * 60
def check_ip(self, ip: str) -> bool:
if ip in self.ips:
ip_info = self.ips[ip]
if ip_info["count"] >= self.count:
if ip_info["time"] + timedelta(minutes=self.minutes) > datetime.now():
return False
self.ips.pop(ip)
record = self.ips.get(ip)
if record is None:
return True
if record["count"] >= self.count:
if record["last_seen"] + self._window_seconds() > time.monotonic():
return False
self.ips.pop(ip, None)
return True
def add_ip(self, ip: str) -> int:
ip_info = self.ips.setdefault(
ip, {"count": 0, "time": datetime.now()}
)
ip_info["count"] += 1
ip_info["time"] = datetime.now()
self.ips[ip] = ip_info
return ip_info["count"]
now = time.monotonic()
record = self.ips.get(ip)
if record is None:
record = {"count": 0, "last_seen": now}
self.ips[ip] = record
else:
self.ips.move_to_end(ip)
record["count"] += 1
record["last_seen"] = now
while len(self.ips) > self.max_ips:
# Evict the least recently active IP; fail-open for the evicted
# address is preferable to unbounded memory growth.
self.ips.popitem(last=False)
return record["count"]
async def remove_expired_ip(self) -> None:
now = datetime.now()
expiration = timedelta(minutes=self.minutes)
self.ips = {
ip: info
for ip, info in self.ips.items()
if info["time"] + expiration >= now
}
now = time.monotonic()
window = self._window_seconds()
while self.ips:
_, record = next(iter(self.ips.items()))
if record["last_seen"] + window >= now:
break
self.ips.popitem(last=False)
def __call__(self, request: Request) -> str:
ip = get_client_ip(request)
+2 -4
View File
@@ -13,12 +13,11 @@ from __future__ import annotations
import time
from pathlib import Path
from urllib.parse import quote
from fastapi import HTTPException
from core.errors import StorageError
from core.storage import StoredDownload
from core.storage import StoredDownload, build_attachment_headers
LOCAL_REF_MARKER = "local-ref"
MAX_RELPATH_LEN = 255
@@ -97,8 +96,7 @@ def build_local_ref_download(file_code) -> StoredDownload:
filename = f"{getattr(file_code, 'prefix', '') or ''}{getattr(file_code, 'suffix', '') or ''}"
if not filename:
filename = path.name
encoded_filename = quote(filename, safe="")
headers = {"Content-Disposition": f"attachment; filename*=UTF-8''{encoded_filename}"}
headers = build_attachment_headers(filename)
try:
headers["Content-Length"] = str(path.stat().st_size)
except OSError:
+2 -73
View File
@@ -4,7 +4,7 @@ import binascii
import html
import io
import json
from html.parser import HTMLParser
from typing import Any
from urllib.parse import quote, urlsplit
from fastapi import APIRouter, HTTPException, Request, UploadFile
@@ -191,6 +191,7 @@ async def mcp_endpoint(request: Request):
method = message["method"]
if request_id is None and method.startswith("notifications/"):
return Response(status_code=202)
result: dict[str, Any] = {}
try:
if method == "initialize":
result = {
@@ -228,75 +229,3 @@ async def mcp_endpoint(request: Request):
_jsonrpc_error(request_id, -32001 if exc.status_code == 401 else -32003, str(exc.detail)),
status_code=exc.status_code,
)
_SAFE_TAGS = {
"a", "abbr", "article", "b", "blockquote", "br", "caption", "code", "dd", "del", "details",
"div", "dl", "dt", "em", "figcaption", "figure", "h1", "h2", "h3", "h4", "h5", "h6",
"hr", "i", "img", "li", "main", "mark", "ol", "p", "pre", "s", "section", "small", "span",
"strong", "sub", "summary", "sup", "table", "tbody", "td", "th", "thead", "tr", "u", "ul",
}
_VOID_TAGS = {"br", "hr", "img"}
_DROP_CONTENT_TAGS = {"script", "style", "iframe", "object", "embed", "svg", "math", "form", "template"}
_SAFE_ATTRS = {"alt", "title", "class", "id", "colspan", "rowspan", "width", "height", "scope", "dir", "lang"}
class _HTMLPreviewSanitizer(HTMLParser):
"""Whitelist-only HTML serializer; scripts, active embeds and URL attrs are removed."""
def __init__(self):
super().__init__(convert_charrefs=True)
self.output: list[str] = []
self.drop_depth = 0
def handle_starttag(self, tag, attrs):
tag = tag.lower()
if self.drop_depth:
if tag in _DROP_CONTENT_TAGS:
self.drop_depth += 1
return
if tag in _DROP_CONTENT_TAGS:
self.drop_depth = 1
return
if tag not in _SAFE_TAGS:
return
safe_attrs = []
for name, value in attrs:
name = name.lower()
if value is None or name.startswith("on") or name not in _SAFE_ATTRS:
continue
safe_attrs.append(f' {name}="{html.escape(value, quote=True)}"')
self.output.append(f"<{tag}{''.join(safe_attrs)}>")
def handle_startendtag(self, tag, attrs):
self.handle_starttag(tag, attrs)
if tag.lower() in _SAFE_TAGS and tag.lower() not in _VOID_TAGS:
self.handle_endtag(tag)
def handle_endtag(self, tag):
tag = tag.lower()
if self.drop_depth:
if tag in _DROP_CONTENT_TAGS:
self.drop_depth -= 1
return
if tag in _SAFE_TAGS and tag not in _VOID_TAGS:
self.output.append(f"</{tag}>")
def handle_data(self, data):
if not self.drop_depth:
self.output.append(html.escape(data))
def handle_entityref(self, name):
if not self.drop_depth:
self.output.append(f"&amp;{html.escape(name)};")
def handle_charref(self, name):
if not self.drop_depth:
self.output.append(f"&amp;#{html.escape(name)};")
def sanitize_html_preview(source: str) -> str:
parser = _HTMLPreviewSanitizer()
parser.feed(source)
parser.close()
return "<!doctype html><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width,initial-scale=1\"><body>" + "".join(parser.output) + "</body>"
+26
View File
@@ -0,0 +1,26 @@
from tortoise import connections
# 高频查询字段索引。真实部署的 schema 全部由迁移建立
# (init_db 不调用 generate_schemas),因此索引必须写在迁移里;
# apps/base/models.py 中的 index/Meta 仅用于测试的 generate_schemas 路径。
#
# - filecodes(expired_at):过期扫描(delete_expire_files)与后台过期统计。
# - filecodes(created_at):后台默认倒序列表、首页最近记录、今日/昨日统计。
# - uploadchunk(upload_id, chunk_index):分片状态检查与合并;其前缀同时
# 覆盖仅按 upload_id 的删除/清理查询(原先没有对应索引)。
# 已知缺口:clean_incomplete_uploads 的 chunk_index=-1 AND created_at<? 用不上
# 该复合索引,待清理任务改为分批查询后再评估。
async def migrate():
conn = connections.get("default")
await conn.execute_script(
"""
CREATE INDEX IF NOT EXISTS idx_filecodes_expired_at
ON filecodes (expired_at);
CREATE INDEX IF NOT EXISTS idx_filecodes_created_at
ON filecodes (created_at);
CREATE INDEX IF NOT EXISTS idx_uploadchunk_upload_chunk
ON uploadchunk (upload_id, chunk_index);
"""
)
+10 -3
View File
@@ -21,12 +21,14 @@ class FileCodes(models.Model):
file_path = fields.CharField(max_length=255, null=True)
size = fields.BigIntField(default=0)
text = fields.TextField(null=True)
expired_at = fields.DatetimeField(null=True)
# 过期扫描与后台过期统计的高频过滤列
expired_at = fields.DatetimeField(null=True, index=True)
# -1 = 时间式(看 expired_at),<=0 的 0 = 次数耗尽。默认 -1 而非 0:
# 0 语义是"次数已用完即过期",忘传该字段的创建路径会得到立即过期的记录。
expired_count = fields.IntField(default=-1)
used_count = fields.IntField(default=0)
created_at = fields.DatetimeField(auto_now_add=True)
# 后台默认按创建时间倒序、今日/昨日统计均落在该列
created_at = fields.DatetimeField(auto_now_add=True, index=True)
file_hash = fields.CharField(max_length=64, null=True)
is_chunked = fields.BooleanField(default=False)
upload_id = fields.CharField(max_length=36, null=True)
@@ -44,7 +46,9 @@ class FileCodes(models.Model):
class UploadChunk(models.Model):
id = fields.IntField(pk=True)
upload_id = fields.CharField(max_length=36, index=True)
# 上传会话/分片查询始终同时带 upload_id;复合索引前缀已覆盖仅按 upload_id
# 的删除与清理查询,单列索引会造成重复。
upload_id = fields.CharField(max_length=36)
chunk_index = fields.IntField()
chunk_hash = fields.CharField(max_length=64)
total_chunks = fields.IntField()
@@ -55,6 +59,9 @@ class UploadChunk(models.Model):
created_at = fields.DatetimeField(auto_now_add=True)
completed = fields.BooleanField(default=False)
class Meta:
indexes = [("upload_id", "chunk_index")]
class KeyValue(Model):
# 与 FileCodes 保持同一风格:类属性不写值注解(等号右侧的 Field 描述符
+72 -8
View File
@@ -1,6 +1,9 @@
"""Public-facing pages and routes: setup wizard, theme assets, index,
robots.txt, and the public config endpoints."""
import html
import re
from functools import lru_cache
from pathlib import Path
from fastapi import APIRouter, HTTPException, Request
from fastapi.responses import FileResponse, HTMLResponse, JSONResponse, RedirectResponse
@@ -83,9 +86,58 @@ def resolve_theme_file(*parts: str):
return file_path
# Vite 产物带内容指纹(如 index-BX2s1QqP.js),内容变化时文件名必然变化,
# 可以安全地长缓存;public/ 下的未哈希资源原地升级时名字不变,只给短缓存,
# 过期后靠 FileResponse 自带的 ETag/Last-Modified 走 304。
_HASHED_ASSET_RE = re.compile(
r"-[A-Za-z0-9_-]{8,}\.(?:js|mjs|css|map|woff2?|ttf|otf|eot|svg|png|jpe?g|gif|webp|avif|ico)$",
re.IGNORECASE,
)
_IMMUTABLE_ASSET_CACHE = "public, max-age=31536000, immutable"
_DEFAULT_ASSET_CACHE = "public, max-age=86400"
def _asset_cache_control(asset_path: str) -> str:
if _HASHED_ASSET_RE.search(asset_path):
return _IMMUTABLE_ASSET_CACHE
return _DEFAULT_ASSET_CACHE
@lru_cache(maxsize=8)
def _render_index_html(
template_path: str,
inode: int,
mtime_ns: int,
size: int,
title: str,
description: str,
keywords: str,
opacity: str,
background: str,
) -> str:
"""Render index.html once per (theme file version, site config) pair.
The stat fields keep the cache honest when a theme is replaced in place;
config values are part of the key so admin edits are visible immediately
without any cross-module invalidation hook.
"""
return (
Path(template_path)
.read_text(encoding="utf-8")
.replace("{{title}}", html.escape(title))
.replace("{{description}}", html.escape(description))
.replace("{{keywords}}", html.escape(keywords))
.replace("{{opacity}}", html.escape(opacity))
.replace("{{background}}", html.escape(background))
)
@router.get("/assets/{asset_path:path}", include_in_schema=False)
async def theme_asset(asset_path: str):
return FileResponse(resolve_theme_file("assets", asset_path))
return FileResponse(
resolve_theme_file("assets", asset_path),
headers={"Cache-Control": _asset_cache_control(asset_path)},
)
async def not_found_handler(request, exc=None):
@@ -115,14 +167,26 @@ async def index(request=None, exc=None):
# Site config is admin input (and during the setup window anyone can claim it);
# always escape before injecting into the theme template to prevent stored XSS
# (mirrors the setup page).
template_path = resolve_theme_file("index.html")
try:
stat_result = template_path.stat()
except OSError:
# resolve_theme_file 与 stat 之间文件被删除/替换时仍按 404 处理,
# 不能退回缓存内容。
raise HTTPException(status_code=404, detail="资源不存在")
content = _render_index_html(
str(template_path),
stat_result.st_ino,
stat_result.st_mtime_ns,
stat_result.st_size,
str(settings.name),
str(settings.description),
str(settings.keywords),
str(settings.opacity),
str(settings.background),
)
return HTMLResponse(
content=resolve_theme_file("index.html")
.read_text(encoding="utf-8")
.replace("{{title}}", html.escape(str(settings.name)))
.replace("{{description}}", html.escape(str(settings.description)))
.replace("{{keywords}}", html.escape(str(settings.keywords)))
.replace("{{opacity}}", html.escape(str(settings.opacity)))
.replace("{{background}}", html.escape(str(settings.background))),
content=content,
media_type="text/html",
headers={"Cache-Control": "no-cache"},
)
+38 -18
View File
@@ -9,6 +9,7 @@ import os
from fastapi import HTTPException, UploadFile
from fastapi.responses import FileResponse, Response, StreamingResponse
from tortoise.transactions import in_transaction
from core.logger import logger
from core.settings import settings
@@ -210,13 +211,15 @@ class FileUploadService:
失败路径的配额释放与清理范围与原实现逐一对齐:
完整性校验失败仅抛 400(预留由 TTL 兜底);合并失败清理分片文件后抛 500。
"""
if chunk_info.completed:
raise HTTPException(409, "上传会话已完成,仅待清理")
storage = FileUploadService._storage()
await reserve_storage(
f"chunk:{upload_id}", chunk_info.file_size, ttl_seconds=chunk_reservation_ttl()
)
completed_chunks = await UploadChunk.filter(
upload_id=upload_id, completed=True
upload_id=upload_id, completed=True, chunk_index__gte=0
).all()
if len(completed_chunks) != chunk_info.total_chunks:
raise HTTPException(400, "分片不完整")
@@ -225,16 +228,19 @@ class FileUploadService:
max_total_size = len(completed_chunks) * chunk_info.chunk_size
if max_total_size > settings.upload_size:
save_path = chunk_info.save_path
cleanup_succeeded = True
if save_path:
try:
await storage.clean_chunks(upload_id, save_path)
except Exception:
cleanup_succeeded = False
logger.warning(
"分片超限中止:清理分片文件失败 upload_id=%s",
upload_id,
exc_info=True,
)
await UploadChunk.filter(upload_id=upload_id).delete()
if cleanup_succeeded:
await UploadChunk.filter(upload_id=upload_id).delete()
await release_storage(f"chunk:{upload_id}")
max_size_mb = settings.upload_size / (1024 * 1024)
raise HTTPException(
@@ -258,22 +264,36 @@ class FileUploadService:
expired_at, expired_count, used_count, code = await get_expire_info(
expire_value, expire_style
)
await FileCodes.create(
code=code,
file_hash=file_hash, # 使用合并后计算的哈希
is_chunked=True,
upload_id=upload_id,
size=chunk_info.file_size,
expired_at=expired_at,
expired_count=expired_count,
used_count=used_count,
file_path=path,
uuid_file_name=safe_file_name,
prefix=prefix,
suffix=suffix,
)
await storage.clean_chunks(upload_id, save_path)
await UploadChunk.filter(upload_id=upload_id).delete()
# 发布记录与会话终态一起提交;即使分享过期删除,会话也不能被复用。
async with in_transaction() as connection:
await FileCodes.create(
code=code,
file_hash=file_hash, # 使用合并后计算的哈希
is_chunked=True,
upload_id=upload_id,
size=chunk_info.file_size,
expired_at=expired_at,
expired_count=expired_count,
used_count=used_count,
file_path=path,
uuid_file_name=safe_file_name,
prefix=prefix,
suffix=suffix,
using_db=connection,
)
chunk_info.completed = True
await chunk_info.save(using_db=connection, update_fields=["completed"])
try:
await storage.clean_chunks(upload_id, save_path)
except Exception:
# 分享已创建:不把清理故障伪装成合并失败,保留会话供定时任务重试。
logger.warning(
"分片合并完成:清理临时分片文件失败 upload_id=%s",
upload_id,
exc_info=True,
)
else:
await UploadChunk.filter(upload_id=upload_id).delete()
await release_storage(f"chunk:{upload_id}")
return {"code": code, "name": safe_file_name}
except ValueError as e:
+1
View File
@@ -45,6 +45,7 @@ def build_public_config() -> dict:
"notify_content": settings.notify_content,
"show_admin_address": normalize_public_flag(settings.show_admin_addr),
"max_save_seconds": settings.max_save_seconds,
"background": str(settings.background or ""),
}
+2
View File
@@ -80,6 +80,8 @@ async def clean_incomplete_uploads():
logger.error(
f"清理分片文件失败 upload_id={session.upload_id}: {e}"
)
# 存储清理失败(包括部分删除)时保留记录,下轮继续重试。
continue
try:
await UploadChunk.filter(upload_id=session.upload_id).delete()
+14 -56
View File
@@ -1,16 +1,16 @@
import hashlib
import asyncio
import hmac
import os
import uuid
from datetime import timedelta
from urllib.parse import quote, unquote
from urllib.parse import unquote
from typing import Optional, Tuple, Union
from fastapi import APIRouter, Form, Request, UploadFile, File, Depends, HTTPException
from pydantic import BaseModel, ValidationError
from starlette.responses import HTMLResponse, Response
from starlette import status
from starlette.responses import Response
from tortoise.expressions import Case, F, Q, When
from apps.base.auth import share_required_login
@@ -24,7 +24,7 @@ from apps.base.services import (
stored_file_of,
validate_file_size,
)
from core.storage import StoredFile
from core.storage import StoredFile, build_attachment_headers
from core.logger import logger
from apps.base.schemas import (
SelectFileModel,
@@ -39,7 +39,6 @@ from apps.base.utils import (
validate_expire_style,
)
from apps.base.local_share import is_local_ref
from apps.base.mcp import sanitize_html_preview
from core.response import APIResponse
from core.settings import settings
from core.storage import storages, FileStorageInterface
@@ -195,49 +194,6 @@ async def post_file_metadata(
return APIResponse(detail=build_file_metadata(file_code))
@share_api.get("/preview/html/{code}", include_in_schema=False)
async def preview_html(code: str):
has, file_code = await get_code_file_by_code(code)
if not has or not isinstance(file_code, FileCodes):
raise HTTPException(status_code=404, detail="文件不存在")
if file_code.text is not None or (file_code.suffix or "").lower() not in {".html", ".htm"}:
raise HTTPException(status_code=415, detail="仅支持 HTML 文件预览")
preview_limit = 2 * 1024 * 1024
if file_code.size > preview_limit:
raise HTTPException(status_code=413, detail="HTML 文件过大,无法在线预览")
download = await get_stored_download(file_code)
if download.path is not None:
if download.path.stat().st_size > preview_limit:
raise HTTPException(status_code=413, detail="HTML 文件过大,无法在线预览")
content = await asyncio.to_thread(download.path.read_bytes)
elif download.content is not None:
content = download.content
elif download.stream_factory is not None:
stream = download.stream_factory()
if hasattr(stream, "__aiter__"):
chunks = bytearray()
async for chunk in stream:
chunks.extend(chunk)
if len(chunks) > preview_limit:
raise HTTPException(status_code=413, detail="HTML 文件过大,无法在线预览")
content = bytes(chunks)
else:
raise HTTPException(status_code=500, detail="HTML 预览读取失败")
else:
raise HTTPException(status_code=500, detail="HTML 预览读取失败")
if len(content) > preview_limit:
raise HTTPException(status_code=413, detail="HTML 文件过大,无法在线预览")
safe_html = sanitize_html_preview(content.decode("utf-8-sig", errors="replace"))
return HTMLResponse(
safe_html,
headers={
"Content-Security-Policy": "sandbox; default-src 'none'; img-src data:; style-src 'unsafe-inline'; font-src data:; base-uri 'none'; form-action 'none'",
"X-Content-Type-Options": "nosniff",
"Cache-Control": "private, no-store",
"Content-Disposition": 'inline; filename="preview.html"',
},
background=download.background,
)
@share_api.get("/select/")
@@ -256,11 +212,7 @@ async def get_code_file(code: str, ip: str = Depends(ip_limit["error"])):
return Response(
content=file_code.text,
media_type="text/plain",
headers={
"Content-Disposition": (
f"attachment; filename*=UTF-8''{quote(filename, safe='')}"
)
},
headers=build_attachment_headers(filename),
)
return response_from_download(await get_stored_download(file_code, file_storage))
@@ -357,6 +309,7 @@ async def init_chunk_upload(data: InitChunkUploadModel = Depends(parse_init_chun
existing_session = await UploadChunk.filter(
chunk_hash=data.file_hash,
chunk_index=-1,
completed=False,
file_size=data.file_size,
file_name=safe_file_name,
).first()
@@ -375,7 +328,7 @@ async def init_chunk_upload(data: InitChunkUploadModel = Depends(parse_init_chun
* 3600,
)
uploaded_chunks = await UploadChunk.filter(
upload_id=existing_session.upload_id, completed=True
upload_id=existing_session.upload_id, completed=True, chunk_index__gte=0
).values_list("chunk_index", flat=True)
return APIResponse(
detail={
@@ -435,6 +388,8 @@ async def upload_chunk(
chunk_info = await UploadChunk.filter(upload_id=upload_id, chunk_index=-1).first()
if not chunk_info:
raise HTTPException(status.HTTP_404_NOT_FOUND, detail="上传会话不存在")
if chunk_info.completed:
raise HTTPException(409, "上传会话已完成,仅待清理")
await reserve_storage(
f"chunk:{upload_id}",
chunk_info.file_size,
@@ -469,7 +424,7 @@ async def upload_chunk(
# 计算已上传分片数,校验累计大小不超限(用分片数 * chunk_size 估算)
uploaded_count = await UploadChunk.filter(
upload_id=upload_id, completed=True
upload_id=upload_id, completed=True, chunk_index__gte=0
).count()
# 已上传分片的最大可能大小 + 当前分片
max_uploaded_size = uploaded_count * chunk_info.chunk_size + chunk_size
@@ -527,6 +482,7 @@ async def cancel_upload(upload_id: str):
await storage.clean_chunks(upload_id, save_path)
except Exception:
logger.warning("取消分片上传:清理分片文件失败 upload_id=%s", upload_id, exc_info=True)
raise HTTPException(503, "分片清理失败,请稍后重试")
# 清理数据库记录
await UploadChunk.filter(upload_id=upload_id).delete()
@@ -546,7 +502,7 @@ async def get_upload_status(upload_id: str):
# 获取已上传的分片列表
uploaded_chunks = await UploadChunk.filter(
upload_id=upload_id, completed=True
upload_id=upload_id, completed=True, chunk_index__gte=0
).values_list("chunk_index", flat=True)
return APIResponse(
@@ -574,6 +530,8 @@ async def complete_upload(
chunk_info = await UploadChunk.filter(upload_id=upload_id, chunk_index=-1).first()
if not chunk_info:
raise HTTPException(status.HTTP_404_NOT_FOUND, detail="上传会话不存在")
if chunk_info.completed:
raise HTTPException(409, "上传会话已完成,仅待清理")
validate_expire_style(data.expire_style)
detail = await FileUploadService.complete_chunked_upload(
upload_id, chunk_info, data.expire_value, data.expire_style
+11
View File
@@ -25,6 +25,17 @@ def get_db_config() -> dict:
"journal_mode": "WAL",
"busy_timeout": 10000,
"foreign_keys": "ON",
# 小内存 VPS 下的 IOPS 与内存折中。Tortoise 的 SqliteClient 会把
# credentials 中 file_path 以外的键逐条执行成 PRAGMA,且 SQLite
# 后端只维持单条连接,因此这些设置在进程生命周期内全局生效。
# synchronous=NORMAL:WAL 下应用崩溃不丢已提交事务,
# 仅 OS/断电可能丢最后若干事务,省去每次提交的 fsync。
# temp_store=MEMORY:临时表与排序走内存,小库收益明显;
# 后台大排序的内存峰值需在压测中关注。
# cache_size=-8000:page cache 上限约 8MB(负数单位为 KiB)。
"synchronous": "NORMAL",
"temp_store": "MEMORY",
"cache_size": -8000,
},
}
},
+9 -1
View File
@@ -3,6 +3,7 @@
# @File : storage.py
# @Software: PyCharm
import hashlib
import re
from typing import BinaryIO, Optional
from urllib.parse import quote
@@ -66,8 +67,15 @@ def build_attachment_headers(filename: str, content_length=None) -> dict:
(/share/download)因此永不内联渲染 HTML/SVG。此函数是唯一构造点,
新增后端必须复用(tests/test_attachment_guard.py 有源码级 tripwire)。
"""
# Older Android download managers ignore filename*. Keep an ASCII fallback
# with the extension, without allowing quoted-string/header injection.
fallback = re.sub(r"[^A-Za-z0-9._ -]", "_", filename).strip(" .") or "download"
encoded_filename = quote(filename, safe="")
headers = {"Content-Disposition": f"attachment; filename*=UTF-8''{encoded_filename}"}
headers = {
"Content-Disposition": (
f'attachment; filename="{fallback}"; filename*=UTF-8\'\'{encoded_filename}'
)
}
if content_length is not None:
headers["Content-Length"] = str(content_length)
return headers
+14 -15
View File
@@ -45,7 +45,12 @@ class S3FileStorage(FileStorageInterface):
self.endpoint_url = settings.s3_endpoint_url
def _client_config(self) -> Config:
config = {"signature_version": self.signature_version}
# OSS 等兼容服务不支持默认 flexible checksum 的 aws-chunked trailer。
config = {
"signature_version": self.signature_version,
"request_checksum_calculation": "when_required",
"response_checksum_validation": "when_required",
}
s3_config = {}
if self.addressing_style in {"path", "virtual", "auto"}:
s3_config["addressing_style"] = self.addressing_style
@@ -277,21 +282,15 @@ class S3FileStorage(FileStorageInterface):
:param upload_id: 上传会话ID
:param save_path: 文件保存路径
"""
chunk_dir = str(Path(save_path).parent / "chunks" / upload_id)
# 尾部斜杠防止误删 upload_id 相同前缀的其他会话。
chunk_dir = str(Path(save_path).parent / "chunks" / upload_id) + "/"
async with self._client() as s3:
try:
# 列出并删除所有分片对象
paginator = s3.get_paginator('list_objects_v2')
async for page in paginator.paginate(Bucket=self.bucket_name, Prefix=chunk_dir):
objects = page.get('Contents', [])
if objects:
delete_objects = [{'Key': obj['Key']} for obj in objects]
await s3.delete_objects(
Bucket=self.bucket_name,
Delete={'Objects': delete_objects}
)
except Exception as e:
logger.warning(f"清理 S3 分片数据时出错: {e}")
paginator = s3.get_paginator("list_objects_v2")
async for page in paginator.paginate(Bucket=self.bucket_name, Prefix=chunk_dir):
for obj in page.get("Contents", []):
# OSS 批量删除要求 Content-MD5;单对象删除不依赖该扩展。
# 失败向调用方传播,保留会话供重试,不能当作清理成功。
await s3.delete_object(Bucket=self.bucket_name, Key=obj["Key"])
async def generate_presigned_upload_url(self, save_path: str, expires_in: int = 900) -> Optional[str]:
"""
+1 -1
View File
@@ -1,7 +1,7 @@
version: "3"
services:
file-code-box:
image: lanol/filecodebox:2.8.0 # x-release-please-version
image: lanol/filecodebox:2.9.0 # x-release-please-version
volumes:
- fcb-data:/app/data:rw
# NAS 已有文件:把共享目录挂到 /app/data/local,即可在后台「本地分享」里生成提取码,无需再上传
+4
View File
@@ -31,6 +31,7 @@ export default defineConfig({
{ text: '上传与分享', link: '/guide/upload' },
{ text: '管理面板', link: '/guide/management' },
{ text: '存储配置', link: '/guide/storage' },
{ text: 'MCP 接入', link: '/guide/mcp' },
],
},
{ text: 'API', link: '/api/' },
@@ -64,6 +65,7 @@ export default defineConfig({
{ text: '文件上传', link: '/guide/upload' },
{ text: '文件分享', link: '/guide/share' },
{ text: '文件管理', link: '/guide/management' },
{ text: 'MCP 接入', link: '/guide/mcp' },
],
},
{
@@ -148,6 +150,7 @@ export default defineConfig({
{ text: 'Getting Started', link: '/en/guide/getting-started' },
{ text: 'Upload & Share', link: '/en/guide/upload' },
{ text: 'Admin Panel', link: '/en/guide/management' },
{ text: 'MCP Integration', link: '/en/guide/mcp' },
{ text: 'Storage', link: '/en/guide/storage' },
],
},
@@ -185,6 +188,7 @@ export default defineConfig({
{ text: 'File Upload', link: '/en/guide/upload' },
{ text: 'File Sharing', link: '/en/guide/share' },
{ text: 'File Management', link: '/en/guide/management' },
{ text: 'MCP Integration', link: '/en/guide/mcp' },
],
},
{
+46
View File
@@ -1,5 +1,51 @@
# Changelog
## [2.9.0](https://github.com/vastsa/FileCodeBox/compare/v2.8.5...v2.9.0) (2026-11-16)
### Features
* expose background in public config for 2024 theme ([#546](https://github.com/vastsa/FileCodeBox/issues/546)) ([f9cc16c](https://github.com/vastsa/FileCodeBox/commit/f9cc16c805757dc4dcf198456f4a5e84922b9b8f))
## [2.8.5](https://github.com/vastsa/FileCodeBox/compare/v2.8.4...v2.8.5) (2026-10-27)
### Bug Fixes
* resolve OSS chunk cleanup and legacy download issues ([381a539](https://github.com/vastsa/FileCodeBox/commit/381a539c92ba85681a1e820a4150fff17984147a))
## [2.8.4](https://github.com/vastsa/FileCodeBox/compare/v2.8.3...v2.8.4) (2026-10-25)
### Performance Improvements
* **db:** tune SQLite connection pragmas for small VPS ([2073f9b](https://github.com/vastsa/FileCodeBox/commit/2073f9ba4b6d17e2a9c81832c7d77468e9a15814))
* **db:** tune SQLite connection pragmas for small VPS ([ec714dc](https://github.com/vastsa/FileCodeBox/commit/ec714dcd6fec2dc2097d05a250ea3962d01d66aa))
* **rate-limit:** bound tracked IPs and use monotonic clock ([496a713](https://github.com/vastsa/FileCodeBox/commit/496a7139bdb2cdac7f516986b613715a693fff13))
* **startup:** drop duplicate Tortoise init, keep ORM exception handlers ([4f141ee](https://github.com/vastsa/FileCodeBox/commit/4f141eeb4940311ad6dcfe2bc11650fa505f825b))
* **web:** cache rendered index.html and add theme asset cache headers ([5b5d70c](https://github.com/vastsa/FileCodeBox/commit/5b5d70c3b7eb3b8efb3d77d731c860e88d1e7a92))
## [2.8.3](https://github.com/vastsa/FileCodeBox/compare/v2.8.2...v2.8.3) (2026-10-10)
### Bug Fixes
* remove HTML preview endpoint ([65f4929](https://github.com/vastsa/FileCodeBox/commit/65f4929d405408e1c9e58127e0097f8b78b1819f))
## [2.8.2](https://github.com/vastsa/FileCodeBox/compare/v2.8.1...v2.8.2) (2026-10-10)
### Bug Fixes
* type MCP protocol results ([da374b4](https://github.com/vastsa/FileCodeBox/commit/da374b459537ee079e1d03da121a935c8fb18f0e))
## [2.8.1](https://github.com/vastsa/FileCodeBox/compare/v2.8.0...v2.8.1) (2026-10-10)
### Bug Fixes
* restore lint and Docker theme build ([2f5eab1](https://github.com/vastsa/FileCodeBox/commit/2f5eab18b1ab28fcb4f416248076ce574998d718))
## [2.8.0](https://github.com/vastsa/FileCodeBox/compare/v2.7.1...v2.8.0) (2026-10-10)
+1 -1
View File
@@ -1,6 +1,6 @@
## [Unreleased]
<!-- Current release: 2.8.0 x-release-please-version -->
<!-- Current release: 2.9.0 x-release-please-version -->
### Added
+1 -1
View File
@@ -6,7 +6,7 @@ FileCodeBox provides rich configuration options. Prefer the admin panel; directl
FileCodeBox supports two configuration methods:
1. **Admin Panel Configuration** (Recommended): Access `/admin` to enter the admin panel and modify settings on the settings page
1. **Admin Panel Configuration** (Recommended): Access `/#/admin` to enter the admin panel and modify settings on the settings page
2. **Database Configuration**: Configuration is stored in the `data/filecodebox.db` database
::: tip Note
+2
View File
@@ -39,6 +39,8 @@ The named volume persists configuration and uploaded files across container upgr
| `APP_ENV` | `production` (Docker) | Runtime environment; production disables HTTP access logs by default |
| `LOG_LEVEL` | `warning` (Docker) | `debug`, `info`, `warning`, or `error` |
| `ACCESS_LOG` | `false` (production) | Enable HTTP access logs; set to `true` when debugging requests |
| `TIMEOUT_KEEP_ALIVE` | `5` | HTTP Keep-Alive idle timeout in seconds; raise it behind a reverse proxy to reduce reconnects |
| `MALLOC_TRIM_THRESHOLD_` | `65536` | glibc heap trim threshold in bytes; lowers resident memory after large transfers (glibc images only) |
Production deployments output only warning, error, and other key events by default. Docker retains at most three 10 MB log files.
+1 -1
View File
@@ -179,7 +179,7 @@ python main.py
3. Download file or view text
### Admin Panel
1. Visit `/admin`
1. Visit `/#/admin`
2. Enter admin password
3. Manage files and settings
+3 -3
View File
@@ -10,10 +10,10 @@ FileCodeBox provides a fully-featured admin panel that allows administrators to
### Login Method
The admin panel is located at the `/admin` path. Access method:
The admin panel is located at `/#/admin` (a frontend hash route). Access method:
1. On first run, set the admin password on the setup page
2. Visit `http://your-domain.com/admin` in your browser
2. Visit `http://your-domain.com/#/admin` in your browser
3. Enter the admin password (the value of the `admin_token` configuration)
4. Click the login button
@@ -30,7 +30,7 @@ By default, the admin panel entry is not shown on the homepage. You can control
| `show_admin_addr` | int | `0` | Show admin entry on homepage (1=show, 0=hide) |
::: warning Security Recommendation
For public services, it's recommended to keep `show_admin_addr` at `0` and access the admin panel directly via the `/admin` path to reduce the risk of malicious scanning.
For public services, keep `show_admin_addr` at `0` and access the admin panel via `/#/admin`. Hiding the link is not access control; a strong password is still required.
:::
### Authentication Mechanism
+24
View File
@@ -0,0 +1,24 @@
# MCP Integration
FileCodeBox provides a **Model Context Protocol (MCP)** endpoint over Streamable HTTP. Compatible AI clients can create text or file shares through it.
## Connection
Add a remote HTTP server in your MCP client:
- **Transport**: Streamable HTTP
- **Server URL**: `http://<FileCodeBox host>:12345/mcp`
- **Authentication**: no token required by default
For a local instance, use `http://localhost:12345/mcp`. If the administrator disables guest uploads or enables MCP token validation in the admin settings, configure `Authorization: Bearer <admin token>` in the client. Never commit the token to public configuration or source code.
## Available tools
- `share_text`: create a text share. Required: `text`; optional: `expire_value`, `expire_style`.
- `share_file`: upload a Base64-encoded file and create a share. Required: `filename`, `content_base64`; optional: `content_type`, `expire_value`, `expire_style`.
`expire_style` accepts `day`, `hour`, `minute`, `forever`, or `count`; `expire_value` must be greater than zero. Successful calls return a share code, URL, and Markdown link.
## Security and limits
MCP uploads follow FileCodeBox's guest-upload switch, admin-token validation, upload-size and file-type limits, and upload rate limits. Expose the endpoint only to trusted AI clients. Use HTTPS for public deployments and keep the admin token secure.
+2 -2
View File
@@ -12,7 +12,7 @@ FileCodeBox does not ship with a default admin password. Open the site in your b
Change the admin password through the admin panel:
1. Access `/admin` to enter the admin panel
1. Access `/#/admin` to enter the admin panel
2. Log in with the current password
3. Go to the "System Settings" page
4. Find the `admin_token` configuration item
@@ -41,7 +41,7 @@ By default, the admin panel entry is hidden. You can control whether to show the
| `show_admin_addr` | int | `0` | Show admin entry (1=show, 0=hide) |
::: tip Recommendation
For public services, it's recommended to keep `show_admin_addr` at `0` and access the admin panel directly via the `/admin` path.
For public services, it's recommended to keep `show_admin_addr` at `0` and access the admin panel directly via `/#/admin`.
:::
## IP Rate Limiting
+6 -2
View File
@@ -52,6 +52,7 @@ Supports all S3-compatible object storage services, including AWS S3, Aliyun OSS
| `s3_endpoint_url` | string | `""` | S3 endpoint URL |
| `s3_region_name` | string | `auto` | Region name |
| `s3_signature_version` | string | `s3v2` | Signature version (`s3v2` or `s3v4`) |
| `s3_addressing_style` | string | `auto` | Addressing style (`auto`, `path`, or `virtual`); use `virtual` for OSS |
| `s3_hostname` | string | `""` | S3 hostname (alternative) |
| `s3_proxy` | int | `0` | Download through server proxy (1=yes, 0=no) |
| `aws_session_token` | string | `""` | AWS session token (optional) |
@@ -76,13 +77,16 @@ file_storage=s3
s3_access_key_id=YourAccessKeyId
s3_secret_access_key=YourSecretAccessKey
s3_bucket_name=bucket-name
s3_endpoint_url=https://bucket-name.oss-cn-hangzhou.aliyuncs.com
s3_endpoint_url=https://oss-cn-hangzhou.aliyuncs.com
s3_region_name=oss-cn-hangzhou
s3_signature_version=s3v4
s3_addressing_style=virtual
```
::: tip Aliyun OSS Endpoint Format
Endpoint URL format: `https://<bucket-name>.<region>.aliyuncs.com`
Endpoint URL format: `https://oss-<region>.aliyuncs.com` (for example, region `cn-hangzhou`), **without the bucket name**. Set the bucket only in `s3_bucket_name` and select Virtual-host (`virtual`) addressing. The SDK adds the bucket to the hostname; including it in the endpoint can duplicate the bucket prefix or cause DNS failures.
The S3 client sets request and response checksums to `when_required` to avoid OSS's unsupported `STREAMING-UNSIGNED-PAYLOAD-TRAILER`. Chunk cleanup uses paginated, single-object deletion; no additional checksum environment variables are needed. Failed cleanup retains the upload session for retry by the scheduled task.
Common regions:
- Hangzhou: `oss-cn-hangzhou`
+1 -1
View File
@@ -42,7 +42,7 @@ docker run -d --restart unless-stopped \
--log-opt max-size=10m \
--log-opt max-file=3 \
--name filecodebox \
lanol/filecodebox:2.8.0 # x-release-please-version
lanol/filecodebox:2.9.0 # x-release-please-version
```
Open `http://localhost:12345` and complete the first-run setup.
+1 -1
View File
@@ -6,7 +6,7 @@ FileCodeBox 提供了丰富的配置选项。请优先通过管理面板修改
FileCodeBox 支持两种配置方式:
1. **管理面板配置**(推荐):访问 `/admin` 进入管理面板,在设置页面修改配置
1. **管理面板配置**(推荐):访问 `/#/admin` 进入管理面板,在设置页面修改配置
2. **数据库配置**:配置存储在 `data/filecodebox.db` 数据库中
::: tip 提示
+2
View File
@@ -68,6 +68,8 @@ volumes:
| `APP_ENV` | `production`(Docker) | 运行环境;生产环境默认关闭 HTTP 访问日志 |
| `LOG_LEVEL` | `warning`(Docker) | 日志级别:debug/info/warning/error |
| `ACCESS_LOG` | `false`(生产环境) | 是否启用 HTTP 访问日志;调试请求时可设为 `true` |
| `TIMEOUT_KEEP_ALIVE` | `5` | HTTP Keep-Alive 空闲超时(秒);前置反向代理后可调大以减少重复握手 |
| `MALLOC_TRIM_THRESHOLD_` | `65536` | glibc 归还空闲堆的阈值(字节);降低大文件请求后的常驻内存(仅 glibc 镜像生效) |
生产环境默认只输出 warning、error 等关键日志,并限制 Docker 日志最多保留 3 个 10 MB 文件。
+1 -1
View File
@@ -193,7 +193,7 @@ python main.py
3. 下载文件或查看文本
### 管理面板
1. 访问 `/admin`
1. 访问 `/#/admin`
2. 输入管理员密码
3. 管理文件和配置
+3 -3
View File
@@ -10,10 +10,10 @@ FileCodeBox 提供了功能完善的管理面板,让管理员可以方便地
### 登录方式
管理面板位于 `/admin` 路径。访问方式:
管理面板位于 `/#/admin`(前端 Hash 路由)。访问方式:
1. 首次访问站点时,按初始化页面设置管理员密码
2. 在浏览器中访问 `http://your-domain.com/admin`
2. 在浏览器中访问 `http://your-domain.com/#/admin`
3. 输入管理员密码(`admin_token` 配置项的值)
4. 点击登录按钮
@@ -30,7 +30,7 @@ FileCodeBox 提供了功能完善的管理面板,让管理员可以方便地
| `show_admin_addr` | int | `0` | 是否在首页显示管理入口(1=显示,0=隐藏) |
::: warning 安全建议
在公开服务中,建议保持 `show_admin_addr` 为 `0`,通过直接访问 `/admin` 路径进入管理面板,减少被恶意扫描的风险。
在公开服务中,建议保持 `show_admin_addr` 为 `0`,通过直接访问 `/#/admin` 进入管理面板。隐藏入口不是访问控制,仍须使用强密码。
:::
### 认证机制
+24
View File
@@ -0,0 +1,24 @@
# MCP 接入
FileCodeBox 提供 **Model Context Protocol (MCP)** 接口,支持通过 Streamable HTTP 让兼容的 AI 客户端创建文本或文件分享。
## 连接配置
在 MCP 客户端添加远程 HTTP 服务:
- **传输方式**:Streamable HTTP
- **服务地址**:`http://<FileCodeBox 地址>:12345/mcp`
- **认证**:默认无需 Token
例如本机部署时,地址为 `http://localhost:12345/mcp`。若管理员关闭了游客上传,或在管理设置中启用了 MCP Token 校验,请在客户端设置 `Authorization: Bearer <管理员 Token>`。不要把 Token 提交到公开配置或代码仓库。
## 可用工具
- `share_text`:创建文本分享。必填参数 `text`;可选 `expire_value`、`expire_style`。
- `share_file`:上传 Base64 文件并创建分享。必填参数 `filename`、`content_base64`;可选 `content_type`、`expire_value`、`expire_style`。
过期方式 `expire_style` 支持 `day`、`hour`、`minute`、`forever` 和 `count`,`expire_value` 必须大于 0。调用成功后返回分享码、链接和 Markdown 链接。
## 安全与限制
MCP 上传遵循 FileCodeBox 的游客上传开关、管理员 Token 校验、上传大小限制、文件类型校验及上传频率限制。仅向可信任的 AI 客户端提供服务地址;使用公网部署时请配置 HTTPS 并妥善保管管理员 Token。
+2 -2
View File
@@ -12,7 +12,7 @@ FileCodeBox 首次启动时不会生成默认管理员密码。请在浏览器
请通过管理面板修改管理员密码:
1. 访问 `/admin` 进入管理面板
1. 访问 `/#/admin` 进入管理面板
2. 使用当前密码登录
3. 进入「系统设置」页面
4. 找到 `admin_token` 配置项
@@ -41,7 +41,7 @@ FileCodeBox 首次启动时不会生成默认管理员密码。请在浏览器
| `show_admin_addr` | int | `0` | 是否显示管理入口(1=显示,0=隐藏) |
::: tip 建议
在公开服务中,建议保持 `show_admin_addr` 为 `0`,通过直接访问 `/admin` 路径进入管理面板。
在公开服务中,建议保持 `show_admin_addr` 为 `0`,通过直接访问 `/#/admin` 进入管理面板。
:::
## IP 速率限制
+6 -2
View File
@@ -52,6 +52,7 @@ storage_path=
| `s3_endpoint_url` | string | `""` | S3 端点 URL |
| `s3_region_name` | string | `auto` | 区域名称 |
| `s3_signature_version` | string | `s3v2` | 签名版本(`s3v2` 或 `s3v4`) |
| `s3_addressing_style` | string | `auto` | 地址风格(`auto`、`path` 或 `virtual`);OSS 使用 `virtual` |
| `s3_hostname` | string | `""` | S3 主机名(备用) |
| `s3_proxy` | int | `0` | 是否通过服务器代理下载(1=是,0=否) |
| `aws_session_token` | string | `""` | AWS 会话令牌(可选) |
@@ -75,13 +76,16 @@ file_storage=s3
s3_access_key_id=您的AccessKeyId
s3_secret_access_key=您的SecretAccessKey
s3_bucket_name=bucket-name
s3_endpoint_url=https://bucket-name.oss-cn-hangzhou.aliyuncs.com
s3_endpoint_url=https://oss-cn-hangzhou.aliyuncs.com
s3_region_name=oss-cn-hangzhou
s3_signature_version=s3v4
s3_addressing_style=virtual
```
::: tip 阿里云 OSS 端点格式
端点 URL 格式为:`https://<bucket-name>.<region>.aliyuncs.com`
端点 URL 格式为:`https://oss-<region>.aliyuncs.com`(例如 region 为 `cn-hangzhou`),**不要包含桶名**。桶名仅填入 `s3_bucket_name`,地址风格选择 Virtual-host(`virtual`),由 SDK 自动拼接桶名;否则可能出现桶名前缀重复或 DNS 解析失败。
S3 客户端已将请求和响应校验和设为 `when_required`,避免 OSS 不支持的 `STREAMING-UNSIGNED-PAYLOAD-TRAILER`;分片采用分页、逐对象删除,无需配置额外的校验和环境变量。清理失败会保留上传会话,供定时任务重试。
常用区域:
- 杭州:`oss-cn-hangzhou`
+1 -1
View File
@@ -42,7 +42,7 @@ docker run -d --restart unless-stopped \
--log-opt max-size=10m \
--log-opt max-file=3 \
--name filecodebox \
lanol/filecodebox:2.8.0 # x-release-please-version
lanol/filecodebox:2.9.0 # x-release-please-version
```
打开 `http://localhost:12345`,按引导完成首次初始化。
+10 -11
View File
@@ -10,7 +10,7 @@ from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from tortoise import Tortoise
from tortoise.contrib.fastapi import register_tortoise
from tortoise.contrib.fastapi import tortoise_exception_handlers
from apps.admin.views import admin_api
from apps.base.config import (
@@ -29,7 +29,7 @@ from apps.base.tasks import (
)
from apps.base.views import share_api, chunk_api, presign_api
from apps.base.mcp import router as mcp_router
from core.database import db_startup_lock, get_db_config, init_db
from core.database import db_startup_lock, init_db
from core.errors import StorageError
from core.logger import get_log_level_name, is_access_log_enabled, logger
from core.settings import settings
@@ -89,7 +89,14 @@ async def load_config():
)
app = FastAPI(lifespan=lifespan, version=APP_VERSION)
# Tortoise 的 ORM 异常映射(DoesNotExist → 404、IntegrityError → 422)。
# 只注册处理器;连接初始化统一由 lifespan 里的 init_db() 负责,
# 避免 register_tortoise 合并 lifespan 造成的双重初始化。
app = FastAPI(
lifespan=lifespan,
version=APP_VERSION,
exception_handlers=tortoise_exception_handlers(),
)
@app.exception_handler(StorageError)
@@ -126,14 +133,6 @@ app.add_middleware(
allow_headers=["*"],
)
# 使用 register_tortoise 来添加异常处理器
register_tortoise(
app,
config=get_db_config(),
generate_schemas=False,
add_exception_handlers=True,
)
app.include_router(share_api)
app.include_router(chunk_api)
app.include_router(presign_api)
+6 -21
View File
@@ -32,10 +32,15 @@ docker run -d --restart unless-stopped \
--log-opt max-size=10m \
--log-opt max-file=3 \
--name filecodebox \
lanol/filecodebox:2.8.0 # x-release-please-version
lanol/filecodebox:2.9.0 # x-release-please-version
```
访问 `http://localhost:12345`,完成首次初始化。生产环境建议固定版本号;`latest` 指向最新正式版。
## AI 客户端与 MCP
FileCodeBox 支持 **MCP Streamable HTTP**,可从兼容的 AI 客户端创建文本和文件分享。服务端点为 `http://<你的服务器地址>:12345/mcp`(本机部署可用 `http://localhost:12345/mcp`),默认无需 Token。工具包括 `share_text` 和 `share_file`。若关闭游客上传或启用 MCP Token 校验,客户端需配置 `Authorization: Bearer <管理员 Token>`。
详见 [MCP 接入指南](https://fcb-docs.aiuo.net/guide/mcp)。
## 简单,但足够强大
@@ -78,26 +83,6 @@ docker run -d --restart unless-stopped \
- [API 文档](https://fcb-docs.aiuo.net/api/) · 上传、取件与管理接口
- [前端源码](https://github.com/vastsa/FileCodeBoxFronted) · 当前 2024 主题
## 相关项目
如果你也喜欢「盒子」系列的自托管工具,可以看看同作者的:
### [BokeBox](https://github.com/vastsa/BokeBox/) · 私人 AI 播客盒
把视频、链接或文稿丢进去,生成可听完的个人播客,支持自定义主持人设与声音。
<p align="center">
<a href="https://github.com/vastsa/BokeBox/">
<img src="./.github/images/readme/bokebox/banner_zh.webp" alt="BokeBox 横幅" width="860" />
</a>
</p>
<p align="center">
<a href="https://github.com/vastsa/BokeBox/">
<img src="./.github/images/readme/bokebox/home_zh.webp" alt="BokeBox 首页" width="860" />
</a>
</p>
## 参与项目
欢迎 [提交 Issue](https://github.com/vastsa/FileCodeBox/issues/new/choose) 或 Pull Request。项目基于 [LGPL-3.0](./LICENSE) 发布。
+6 -21
View File
@@ -32,10 +32,15 @@ docker run -d --restart unless-stopped \
--log-opt max-size=10m \
--log-opt max-file=3 \
--name filecodebox \
lanol/filecodebox:2.8.0 # x-release-please-version
lanol/filecodebox:2.9.0 # x-release-please-version
```
Open `http://localhost:12345` and complete first-run setup. Pin a version in production; `latest` tracks the newest stable release.
## MCP for AI clients
FileCodeBox supports **MCP over Streamable HTTP**, allowing compatible AI clients to create text and file shares. Use `http://<your-server>:12345/mcp` (or `http://localhost:12345/mcp` for a local instance). No token is required by default. The available tools are `share_text` and `share_file`. If guest uploads are disabled or MCP token validation is enabled, configure `Authorization: Bearer <admin token>` in the client.
See the [MCP integration guide](https://fcb-docs.aiuo.net/en/guide/mcp).
## Simple, yet capable
@@ -78,26 +83,6 @@ Open `http://localhost:12345` and complete first-run setup. Pin a version in pro
- [API reference](https://fcb-docs.aiuo.net/en/api/) · upload, retrieval, and administration
- [Frontend source](https://github.com/vastsa/FileCodeBoxFronted) · the active 2024 theme
## Related projects
If you enjoy self-hosted “box” tools, also check out this project from the same author:
### [BokeBox](https://github.com/vastsa/BokeBox/) · Private AI Podcast Box
Drop in videos, links, or drafts, and get listen-worthy personal podcasts with custom host persona and voice.
<p align="center">
<a href="https://github.com/vastsa/BokeBox/">
<img src="./.github/images/readme/bokebox/banner_en.webp" alt="BokeBox banner" width="860" />
</a>
</p>
<p align="center">
<a href="https://github.com/vastsa/BokeBox/">
<img src="./.github/images/readme/bokebox/home_en.webp" alt="BokeBox home" width="860" />
</a>
</p>
## Contributing
[Issues](https://github.com/vastsa/FileCodeBox/issues/new/choose) and pull requests are welcome. FileCodeBox is released under [LGPL-3.0](./LICENSE).
+183
View File
@@ -328,6 +328,126 @@ h11==0.16.0 \
--hash=sha256:4e35b956cf45792e4caa5885e69fba00bdbc6ffafbfa020300e549b208ee5ff1 \
--hash=sha256:63cf8bbe7522de3bf65932fda1d9c2772064ffb3dae62d55932da54b31cb6c86
# via uvicorn
httptools==0.9.0 \
--hash=sha256:02bc5b3dcb6394b9d825fd62a7bfa0b2943063a3c89abc4492ad45e334a20eb5 \
--hash=sha256:050f7ab098121873c8f13e35857f97ab60a76185c8302bde9a384939bb7c3b96 \
--hash=sha256:050f84b7ec46a6efe0e5f521cf8729e3397c1cef4384f62ed8d5d68ca0045776 \
--hash=sha256:06bfe7fad972a417269d8a5fc53b87e4eca970354abf5e9e24336fd06d64292e \
--hash=sha256:088de1738e1af624466a01c35d652dbe6fb825be887c76d68aa850621d81db88 \
--hash=sha256:0adc974916efe1fbf89d0363a86dcb2c746727643e362ff398de1a4b50b6bc77 \
--hash=sha256:0cc339a807c156d840b54f8bf050ba0fc265eb81692c24bca8535b52fbd797c6 \
--hash=sha256:0fd73d0bbf700a30dd87e4412adf41cfa71542a533d6b390c7244bbb8a1152bb \
--hash=sha256:130635fea6e611a6b2026120037965ddb88b3dafd11bb64e264b101a70a76630 \
--hash=sha256:13873eb8aef5972fcfee614f63d47064312ad4efbfe65ade15b8a3b77f8c8659 \
--hash=sha256:18d800aaa2d6bff7d889df810d1b19a5fde72b1f6c0ca96e8d9f28a692fe5460 \
--hash=sha256:1a4050a651e1f2faf05eb028ce9f2168abbcee9e24b209f5c1f2eb96d8c569e4 \
--hash=sha256:1a7f1df31829c258158be01bb04eb668c4fba7df1ddf2262131a972962e651b6 \
--hash=sha256:1b01c0fcd6725a8d79a164ecdc4116866282479d68bb3d6d74a909bf994656c4 \
--hash=sha256:1b95775f6292d72cb452c33e5c0f8b8551807c29a10e3c1671fef7f61361370a \
--hash=sha256:1f6da814aeecbc6cb8872d6d3e85ed16e8ab1653f9557cea8658725ce212348a \
--hash=sha256:2095207b75a83c9e947346da9c127fb7e4fb29f41589df2643764f06b750989c \
--hash=sha256:22ab1b10b06d357f01092e60f5e6856a0d479ed79b0ec2166a339ea26c699be2 \
--hash=sha256:2319858018eedd0c0b2f950a620413c0a9d1352607be4267eb28209eca8b1e3f \
--hash=sha256:268d18601feb5367885c6ebf6f402c18fc25a324cee215784adafe0a1eef925f \
--hash=sha256:26e1d9629f3bf70d23f0d22238152aec51c837a7c9e384cb74f356fdccad7eb3 \
--hash=sha256:272db0c51e8b71e953c1f2ecbe63402b819680e4564be2ef285cfd4584ee8355 \
--hash=sha256:289f213d2a3dde2e8312c415ffecec5a01698589ec6249ec4e8fb3b47c0444ba \
--hash=sha256:29b0d823e3c1e7cd1093a5dc889245db693ef13ada624cd66e2262421ef38867 \
--hash=sha256:310266a2db1377ffae3bdf6556ab4973f4f94508a8ce37b2f6bb096a89bcefa1 \
--hash=sha256:3238e198429cb8909ec42951b82d6a33fe0fdfcf86371732f8f09311c5b8ac32 \
--hash=sha256:34266cec8c1d4e3e91fcca7efe38971d6bdda64a7944f2a46ab576da15173680 \
--hash=sha256:36fac804b8cfd6b935ae64f71349f833d2b6298404626d017a2c57bb942bc643 \
--hash=sha256:3af4e45ff455fce5511fdf2653c1ce428ef09c56fe37a83eb4d924c2d474f31e \
--hash=sha256:3e3201fe4d46e0d15d7ff9fafc94a605da9eb82d2c5b9837f0368acb325481f1 \
--hash=sha256:45b3002392948dcf578029c89f6318e1289a993a1a5ec38a4161560fab60f811 \
--hash=sha256:465bc1526debf53a3be92022a16ca0c38f891ea3b5c1587af4f52e44020f8a07 \
--hash=sha256:48c705bd0b1afb6253ed71eca9f9ba7ac7d47838e5fed1ef7891d67f21ecd4de \
--hash=sha256:4a4d8c2c7e73ba5967be74d7c3a5ff81fde815ee1b48d9c5c0f14de8463a847b \
--hash=sha256:4a85401b0c3f893cf5695c1199e8679fbf673f7f78c2f6c11d6b1850f8c7e358 \
--hash=sha256:4c58dc91aefb31adad500aa68054334f429b840b36dd29e34e834101044cb2ef \
--hash=sha256:4efbee349138a3fee7a4cc3a95abd2d499fae70dd5bff9fed9138d6f570f4283 \
--hash=sha256:4fb995082fe41ec410b33c48b54fb1d44abb8a6ee762c31e8c42519e8c3a30a9 \
--hash=sha256:5042aa1c7e2b1a24c17dab31d8770b63a5101c9abc25f832c6aef6b201e1ca4f \
--hash=sha256:52fe0176682a25b15370f23f5b0f1366a84771df89144fb0cd979cb72a94b5ca \
--hash=sha256:5332a020a60bbe32ede4bda1a62b3d56c4831d309cdf0932842c0fca8ad6aaa3 \
--hash=sha256:563e4568217dc907a91843f38c737be865222c0400a38cdcd0d26ce92b3db271 \
--hash=sha256:581b27663c6e9f4df68068f32fe6d1cd7647b31fac90237221a66f8821c342eb \
--hash=sha256:58a1b0ec4cbb930e69669f9771715b2c7898d3cdf064d9811f7a66afef96b544 \
--hash=sha256:5cc5d3a29f9ec86ce406e5ec09c241dd8dc4d30e838f74f68d728b89131a3acf \
--hash=sha256:63d38e9a9a10a20fb57593742e63c6b1e78dd7f6ef5472de8e0b1e4cf4f3db26 \
--hash=sha256:6b1ac7f1bc6c0dbf90684b77571a51a21b2463909fd916ce0ac9bfc4d566dc75 \
--hash=sha256:6b900073e7b8481ef1aaf4f6c1789d210a1db01a9da8789821578cfeb4c2d540 \
--hash=sha256:6c12d0393a903b58bc5f5a7406d6c5290acfb8284290d68547ce620c06f7d133 \
--hash=sha256:6e2780e33a58a93f27cc3bb74a55bae6f9a8278a1dbabdff392940d30d381671 \
--hash=sha256:6ebd39ee26db460cfe5ab8b71a15d1149b289139a0d3981522757d6af620887e \
--hash=sha256:6f8b41299b203ce8f627db670cfea82067d9638853dbeaf86dccd93878879b85 \
--hash=sha256:6f9549ca354a1d6d6167c458a1f1b12147726b968f02dd64b6a5801dba91ae0f \
--hash=sha256:6ff0145b34610e57c9fae20df4e133c8d54266447387de6fcc0bdabfe4db4569 \
--hash=sha256:6ff5f0ed70783dcb9562dbd20edca51c3d4d277f128223709e3da6b75986d1d4 \
--hash=sha256:714bf348f468532d86bed670837e7d5ddff3834dd7f5d3c08066da400c86f088 \
--hash=sha256:757e3f79cb865a7db94e0db5f4d0ed3284a69e39d53568f433982ea13c60cac1 \
--hash=sha256:7e32b83bd8c2f8b6fa726ef34e63e21c4d7eddc277d40d4ef7245ea3ed28e5b6 \
--hash=sha256:805b0f2618e5d4c3e28f45b731eb1a0539691ae4a2f97b4ce014de0bf96a1ff5 \
--hash=sha256:80eae881cfb69383303e9a4d7961a478025b89c24f38f2e69b30c516fa0d57f2 \
--hash=sha256:813a32f94991b9627795528053c73a57d2ce3eb98ede89f0e1c7a31095938e81 \
--hash=sha256:8463b34ebde3f000627e9dbd8a545f995ad49fbf7ff9dd5abc0cd507da98a603 \
--hash=sha256:8a59c749a73fbdbc8e63b895a3079825fa085d752e75bc0a500042cb8a801e48 \
--hash=sha256:8d90d10e9b6594c28f27896a68fab97fd784c43804e9fe419dab8e8dcfcf4b02 \
--hash=sha256:8e1e037bb57dbc549c6fe20370b763ea74bdb09413cdcf857e4f14d9e4e2fb13 \
--hash=sha256:931f45f84e15daafec5f82cc92e6710569e1f50933f3253d206eab4132bec678 \
--hash=sha256:995b52f7c260ac7023640221f27472303968753cb6fc6fce1ddfb0e9db59a398 \
--hash=sha256:9b4da5789d7cf576c7e81f0088c632f6ee3786d87d17f08e90e703c22ce15633 \
--hash=sha256:a3ed60ea9a7c352c590182c67404599e6b5a0c901e75ae4cceee9a9fd6bfa455 \
--hash=sha256:a4d1ecad62e83cc65b411ea0125972cf3af98821e8117129947fd1e3a113f8d2 \
--hash=sha256:ae9bb62a7902e2ab65782447cd3eeb753510feace4e3ea03937a85489b01b16b \
--hash=sha256:b2ab3aad55d75d0b8df8d8a1b5920baaec9b161112cd5e95984848b4d2cd3dfe \
--hash=sha256:b2cc6991f16f6d666d48e4b57318104e7b29109e32e2f6b86e9d44c4e6a27f4e \
--hash=sha256:b5a3f5f70967a1aa2bc47fec42a1e19d2fb38c61700e3ee62b63a4af4f4fd001 \
--hash=sha256:b68fb053b37c258a473ab67f4965c3b439500dc160fe364667035a6833eaf50a \
--hash=sha256:b6ee42112d785a913dd63ec0335435a3dddbea5040c151252db815b0095cf066 \
--hash=sha256:b928ab0ecaa664e8caecc529dcb8bc881b6b35bb2b74bf9a39ae25f982ee8812 \
--hash=sha256:b9430f65db521db7962ad951571d446171213686f96c998a54dc18ed574821e2 \
--hash=sha256:b9cd15cb7cf0d5cc41f649fd789aae12c56c3b83eff593f8e095c1d4555ad5c3 \
--hash=sha256:bb1533541c729ad422f870a780d8b4af924f9817d45b5f580390418cda72eaa2 \
--hash=sha256:bbf7377fbd41b7c87d47820e25b9876724963681c2a1d6f6ff2adb4db46ac174 \
--hash=sha256:bca180cbe84e4fba7807eb408a8655295f697928512324517e30a091ede522a8 \
--hash=sha256:beb2c8a34cc90fb4d862b7284eafdb322030d6a8b2ee5eb6a744f84205beedc3 \
--hash=sha256:bfdabac0c6d3d6a5be8c2a100a001c92c14a39bbafd5999545a675c493626e64 \
--hash=sha256:c0e45def4d9ce7073e2226535572442d9d6efb4047c7a5fd8960807e877ce70a \
--hash=sha256:c0f537e5e8152e8d9cae82804024790cb973061abd3b7ef8f66f46e2b5c7bb51 \
--hash=sha256:c195a69df0ab2541252ab5b1d76e3c182e5688ac2a9b708e5e6f66aaeda91e9a \
--hash=sha256:c271bfb832be5c5c020b4e2fcbc1e70a0b990adba6de874b0bba1184b89cdea3 \
--hash=sha256:c42424213c28804f8d0e20f5692106cfb57bf72e1dbc4092b8481fb2f9e4c707 \
--hash=sha256:c4fa57d3c31889722f64bfa785545a5e603a893b6f29ac1a41bfa830abeaefd5 \
--hash=sha256:cb2bb3ac0af7fdab2311b895c9eb95442b45deb14cc949b9e65545e74aa0be69 \
--hash=sha256:cb3e7a4fd0168e362673a980380bf4fd6ae3b1555150e60c5390b4b10d9c50c4 \
--hash=sha256:cbbfcd5d15056fbd1edd5e725cf3feeb47c7cbccbe205927ebab422cc229f417 \
--hash=sha256:cd3e55223a77d6e08d5730ebacb4930ecca5d2ce7c57e7ba10833be7e52903f1 \
--hash=sha256:ce8e723b4637034b76f5382a30a6b725518c332273e8d62a6c7d46e90837c947 \
--hash=sha256:d1e329a1866981efe0201d05a374617f6c6cf14434a501d78ab22793d1ab1fa6 \
--hash=sha256:d20ba5c84cf0592afb2713336f07e2b6ced082e4ae803ceada153a85613efc9f \
--hash=sha256:d2b095129b9a98eb46a271ee9631089529c4e40354576b4aa74e24de9d2bf2f7 \
--hash=sha256:d3906b5c549ff2ad2473cb711e1fc65d76715c2726a402108fbf55eab6c6b49d \
--hash=sha256:d484ebb7e3a3f3597b0f645fbd1b85633674ca808c1f5ba11c2caf7c66f5c8b6 \
--hash=sha256:db735a23ecb0f0450d2b24e0a05fb00a8a35c9db172919c4d3e023e7c7ee4c9b \
--hash=sha256:dbc9fd1521e573045d71b6afab7398439c5cc259e8cb9d416fe62d485c4899c6 \
--hash=sha256:df3867518b205be3648e2fbd522bf380c851b5c2500588047505afdd786b6669 \
--hash=sha256:e0acbd474d0af4afacc6e66c4273f8a19e25f8af4379fc816388095ea6b01371 \
--hash=sha256:eacf0f45ca3ff84c01481c60c15da9ee56711f7292f66663df0f57af61e011c2 \
--hash=sha256:ead1a40543a033a6732a9e1e515944979a19db3737ce77363fc0660e38554344 \
--hash=sha256:eae4e9c7a0785a1a715de0a74fb822ab40084c060f444f18f075d05e322aa7ef \
--hash=sha256:ecf7037e491c220cd73987838c1ac3958d787bb098c3be0bfaf7f04204a6162c \
--hash=sha256:ecfeee649184ffd800955068be9a6b579a0f33fc3c98535d685d5779cb59347f \
--hash=sha256:edd5aa045fa3cc57143db018dd32ce7962bd5b525d05230709015d7e570100aa \
--hash=sha256:f0ef48ce353f6b6a52232ba23d0983d4c2c84c84a778899404e34b4718509bf2 \
--hash=sha256:f1734bd6f588975ffc246211e8b96c11933344087ca280d2cbcbf35cf835d7a9 \
--hash=sha256:f67db0ba2bedafec15b8e5330d40da1e1c7921559fa715af021252bfef81a6f8 \
--hash=sha256:f6ac1414556b910a879c108d79736f77e797871f9919ed0d2c3cf8cf3ecca986 \
--hash=sha256:f78f7ae1c2e5aabf29583fc0d302d8081a663776f84578025662eb6f5d63a921 \
--hash=sha256:f9489c1d87160c126f73b004742fe8654fa1ce37ed89e9e01330a1c10aaecde4 \
--hash=sha256:f9ccc9884241efceb4547a92955d128574c864681f11b7ea3ecbde295fafbe8b \
--hash=sha256:fc1a4f9d18d32a6e0a0a0a382986a60a2126f5144dd08715be7adb8df18e8a46
# via -r requirements.txt
idna==3.19 \
--hash=sha256:5e0811a4383b21dc5838069f801c4fb62113b7447663d2530d2bd6e77b49bf15 \
--hash=sha256:815e7be7a7806d54abb586dc943addc79e8b2ee16915059658cbeff4b1b43bf4
@@ -836,6 +956,69 @@ uvicorn==0.53.0 \
--hash=sha256:a9356f0cb89b3b8621529c5d5eebd69bfe154f4c3f68b4cf2de47e45fa855c2e \
--hash=sha256:e8dca71ec86dce5f04e333f0d56cdedf942446e6643b9cea1af0d6d3a02cb03e
# via -r requirements.txt
uvloop==0.23.0 ; sys_platform != 'win32' \
--hash=sha256:0305871ac712f54b62af73f943dbf21ae3ce80a44bc0f0151424484affa85645 \
--hash=sha256:090865d8ce7a03986755a3ce711b7dd0d4b44eb14ab74368b717f3fad1180208 \
--hash=sha256:098a85e1393ef5202767b7e5fb41a32cd8bd81e6ee4af364c179801c4aa3f6d4 \
--hash=sha256:0efdd55bddbd36bb2fcb842d64c0d5f6407c6958c68088cc25df8c09edc5b5fd \
--hash=sha256:12634f15e6625f78b3f2922f91404c4d7173487eba11746764153f556e9852dc \
--hash=sha256:1748321e3c59a14a75404b1ae8d5a8d81c4e201803ea0e14c1b6fd84421024b5 \
--hash=sha256:19c64108b507cd0bc140e400e3396bacebd9d504956aa7726272bf6de7d9aabb \
--hash=sha256:1e84575f11873c109cf3962ad0bdf679094466184125f4cadcc41a73febff41f \
--hash=sha256:24c58ae4a83e93a04c504bcc678125e36a0bfc44af928ad69444880c60f187a5 \
--hash=sha256:28d160f51ab4da3b187063652e643dea6831072add4adc1e6d62afbe73b6be27 \
--hash=sha256:2dcff2d69be43e6559e5dad2c5a7a2dbfb60e05a77311b6c4b7a4a8123d86c65 \
--hash=sha256:31e0cf90bc8fd88784f6802cdba968a51fb1aec1cc3feec74d862b2d371d1330 \
--hash=sha256:378188efbb1524f2219d05246a3e1e5907217848d2882144dff59585f1b81d55 \
--hash=sha256:42feced24b9b44b856c633eafb5cc5dec354972da55ce77598db6844c054bc7c \
--hash=sha256:4448e9124537620f9c25d004c227bb5104440b58955c19bbd312d910af919a63 \
--hash=sha256:4a08875543bbd4519faf30497506c9cda8a48470467ffdf967c7313c7a5981a8 \
--hash=sha256:4b8e207c67d207a8608fec57e116511030af3495dc0109b8c333cf9cb412b16f \
--hash=sha256:4bb7f5d0b62b5afaaaea2b7b60d508921c24b0fe39c22c1438bec1811ffe10ec \
--hash=sha256:4f1798f56c6f4ba5ac11fa2869e5717926e4470d97a1dd42b4f59219d43b5027 \
--hash=sha256:514698d3683189031dcbfdc31e87115992e5ce9e1b19fe5359941323f2df800c \
--hash=sha256:53c2c5d7e2024e46776c2d90e6c637d01102126b61aaf5faa5edaf05f8b5722a \
--hash=sha256:55d6f4135d914305929fe9e9c44d8b5383a9b3fa1bee3bfcf60ee97e01af07ea \
--hash=sha256:5a2bbad3a63007f7e9524d4903ba04fee252557c2acd86f9a3d4f91786695254 \
--hash=sha256:5a3e0f56ec19bfd9ad1605572878dd6ff7f01b325f4fc154812ae70d615c3aff \
--hash=sha256:5bb9be71d9ee39b4359b832f9569518ec9bc08704194034e79e4958e6bc4d46d \
--hash=sha256:60ec798c40a1810d282ee046f61ecac1c5675cb898763d9f08d97d53a5e00a81 \
--hash=sha256:6b3cbc4f96ddfa1fb88a78a69dd851369825b7816d9702eee8c4461505ba172e \
--hash=sha256:6c7ef4701a96553514b2688e342ef1bf2beae6cfd172d89a76c768292aabf405 \
--hash=sha256:7337b06a9f9ed9ea3049f04b76f65819db9b19bb832ee598e97b388eadf25e5f \
--hash=sha256:76345f51367fb1f23e08605c6efb18374f669be5b223658fbab6b17627950507 \
--hash=sha256:7e35c9bc977760981693e1a7a51493b58ee5a501f9ebb1e547565ee40b6c6208 \
--hash=sha256:80cac5cb90ed7b9b72a217a1d6982b15b829cdbd0ee6bc19b93e3a9e47fb0ac9 \
--hash=sha256:8af88fe5c7dd68fe1fec6dea8155caa1a47155d219a750ff34049541cf536a5e \
--hash=sha256:8fcd721113260ffb5e38bf14a8725b17d431f34209f7d1c7005b667946e630b3 \
--hash=sha256:93087a845cdfb35753e539354ac9551bdd2ff528c202a98df0ae46e852bcf021 \
--hash=sha256:93935ab27b6eaef4c3e5489aebc84284f0644592f7ab516df60ee1b27eaf5eb3 \
--hash=sha256:9bf08e4b6362dd1c08623bbfa2d061e8bac0f1da8fc2007062cfe1dc360a49fa \
--hash=sha256:a6ac96da66c35bf789bdcde78a88dc7d56b7907d8379648c54adc1c61594575d \
--hash=sha256:ab17b3a8aa754be0de0e397f7b95f13b14e56f077a4c6ae295e3d4afd199b325 \
--hash=sha256:b0d106d9314546d69b3df1b5352639aa628530ec3ecef8a98a21942d2a2a64f5 \
--hash=sha256:b90397a50ad6332ed3e459c648ac20d182cce24a557354363ad85fc9ea4a17cd \
--hash=sha256:bbbdb8fcd5e7062e546eec1ac78c28bb21ae7df54c18f8e4b06e15a18d661a49 \
--hash=sha256:bd6f2f81c7b9da99d301c0b16b82044e76fe887086e42e1590ecf520b94dbdac \
--hash=sha256:be53e1d5f83de43dc175c87612ecc128d444b38e5c56cb3f807f5a73d6887476 \
--hash=sha256:c3f23f403a273900d57de6ee5ca0614c650f7f58563065dad1a4744498960e53 \
--hash=sha256:cbe8d03d4efcccdb7fcedecbaa1e1fa02913eaf3a74cb933634a6bc6d2ea9e2a \
--hash=sha256:ce17bc317d089f361b33521654c13e30eacfd3d2034fd34e613ca9c51c969686 \
--hash=sha256:d918d6f304a309222a784bbd140b85ec5594d97e4dc0e79f590549d28970663a \
--hash=sha256:dc61e4f9e37b507069dc7e659ae28bca7adcb04c993c3508214315d12c63f848 \
--hash=sha256:e095f9e105af76593b4c183bb0bcbdae64bd913a59ec595732dc108b48730ab5 \
--hash=sha256:e2cba180d6451822763eda8364f342435a873bcfb3849cbd82fdeca248ca65eb \
--hash=sha256:e49eba8f1e28e7c03648b7a476e1ba05309e087ccdea859fc6dd659564aa8d7e \
--hash=sha256:f1341c6abcee1c31277cfe28d34e46196f2143ec3d755e6efe7452126e1f626d \
--hash=sha256:f3fbfe82829d8e381426a289b87e59e585278728361db9ce975b88b51f64f410 \
--hash=sha256:f50b580fad005a092ed87c5a3a4683459b21d1620497d6a5bccad203bee4c071 \
--hash=sha256:f5576e8ae1723ece60d8f93c6710abf784714e99388bcf023ba9ca800bc587f6 \
--hash=sha256:f673d835bdb1a60229cc3609a113fd2c9ce3f4a3c75ad4eaed111180c00199d2 \
--hash=sha256:f7548ede3ee908cfabc0d068106e303a9a2d811af959cdf6ab85676344cedcda \
--hash=sha256:fa8ed556fcc87a4091cf61587ef172fa104323dc89ecc085a618ba7ff8629a8f \
--hash=sha256:fefea5cf8cdda9053b962ca8a90216fb0b1d40907dcb6819382b42e483e6e9f6 \
--hash=sha256:ff7144d8167e513fe39fbb46bffb4f6f192dfb1f4b0b4e9102e1fd4f212e4747
# via -r requirements.txt
wrapt==1.17.3 \
--hash=sha256:02b551d101f31694fc785e58e0720ef7d9a10c4e62c1c9358ce6f63f23e30a56 \
--hash=sha256:042ec3bb8f319c147b1301f2393bc19dba6e176b7da446853406d041c36c7828 \
+4
View File
@@ -7,3 +7,7 @@ pydantic==2.13.5
uvicorn==0.53.0
tortoise-orm==0.25.3
python-multipart==0.0.32
# Uvicorn auto-selects these when importable (`--loop auto` / `--http auto`).
# uvloop has no Windows wheels/support; httptools ships Windows wheels.
uvloop==0.23.0; sys_platform != "win32"
httptools==0.9.0
+82
View File
@@ -0,0 +1,82 @@
"""index.html render cache must stay correct across config/theme changes."""
import asyncio
import os
import tempfile
import unittest
from pathlib import Path
from fastapi import HTTPException
import apps.base.pages as pages
from core.settings import settings
class IndexRenderCacheTests(unittest.TestCase):
def setUp(self):
self._original_user_config = dict(settings.user_config)
self._original_resolve_theme_file = pages.resolve_theme_file
self._tmpdir = tempfile.TemporaryDirectory()
self.template = Path(self._tmpdir.name) / "index.html"
self.template.write_text(
"<title>{{title}}</title>"
'<meta name="description" content="{{description}}">'
"<div>{{opacity}}</div>",
encoding="utf-8",
)
pages.resolve_theme_file = lambda *args, **kwargs: self.template
pages._render_index_html.cache_clear()
def tearDown(self):
pages.resolve_theme_file = self._original_resolve_theme_file
settings.user_config = self._original_user_config
pages._render_index_html.cache_clear()
self._tmpdir.cleanup()
def _render(self) -> str:
return asyncio.run(pages.index()).body.decode("utf-8")
def test_repeated_requests_hit_the_render_cache(self):
first = self._render()
second = self._render()
self.assertEqual(first, second)
info = pages._render_index_html.cache_info()
self.assertEqual(info.misses, 1)
self.assertEqual(info.hits, 1)
def test_site_config_change_invalidates_cache(self):
self._render()
settings.name = "New Site Name"
html = self._render()
self.assertIn("<title>New Site Name</title>", html)
self.assertEqual(pages._render_index_html.cache_info().misses, 2)
def test_template_replacement_invalidates_cache(self):
self._render()
old_stat = self.template.stat()
self.template.write_text(
"<title>{{title}}</title><p>rebuilt</p>", encoding="utf-8"
)
os.utime(
self.template,
ns=(old_stat.st_atime_ns, old_stat.st_mtime_ns + 1_000_000),
)
html = self._render()
self.assertIn("<p>rebuilt</p>", html)
self.assertEqual(pages._render_index_html.cache_info().misses, 2)
def test_missing_template_still_404s_instead_of_serving_cache(self):
self._render()
self.template.unlink()
with self.assertRaises(HTTPException) as ctx:
asyncio.run(pages.index())
self.assertEqual(ctx.exception.status_code, 404)
if __name__ == "__main__":
unittest.main()
+2
View File
@@ -18,6 +18,7 @@ class IndexTemplateEscapingTests(unittest.TestCase):
def setUp(self):
self._original_user_config = dict(settings.user_config)
self._original_resolve_theme_file = pages.resolve_theme_file
pages._render_index_html.cache_clear()
tmp = tempfile.NamedTemporaryFile(
"w", suffix=".html", delete=False, encoding="utf-8"
)
@@ -32,6 +33,7 @@ class IndexTemplateEscapingTests(unittest.TestCase):
def tearDown(self):
settings.user_config = self._original_user_config
pages.resolve_theme_file = self._original_resolve_theme_file
pages._render_index_html.cache_clear()
self._template_path.unlink()
def _patch_template(self):
+16
View File
@@ -24,3 +24,19 @@ class AdminAddressPublicConfigTests(unittest.TestCase):
self.assertIs(type(public_value), int)
self.assertEqual(public_value, expected_value)
self.assertIs(feature_value, bool(expected_value))
class BackgroundPublicConfigTests(unittest.TestCase):
def setUp(self):
self._original_user_config = dict(settings.user_config)
def tearDown(self):
settings.user_config = self._original_user_config
def test_background_is_exposed_for_themes(self):
settings.background = "https://example.com/bg.jpg"
self.assertEqual(build_public_config()["background"], "https://example.com/bg.jpg")
def test_empty_background_is_string(self):
settings.background = None
self.assertEqual(build_public_config()["background"], "")
+1 -1
View File
@@ -163,7 +163,7 @@ class ShareUsageSecurityTests(unittest.TestCase):
self.assertEqual(response.media_type, "text/plain")
self.assertEqual(
response.headers["content-disposition"],
"attachment; filename*=UTF-8''TAG.txt",
'attachment; filename="TAG.txt"; filename*=UTF-8\'\'TAG.txt',
)
await record.refresh_from_db()
self.assertEqual(record.used_count, 1)
+84
View File
@@ -0,0 +1,84 @@
"""Issue #531: keep a legacy filename alongside the RFC 5987 UTF-8 name."""
import re
from types import SimpleNamespace
from urllib.parse import quote, unquote
import pytest
from apps.base import local_share, views
from apps.base.models import FileCodes
from core.settings import settings
from core.storage import build_attachment_headers
def legacy_filename(disposition):
match = re.search(r'(?:^|;\s*)filename="([^"]+)"', disposition)
assert match is not None, "legacy download clients cannot read filename*"
return match.group(1)
@pytest.mark.parametrize(
("filename", "fallback"),
[
("report.pdf", "report.pdf"),
("my archive.tar.gz", "my archive.tar.gz"),
("报告.pdf", "__.pdf"),
("图像 🖼.png", "__ _.png"),
("", "download"),
],
)
def test_attachment_has_legacy_and_utf8_filenames(filename, fallback):
headers = build_attachment_headers(filename, 42)
disposition = headers["Content-Disposition"]
assert disposition.startswith("attachment;")
assert legacy_filename(disposition) == fallback
assert disposition.endswith("filename*=UTF-8''" + quote(filename, safe=""))
assert headers["Content-Length"] == "42"
assert disposition.isascii()
def test_legacy_fallback_cannot_inject_parameters_headers_or_paths():
filename = '../bad\\name"; inline;\r\nX-Evil: true\x00%20.html'
disposition = build_attachment_headers(filename)["Content-Disposition"]
fallback = legacy_filename(disposition)
assert re.fullmatch(r"[A-Za-z0-9._ -]+", fallback)
assert not fallback.startswith(".")
assert fallback.endswith(".html")
assert "\r" not in disposition and "\n" not in disposition and "\x00" not in disposition
assert len(disposition.split(";")) == 3
assert unquote(disposition.split("filename*=UTF-8''", 1)[1]) == filename
def test_local_reference_download_uses_compatible_headers(tmp_path, monkeypatch):
source = tmp_path / "报告.pdf"
source.write_bytes(b"pdf contents")
monkeypatch.setattr(local_share, "get_local_root", lambda: tmp_path)
record = SimpleNamespace(uuid_file_name=source.name, prefix="报告", suffix=".pdf")
download = local_share.build_local_ref_download(record)
assert download.path == source
assert download.headers == build_attachment_headers("报告.pdf", source.stat().st_size)
assert legacy_filename(download.headers["Content-Disposition"]) == "__.pdf"
@pytest.mark.asyncio
async def test_text_download_uses_compatible_headers(db, monkeypatch):
monkeypatch.setattr(settings, "user_config", dict(settings.user_config))
settings.file_storage = "local"
record = await FileCodes.create(
code="legacy-text",
prefix="文本",
text="hello, 文本直链",
expired_count=-1,
expired_at=None,
)
response = await views.get_code_file(code=record.code, ip="127.0.0.1")
assert response.status_code == 200
assert response.body.decode("utf-8") == record.text
assert response.headers["content-disposition"] == build_attachment_headers("文本.txt")[
"Content-Disposition"
]
assert legacy_filename(response.headers["content-disposition"]) == "__.txt"
-46
View File
@@ -1,21 +1,7 @@
import base64
import pytest
from apps.base.models import KeyValue
from apps.base.mcp import sanitize_html_preview
def test_html_preview_sanitizer_removes_active_content_and_event_handlers():
output = sanitize_html_preview(
'<h1 onclick="alert(1)">Hello</h1><script>alert(2)</script>'
'<iframe src="https://attacker.invalid"></iframe><a href="javascript:alert(3)">link</a>'
)
assert "Hello" in output
assert "<script" not in output.lower()
assert "onclick" not in output.lower()
assert "attacker.invalid" not in output
assert "javascript:" not in output.lower()
@pytest.mark.asyncio
@@ -83,35 +69,3 @@ async def test_mcp_token_can_be_required(initialized_client):
)
assert authorized.status_code == 200
assert "/#/?code=" in authorized.json()["result"]["structuredContent"]["url"]
@pytest.mark.asyncio
async def test_mcp_html_share_has_sandboxed_safe_preview(initialized_client):
dangerous_html = '<h1>Hello</h1><script>alert(1)</script><img src=x onerror="alert(2)">'
response = await initialized_client.post(
"/mcp",
json={
"jsonrpc": "2.0",
"id": 5,
"method": "tools/call",
"params": {
"name": "share_file",
"arguments": {
"filename": "preview.html",
"content_type": "text/html",
"content_base64": base64.b64encode(dangerous_html.encode()).decode(),
},
},
},
)
assert response.status_code == 200
code = response.json()["result"]["structuredContent"]["code"]
preview = await initialized_client.get(f"/share/preview/html/{code}")
assert preview.status_code == 200
assert preview.headers["content-type"].startswith("text/html")
assert "sandbox" in preview.headers["content-security-policy"]
assert "Hello" in preview.text
assert "<script" not in preview.text.lower()
assert "onerror" not in preview.text.lower()
assert "nosniff" in preview.headers["x-content-type-options"]
+81
View File
@@ -0,0 +1,81 @@
"""迁移 008 建立的索引存在且被查询计划实际选用。
跑真实迁移链(不 generate_schemas),与部署升级形态一致;用
EXPLAIN QUERY PLAN 断言索引名出现在执行计划中,避免"建了索引但查询用不上"。
"""
import pytest
from tortoise import Tortoise
import apps.base.config as config_module
from core.database import execute_migrations
from tests.helpers import MEMORY_DB_CONFIG, close_db
INDEXES = {
"idx_filecodes_expired_at",
"idx_filecodes_created_at",
"idx_uploadchunk_upload_chunk",
}
@pytest.fixture
async def migrated_db():
config_module._config_cached_until = 0.0
if Tortoise._inited:
await Tortoise.close_connections()
await Tortoise.init(config=MEMORY_DB_CONFIG)
conn = Tortoise.get_connection("default")
await conn.execute_script(
"CREATE TABLE IF NOT EXISTS migrates ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"migration_file VARCHAR(255) NOT NULL UNIQUE, "
"executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)"
)
await execute_migrations()
try:
yield conn
finally:
await close_db()
async def _plan(conn, sql: str) -> str:
_, rows = await conn.execute_query(f"EXPLAIN QUERY PLAN {sql}")
return " | ".join(str(row["detail"]) for row in rows)
@pytest.mark.asyncio
class TestMigrationIndexes:
async def test_indexes_exist(self, migrated_db):
_, rows = await migrated_db.execute_query(
"SELECT name FROM sqlite_master WHERE type='index'"
)
names = {row["name"] for row in rows}
assert INDEXES <= names
async def test_expiry_scan_uses_index(self, migrated_db):
plan = await _plan(
migrated_db,
"SELECT id FROM filecodes WHERE expired_at < '2024-01-01 00:00:00'",
)
assert "idx_filecodes_expired_at" in plan
async def test_recent_listing_uses_created_at_index(self, migrated_db):
plan = await _plan(
migrated_db,
"SELECT id FROM filecodes ORDER BY created_at DESC LIMIT 8",
)
assert "idx_filecodes_created_at" in plan
async def test_chunk_lookup_uses_composite_index(self, migrated_db):
plan = await _plan(
migrated_db,
"SELECT chunk_index FROM uploadchunk "
"WHERE upload_id = 'abc' AND chunk_index = 3",
)
assert "idx_uploadchunk_upload_chunk" in plan
async def test_chunk_cleanup_uses_upload_id_prefix(self, migrated_db):
plan = await _plan(
migrated_db,
"DELETE FROM uploadchunk WHERE upload_id = 'abc'",
)
assert "idx_uploadchunk_upload_chunk" in plan
+9 -9
View File
@@ -1,5 +1,5 @@
"""Migration runner coverage: discovery order, registration, idempotency,
and failure propagation. Runs the REAL migrations (001-007) against an
and failure propagation. Runs the REAL migrations (001-008) against an
in-memory database — the same code path a deployment upgrade takes.
"""
import pytest
@@ -8,7 +8,7 @@ from tortoise import Tortoise
from core.database import execute_migrations
from tests.helpers import MEMORY_DB_CONFIG, close_db
EXPECTED = [f"migrations_{i:03d}.py" for i in range(1, 8)]
EXPECTED = [f"migrations_{i:03d}.py" for i in range(1, 9)]
@pytest.fixture
@@ -60,15 +60,15 @@ class TestMigrationRunner:
"""部分已登记(模拟中途升级):只执行缺口,顺序保持。"""
conn = Tortoise.get_connection("default")
await conn.execute_query("DELETE FROM migrates")
# 预登记 006/007(模拟已执行过全部迁移的库),验证 runner 只跑缺口
# 001-005 且跳过已登记的 006/007(跳过语义 = 不再执行其 DDL)
for name in EXPECTED[-2:]:
# 预登记 006/007/008(模拟已执行过末尾迁移的库),验证 runner 只跑缺口
# 001-005 且跳过已登记的 006/007/008(跳过语义 = 不再执行其 DDL)
for name in EXPECTED[-3:]:
await conn.execute_query(
"INSERT INTO migrates (migration_file) VALUES (?)", [name]
)
await execute_migrations()
registered = await _registered()
# 预登记的 006/007 占据登记序前列——断言用集合:7 个全登记、无重复
# 预登记的 006/007/008 占据登记序前列——断言用集合:全部登记、无重复
assert sorted(registered) == sorted(EXPECTED)
assert len(registered) == len(EXPECTED)
tables = {
@@ -107,7 +107,7 @@ class TestMigrationRunner:
"""迁移执行失败必须向上抛(启动失败可见),不能静默吞掉。"""
import importlib
# 构造一个会失败的执行缺口:007 未登记,且其模块 migrate 抛错
# 构造一个会失败的执行缺口:008 未登记,且其模块 migrate 抛错
conn = Tortoise.get_connection("default")
await conn.execute_query("DELETE FROM migrates")
for name in EXPECTED[:-1]:
@@ -117,7 +117,7 @@ class TestMigrationRunner:
real_import = importlib.import_module
def fake_import(name):
if name.endswith("migrations_007"):
if name.endswith("migrations_008"):
class FakeMod:
@staticmethod
async def migrate():
@@ -130,4 +130,4 @@ class TestMigrationRunner:
with pytest.raises(RuntimeError, match="boom"):
await execute_migrations()
# 失败的迁移不得登记
assert "migrations_007.py" not in await _registered()
assert "migrations_008.py" not in await _registered()
+81
View File
@@ -0,0 +1,81 @@
"""IPRateLimit is bounded and uses a monotonic clock."""
import unittest
from unittest import mock
from apps.base.dependencies import IPRateLimit
class FakeClock:
def __init__(self, now: float = 1000.0):
self.now = now
def __call__(self) -> float:
return self.now
class IPRateLimitBoundsTests(unittest.TestCase):
def test_first_add_returns_one(self):
limiter = IPRateLimit(count=3, minutes=1)
self.assertEqual(limiter.add_ip("203.0.113.1"), 1)
self.assertEqual(limiter.add_ip("203.0.113.1"), 2)
def test_capacity_evicts_least_recently_active_ip(self):
limiter = IPRateLimit(count=10, minutes=1, max_ips=3)
for ip in ("a", "b", "c", "d"):
limiter.add_ip(ip)
self.assertEqual(len(limiter.ips), 3)
self.assertEqual(list(limiter.ips), ["b", "c", "d"])
def test_activity_refreshes_recency(self):
limiter = IPRateLimit(count=10, minutes=1, max_ips=2)
limiter.add_ip("a")
limiter.add_ip("b")
limiter.add_ip("a")
limiter.add_ip("c")
self.assertEqual(list(limiter.ips), ["a", "c"])
def test_window_blocks_then_resets(self):
clock = FakeClock()
with mock.patch("time.monotonic", clock):
limiter = IPRateLimit(count=3, minutes=1)
for _ in range(3):
limiter.add_ip("198.51.100.9")
self.assertFalse(limiter.check_ip("198.51.100.9"))
clock.now += 30
self.assertFalse(limiter.check_ip("198.51.100.9"))
clock.now += 31
self.assertTrue(limiter.check_ip("198.51.100.9"))
self.assertNotIn("198.51.100.9", limiter.ips)
def test_remove_expired_compacts_old_entries_only(self):
clock = FakeClock()
with mock.patch("time.monotonic", clock):
limiter = IPRateLimit(count=10, minutes=1)
limiter.add_ip("old")
clock.now += 30
limiter.add_ip("fresh")
clock.now += 31
import asyncio
asyncio.run(limiter.remove_expired_ip())
self.assertEqual(list(limiter.ips), ["fresh"])
def test_record_count_is_bounded_under_ip_scan(self):
limiter = IPRateLimit(count=10, minutes=1, max_ips=10_000)
for i in range(50_000):
limiter.add_ip(f"10.0.{i // 256}.{i % 256}")
self.assertEqual(len(limiter.ips), 10_000)
if __name__ == "__main__":
unittest.main()
+62
View File
@@ -0,0 +1,62 @@
"""运行时调优配置必须与镜像启动命令、锁文件保持一致。
这些是部署层字符串契约(Dockerfile / requirements 锁文件),用测试钉住,
避免后续改动把它们悄悄丢掉。
"""
import re
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
class DockerRuntimeTuningTests(unittest.TestCase):
def setUp(self):
self.dockerfile = (ROOT / "Dockerfile").read_text(encoding="utf-8")
def test_glibc_heap_trim_threshold_is_set(self):
self.assertIn("MALLOC_TRIM_THRESHOLD_=65536", self.dockerfile)
def test_keep_alive_timeout_is_configurable(self):
self.assertIn("TIMEOUT_KEEP_ALIVE=5", self.dockerfile)
# CMD 必须把环境变量透传给 uvicorn,否则 ENV 只是摆设。
self.assertIn("--timeout-keep-alive", self.dockerfile)
self.assertIn("${TIMEOUT_KEEP_ALIVE:-5}", self.dockerfile)
class PerformanceDependencyTests(unittest.TestCase):
def test_uvicorn_extras_are_pinned(self):
requirements = (ROOT / "requirements.txt").read_text(encoding="utf-8")
self.assertIn('uvloop==0.23.0; sys_platform != "win32"', requirements)
self.assertIn("httptools==0.9.0", requirements)
def test_lockfile_contains_hashed_wheels(self):
lock = (ROOT / "requirements.lock.txt").read_text(encoding="utf-8")
for package in ("httptools", "uvloop"):
blocks = re.findall(
rf"(?ms)^{package}==\S+[^\n]*\n((?: --hash=\S+[^\n]*\n)+)",
lock,
)
self.assertTrue(blocks, f"{package} missing from requirements.lock.txt")
self.assertTrue(
any("--hash=sha256:" in block for block in blocks),
f"{package} lock entry has no hashes",
)
def test_lockfile_matches_requirements_pins(self):
requirements = (ROOT / "requirements.txt").read_text(encoding="utf-8")
lock = (ROOT / "requirements.lock.txt").read_text(encoding="utf-8")
pins = dict(
re.findall(r"^([\w-]+)==([\w.]+)(?:\s*;[^#\n]*)?$", requirements, re.M)
)
for name, version in pins.items():
match = re.search(rf"(?mi)^{re.escape(name)}==([\w.]+)\b", lock)
self.assertIsNotNone(match, f"{name} missing from lockfile")
self.assertEqual(match.group(1), version, f"{name} lockfile is stale")
if __name__ == "__main__":
unittest.main()
+314
View File
@@ -0,0 +1,314 @@
"""OSS compatibility regressions: real SDK request preparation, no external I/O."""
from datetime import timedelta
from unittest.mock import AsyncMock
from urllib.parse import urlsplit
import aioboto3
import pytest
from botocore.exceptions import ClientError
from fastapi import HTTPException
from apps.base import services, tasks, views
from apps.base.models import FileCodes, StorageReservation, UploadChunk
from core.settings import settings
from core.storage.s3 import S3FileStorage
from core.utils import get_now
@pytest.fixture
def storage(monkeypatch):
monkeypatch.setattr(settings, "user_config", {
**settings.user_config, "file_storage": "s3", "upload_size": 1024,
"chunk_expire_hours": 24,
})
storage = S3FileStorage.__new__(S3FileStorage)
storage.signature_version = "s3v4"
storage.addressing_style = "virtual"
storage.bucket_name = "test-bucket"
# Reserved .invalid domain; before-send hooks stop requests before any I/O.
storage.endpoint_url = "https://oss-cn-hangzhou.example.invalid"
storage.region_name = "oss-cn-hangzhou"
storage.aws_session_token = None
storage.session = aioboto3.Session(
aws_access_key_id="testing", aws_secret_access_key="testing"
)
return storage
def test_checksum_config_preserves_signature_and_addressing(storage):
config = storage._client_config()
assert config.request_checksum_calculation == "when_required"
assert config.response_checksum_validation == "when_required"
assert config.signature_version == "s3v4"
assert config.s3["addressing_style"] == "virtual"
@pytest.mark.asyncio
@pytest.mark.parametrize("operation", ["PutObject", "UploadPart"])
async def test_sdk_upload_has_no_flexible_checksum_trailer(storage, monkeypatch, operation):
monkeypatch.setenv("AWS_REQUEST_CHECKSUM_CALCULATION", "when_supported")
monkeypatch.setenv("AWS_RESPONSE_CHECKSUM_VALIDATION", "when_supported")
captured = []
class RequestCaptured(Exception):
pass
def intercept(request, **kwargs):
captured.append(request)
raise RequestCaptured
# Register on the session so save_chunk's real _client() inherits the hook.
storage.session.events.register(f"before-send.s3.{operation}", intercept)
with pytest.raises(RequestCaptured):
if operation == "PutObject":
await storage.save_chunk("up", 0, b"payload", "hash", "share/file.bin")
else:
async with storage._client() as client:
assert client.meta.config.response_checksum_validation == "when_required"
await client.upload_part(
Bucket=storage.bucket_name, Key="share/file.bin", UploadId="up",
PartNumber=1, Body=b"payload",
)
assert len(captured) == 1
request = captured[0]
headers = {key.lower(): value for key, value in request.headers.items()}
assert "x-amz-trailer" not in headers
assert not any(key.startswith("x-amz-checksum-") for key in headers)
assert "x-amz-sdk-checksum-algorithm" not in headers
assert b"aws-chunked" not in headers.get("content-encoding", b"")
assert b"TRAILER" not in headers.get("x-amz-content-sha256", b"")
assert urlsplit(request.url).hostname == "test-bucket.oss-cn-hangzhou.example.invalid"
assert not urlsplit(request.url).path.startswith("/test-bucket/")
class FakeS3:
"""Only single-object deletion is available; paginate from a bounded snapshot."""
def __init__(self, keys=(), fail_key=None, list_error=False):
self.keys = set(keys)
self.fail_key = fail_key
self.list_error = list_error
self.deleted = []
self.prefixes = []
self.pages = 0
async def __aenter__(self):
return self
async def __aexit__(self, *args):
pass
def get_paginator(self, operation):
assert operation == "list_objects_v2"
return self
async def paginate(self, Bucket, Prefix):
assert Bucket == "test-bucket"
self.prefixes.append(Prefix)
if self.list_error:
raise RuntimeError("list failed")
# Also exercise a page without Contents.
yield {}
keys = sorted(key for key in self.keys if key.startswith(Prefix))
for offset in range(0, len(keys), 1000):
self.pages += 1
yield {"Contents": [{"Key": key} for key in keys[offset:offset + 1000]]}
async def delete_object(self, Bucket, Key):
assert Bucket == "test-bucket"
if Key == self.fail_key:
raise ClientError({"Error": {"Code": "AccessDenied"}}, "DeleteObject")
self.deleted.append(Key)
self.keys.discard(Key)
return {}
def install_client(storage, monkeypatch, fake):
monkeypatch.setattr(storage, "_client", lambda: fake)
monkeypatch.setattr(tasks, "storages", {"s3": lambda: storage})
monkeypatch.setattr(views, "storages", {"s3": lambda: storage})
monkeypatch.setattr(services.FileUploadService, "_storage", lambda: storage)
@pytest.mark.asyncio
async def test_cleanup_paginates_and_keeps_neighboring_upload(storage, monkeypatch):
prefix = "share/chunks/up/"
keys = {f"{prefix}{i}.part" for i in range(1005)}
neighbors = {"share/chunks/up-other/0.part", "share/file.bin"}
fake = FakeS3(keys | neighbors)
install_client(storage, monkeypatch, fake)
await storage.clean_chunks("up", "share/file.bin")
assert fake.prefixes == [prefix]
assert fake.pages == 2
assert set(fake.deleted) == keys
assert fake.keys == neighbors
await storage.clean_chunks("up", "share/file.bin") # Empty/idempotent retry.
assert fake.keys == neighbors
@pytest.mark.asyncio
async def test_cleanup_listing_failure_propagates(storage, monkeypatch):
install_client(storage, monkeypatch, FakeS3(list_error=True))
with pytest.raises(RuntimeError, match="list failed"):
await storage.clean_chunks("up", "share/file.bin")
async def seed_session(upload_id="up"):
now = await get_now()
values = dict(
upload_id=upload_id, chunk_hash="a" * 64, total_chunks=2,
file_size=8, chunk_size=4, file_name="file.bin", save_path="share/file.bin",
created_at=now - timedelta(hours=48),
)
session = await UploadChunk.create(chunk_index=-1, **values)
for index in range(2):
await UploadChunk.create(chunk_index=index, completed=True, **values)
await StorageReservation.create(
token=f"chunk:{upload_id}", size=8, expires_at=now + timedelta(hours=1)
)
return session
async def run_cleanup_round(monkeypatch):
class StopLoop(Exception):
pass
monkeypatch.setattr(tasks, "refresh_settings", AsyncMock())
monkeypatch.setattr(tasks.asyncio, "sleep", AsyncMock(side_effect=StopLoop))
with pytest.raises(StopLoop):
await tasks.clean_incomplete_uploads()
@pytest.mark.asyncio
async def test_partial_failure_keeps_records_then_task_retries(db, storage, monkeypatch):
await seed_session()
await seed_session("other")
keys = {"share/chunks/up/0.part", "share/chunks/up/1.part"}
fake = FakeS3(keys, fail_key="share/chunks/up/1.part")
install_client(storage, monkeypatch, fake)
with pytest.raises(ClientError):
await storage.clean_chunks("up", "share/file.bin")
assert fake.keys == {fake.fail_key}
await run_cleanup_round(monkeypatch)
assert await UploadChunk.filter(upload_id="up").count() == 3
assert await StorageReservation.filter(token="chunk:up").exists()
# Failure in one session must not prevent another session being cleaned.
assert not await UploadChunk.filter(upload_id="other").exists()
fake.fail_key = None
await run_cleanup_round(monkeypatch)
assert not fake.keys
assert not await UploadChunk.filter(upload_id="up").exists()
assert not await StorageReservation.filter(token="chunk:up").exists()
@pytest.mark.asyncio
@pytest.mark.parametrize("cleanup_fails", [False, True])
async def test_completed_share_survives_cleanup_failure(db, storage, monkeypatch, cleanup_fails):
session = await seed_session()
fake = FakeS3(list_error=cleanup_fails)
install_client(storage, monkeypatch, fake)
monkeypatch.setattr(storage, "merge_chunks", AsyncMock(return_value=("share/file.bin", "a" * 64)))
monkeypatch.setattr(services, "reserve_storage", AsyncMock())
monkeypatch.setattr(services, "get_expire_info", AsyncMock(return_value=(
await get_now() + timedelta(days=1), -1, 0, "oss-code",
)))
result = await services.FileUploadService.complete_chunked_upload("up", session, 1, "day")
assert result == {"code": "oss-code", "name": "file.bin"}
assert await FileCodes.filter(code="oss-code").exists()
assert await UploadChunk.filter(upload_id="up").exists() is cleanup_fails
assert not await StorageReservation.filter(token="chunk:up").exists()
if cleanup_fails:
fake.list_error = False
await run_cleanup_round(monkeypatch)
assert not await UploadChunk.filter(upload_id="up").exists()
assert await FileCodes.filter(code="oss-code").exists()
@pytest.mark.asyncio
async def test_oversize_cleanup_failure_keeps_retry_records(db, storage, monkeypatch):
session = await seed_session()
install_client(storage, monkeypatch, FakeS3(list_error=True))
monkeypatch.setattr(services, "reserve_storage", AsyncMock())
settings.user_config = {**settings.user_config, "upload_size": 1}
with pytest.raises(HTTPException) as error:
await services.FileUploadService.complete_chunked_upload("up", session, 1, "day")
assert error.value.status_code == 403
assert await UploadChunk.filter(upload_id="up").count() == 3
assert not await FileCodes.all().exists()
@pytest.mark.asyncio
async def test_cancel_cleanup_failure_keeps_retry_records(db, storage, monkeypatch):
await seed_session()
fake = FakeS3(list_error=True)
install_client(storage, monkeypatch, fake)
with pytest.raises(HTTPException) as error:
await views.cancel_upload("up")
assert error.value.status_code == 503
assert await UploadChunk.filter(upload_id="up").count() == 3
assert await StorageReservation.filter(token="chunk:up").exists()
fake.list_error = False
await views.cancel_upload("up")
assert not await UploadChunk.filter(upload_id="up").exists()
assert not await StorageReservation.filter(token="chunk:up").exists()
@pytest.mark.asyncio
@pytest.mark.parametrize("delete_share", [False, True])
async def test_published_session_cannot_be_reused(db, storage, monkeypatch, delete_share):
session = await seed_session()
install_client(storage, monkeypatch, FakeS3(list_error=True))
merge = AsyncMock(return_value=("share/file.bin", "a" * 64))
save_chunk = AsyncMock()
reserve = AsyncMock()
monkeypatch.setattr(storage, "merge_chunks", merge)
monkeypatch.setattr(storage, "save_chunk", save_chunk)
monkeypatch.setattr(services, "reserve_storage", reserve)
monkeypatch.setattr(views, "reserve_storage", reserve)
monkeypatch.setattr(services, "get_expire_info", AsyncMock(return_value=(
await get_now() + timedelta(days=1), -1, 0, "oss-code",
)))
result = await services.FileUploadService.complete_chunked_upload("up", session, 1, "day")
assert result["code"] == "oss-code"
persisted_session = await UploadChunk.get(upload_id="up", chunk_index=-1)
assert persisted_session.completed is True
assert await FileCodes.filter(upload_id="up").count() == 1
if delete_share:
# The terminal marker must survive expiration/deletion of the share itself.
await FileCodes.filter(upload_id="up").delete()
with pytest.raises(HTTPException) as error:
await services.FileUploadService.complete_chunked_upload(
"up", persisted_session, 1, "day"
)
assert error.value.status_code == 409
with pytest.raises(HTTPException) as error:
await views.complete_upload(
"up", views.CompleteUploadModel(expire_value=1, expire_style="day"), "127.0.0.1"
)
assert error.value.status_code == 409
# Even when a cleaned fragment no longer has a row, it cannot be uploaded again.
await UploadChunk.filter(upload_id="up", chunk_index=0).delete()
chunk = AsyncMock()
with pytest.raises(HTTPException) as error:
await views.upload_chunk("up", 0, chunk)
assert error.value.status_code == 409
chunk.read.assert_not_awaited()
save_chunk.assert_not_awaited()
merge.assert_awaited_once()
reserve.assert_awaited_once()
assert await FileCodes.filter(upload_id="up").count() == (0 if delete_share else 1)
# The -1 cleanup marker is not a file fragment or progress credit.
status = await views.get_upload_status("up")
assert status.detail["uploaded_chunks"] == [1]
assert status.detail["progress"] == 50
# Initializing the same file must create a new upload, not revive the old ID.
initialized = await views.init_chunk_upload(views.InitChunkUploadModel(
file_name="file.bin", file_size=8, chunk_size=4, file_hash="a" * 64,
))
assert initialized.detail["upload_id"] != "up"
assert initialized.detail["uploaded_chunks"] == []
assert (await UploadChunk.get(upload_id="up", chunk_index=-1)).completed is True
+50
View File
@@ -0,0 +1,50 @@
"""SQLite 连接 PRAGMA 生效性验证。
不 mock 配置字典,而是用 get_db_config() 真起一个文件库读取 PRAGMA——
Tortoise 的 SqliteClient 在 create_connection() 时逐条执行 credentials 中的
PRAGMA,只有真实连接才能验证这一点。
"""
import pytest
from tortoise import Tortoise
import core.database as database
from tests.helpers import close_db
@pytest.fixture
async def file_db(tmp_path, monkeypatch):
monkeypatch.setattr(database, "_DB_FILE", str(tmp_path / "pragma.db"))
if Tortoise._inited:
await Tortoise.close_connections()
await Tortoise.init(config=database.get_db_config())
try:
yield Tortoise.get_connection("default")
finally:
await close_db()
async def _pragma(conn, statement: str):
rows = await conn.execute_query_dict(statement)
return next(iter(rows[0].values()))
@pytest.mark.asyncio
class TestSqlitePragmas:
async def test_wal_and_busy_timeout_kept(self, file_db):
assert str(await _pragma(file_db, "PRAGMA journal_mode")).lower() == "wal"
assert await _pragma(file_db, "PRAGMA busy_timeout") == 10000
async def test_synchronous_normal(self, file_db):
# 1 = NORMAL(WAL 下省 fsync),2 = FULL
assert await _pragma(file_db, "PRAGMA synchronous") == 1
async def test_temp_store_memory(self, file_db):
# 0 = DEFAULT,1 = FILE,2 = MEMORY
assert await _pragma(file_db, "PRAGMA temp_store") == 2
async def test_cache_size_8mb(self, file_db):
# 负值单位为 KiB
assert await _pragma(file_db, "PRAGMA cache_size") == -8000
async def test_foreign_keys_on(self, file_db):
assert await _pragma(file_db, "PRAGMA foreign_keys") == 1
+68
View File
@@ -0,0 +1,68 @@
"""One Tortoise init path; ORM exception mappings still registered."""
import asyncio
import unittest
from pathlib import Path
from unittest import mock
from tortoise.exceptions import DoesNotExist, IntegrityError
import main
async def _idle_task():
# Stand-in for the real background loops; the lifespan cancels it on exit.
await asyncio.sleep(3600)
class StartupSingleInitTests(unittest.TestCase):
def test_orm_exception_handlers_are_registered(self):
# Behavior pinned by test_admin_write_paths: DoesNotExist -> 404 JSON.
# IntegrityError -> 422 must stay available too.
self.assertIn(DoesNotExist, main.app.exception_handlers)
self.assertIn(IntegrityError, main.app.exception_handlers)
def test_register_tortoise_is_not_used(self):
# register_tortoise merges another lifespan that calls Tortoise.init
# before init_db(); batch 4 (SQLite PRAGMAs) needs a single init path.
source = Path("main.py").read_text(encoding="utf-8")
self.assertNotIn("import register_tortoise", source)
self.assertNotIn("register_tortoise(", source)
def test_lifespan_does_not_initialize_tortoise_outside_init_db(self):
async def scenario():
init_db_calls = []
async def fake_init_db():
init_db_calls.append(True)
async def fake_load_config():
return None
with (
mock.patch.object(main, "init_db", fake_init_db),
mock.patch.object(main, "load_config", fake_load_config),
mock.patch.object(main, "delete_expire_files", _idle_task),
mock.patch.object(main, "clean_incomplete_uploads", _idle_task),
mock.patch.object(
main, "clean_expired_presign_sessions", _idle_task
),
mock.patch.object(main.Tortoise, "init") as tortoise_init,
mock.patch.object(
main.Tortoise, "close_connections"
),
):
# FastAPI merges the lifespan of every included router, so use
# the installed context rather than main.lifespan directly.
async with main.app.router.lifespan_context(main.app):
pass
return init_db_calls, tortoise_init
init_db_calls, tortoise_init = asyncio.run(scenario())
self.assertEqual(init_db_calls, [True])
tortoise_init.assert_not_awaited()
if __name__ == "__main__":
unittest.main()
+54
View File
@@ -0,0 +1,54 @@
"""Theme assets get a safe Cache-Control policy.
Fingerprinted (Vite) filenames can be cached immutably; public assets whose
names do not change must use a short TTL so in-place upgrades are picked up.
"""
import asyncio
import tempfile
import unittest
from pathlib import Path
import apps.base.pages as pages
class ThemeAssetCacheTests(unittest.TestCase):
def setUp(self):
self._original_resolve_theme_file = pages.resolve_theme_file
self._tmpdir = tempfile.TemporaryDirectory()
self.root = Path(self._tmpdir.name)
(self.root / "assets").mkdir()
pages.resolve_theme_file = lambda *parts: self.root.joinpath(*parts)
def tearDown(self):
pages.resolve_theme_file = self._original_resolve_theme_file
self._tmpdir.cleanup()
def _asset_response(self, name: str):
(self.root / "assets" / name).write_bytes(b"x")
return asyncio.run(pages.theme_asset(name))
def test_fingerprinted_asset_is_immutable(self):
response = self._asset_response("index-BX2s1QqP.js")
self.assertIn("max-age=31536000", response.headers["cache-control"])
self.assertIn("immutable", response.headers["cache-control"])
def test_plain_asset_gets_short_cache(self):
response = self._asset_response("logo.png")
self.assertIn("max-age=86400", response.headers["cache-control"])
self.assertNotIn("immutable", response.headers["cache-control"])
def test_short_hash_is_not_treated_as_fingerprint(self):
self.assertEqual(
pages._asset_cache_control("logo-2024.png"),
pages._DEFAULT_ASSET_CACHE,
)
self.assertEqual(
pages._asset_cache_control("index-BX2s1QqP.css"),
pages._IMMUTABLE_ASSET_CACHE,
)
if __name__ == "__main__":
unittest.main()