mirror of
https://github.com/vastsa/FileCodeBox.git
synced 2026-11-21 06:16:59 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6f88eae854 | ||
|
|
5da611c5de | ||
|
|
a578fc690c | ||
|
|
2073f9ba4b | ||
|
|
0a1f08fd6e | ||
|
|
9bf03bc427 | ||
|
|
264eb4f986 | ||
|
|
c973d28256 | ||
|
|
e7f761c35f | ||
|
|
8f7a1e1594 | ||
|
|
4f141eeb49 | ||
|
|
496a7139bd | ||
|
|
5b5d70c3b7 | ||
|
|
ec714dcd6f | ||
|
|
588af7b6c3 | ||
|
|
080fecf653 | ||
|
|
3d2c8ec709 | ||
|
|
fda470b076 | ||
|
|
41e3ce1b9c | ||
|
|
31027e6eea | ||
|
|
d0f08a7be5 | ||
|
|
5646f41255 |
No files matched your search
@@ -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():
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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,3 +1,3 @@
|
||||
{
|
||||
".": "2.8.3"
|
||||
".": "2.8.4"
|
||||
}
|
||||
+4
-2
@@ -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:-}\""]
|
||||
+51
-25
@@ -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)
|
||||
|
||||
@@ -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
@@ -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
@@ -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"},
|
||||
)
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
version: "3"
|
||||
services:
|
||||
file-code-box:
|
||||
image: lanol/filecodebox:2.8.3 # x-release-please-version
|
||||
image: lanol/filecodebox:2.8.4 # x-release-please-version
|
||||
volumes:
|
||||
- fcb-data:/app/data:rw
|
||||
# NAS 已有文件:把共享目录挂到 /app/data/local,即可在后台「本地分享」里生成提取码,无需再上传
|
||||
|
||||
@@ -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' },
|
||||
],
|
||||
},
|
||||
{
|
||||
|
||||
@@ -1,5 +1,16 @@
|
||||
# Changelog
|
||||
|
||||
## [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)
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
## [Unreleased]
|
||||
|
||||
<!-- Current release: 2.8.3 x-release-please-version -->
|
||||
<!-- Current release: 2.8.4 x-release-please-version -->
|
||||
|
||||
### Added
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
|
||||
@@ -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.
|
||||
+1
-1
@@ -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.3 # x-release-please-version
|
||||
lanol/filecodebox:2.8.4 # x-release-please-version
|
||||
```
|
||||
|
||||
Open `http://localhost:12345` and complete the first-run setup.
|
||||
|
||||
@@ -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 文件。
|
||||
|
||||
|
||||
@@ -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。
|
||||
+1
-1
@@ -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.3 # x-release-please-version
|
||||
lanol/filecodebox:2.8.4 # x-release-please-version
|
||||
```
|
||||
|
||||
打开 `http://localhost:12345`,按引导完成首次初始化。
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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.3 # x-release-please-version
|
||||
lanol/filecodebox:2.8.4 # 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
@@ -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.3 # x-release-please-version
|
||||
lanol/filecodebox:2.8.4 # 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).
|
||||
|
||||
@@ -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 \
|
||||
|
||||
@@ -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
|
||||
@@ -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()
|
||||
@@ -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):
|
||||
|
||||
@@ -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
|
||||
@@ -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()
|
||||
@@ -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()
|
||||
@@ -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()
|
||||
@@ -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
|
||||
@@ -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()
|
||||
@@ -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()
|
||||
Reference in new issue
Block a user