Compare commits

..
Author SHA1 Message Date
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
26 changed files with 206 additions and 646 deletions

No files matched your search

+1 -1
View File
@@ -43,7 +43,7 @@ jobs:
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
+1 -1
View File
@@ -1,3 +1,3 @@
{
".": "2.7.0"
".": "2.6.0"
}
+1 -1
View File
@@ -1 +1 @@
2.7.0
2.6.0
+94 -106
View File
@@ -1,32 +1,28 @@
import asyncio
import hashlib
import io
from pathlib import Path
import os
import time
import uuid
from datetime import datetime, timedelta
from typing import Any, Optional
from core.response import APIResponse
from core.storage import FileStorageInterface, storages
from core.storage import FileStorageInterface, StoredFile, storages
from core.settings import (
ADMIN_SESSION_EXPIRE_MAX,
ADMIN_SESSION_EXPIRE_MIN,
settings,
)
from apps.base.config import refresh_settings
from apps.base.services import get_stored_download, response_from_download, stored_file_of
from apps.base.services import response_from_download, stored_file_of
from core.security import INTERNAL_CONFIG_KEYS, generate_jwt_secret
from apps.base.models import FileCodes, KeyValue
from apps.base.utils import get_expire_info
from apps.base.local_share import (
LOCAL_REF_MARKER,
MAX_LIST_ENTRIES,
format_local_ctime,
get_local_root,
is_local_ref,
normalize_local_relpath,
resolve_under_local,
should_skip_storage_delete,
)
from apps.base.utils import get_expire_info, get_file_path_name
from apps.base.quota import release_storage, reserve_storage
from fastapi import HTTPException
from core.settings import data_root
from core.utils import get_now, hash_password, is_password_hashed, validate_background_url
# KeyValue 里的 settings/activities/presets 都是整块 JSON 读-改-写;
@@ -102,7 +98,7 @@ class FileService:
return f"{self.FILE_METADATA_KEY_PREFIX}{file_id}"
async def _delete_file_code(self, file_code: FileCodes):
if not should_skip_storage_delete(file_code):
if file_code.text is None:
await self.file_storage.delete_file(stored_file_of(file_code))
await KeyValue.filter(key=self._file_metadata_key(file_code.id)).delete()
await file_code.delete()
@@ -502,7 +498,7 @@ class FileService:
"file_count": 0,
"chunked_count": 0,
**self._empty_health_summary(),
"storage_used": sum(file_code.size for file_code in all_files if not is_local_ref(file_code)),
"storage_used": sum(file_code.size for file_code in all_files),
"used_count": sum(file_code.used_count for file_code in all_files),
}
@@ -607,7 +603,6 @@ class FileService:
"file_hash": file_code.file_hash,
"is_chunked": file_code.is_chunked,
"upload_id": file_code.upload_id,
"is_local_ref": is_local_ref(file_code),
}
data.update(
{
@@ -1417,7 +1412,7 @@ class FileService:
if file_code.text:
return APIResponse(detail=file_code.text)
else:
return response_from_download(await get_stored_download(file_code, self.file_storage))
return response_from_download(await self.file_storage.get_file_response(stored_file_of(file_code)))
async def preview_file(self, file_id: int, max_chars: int = 4000):
max_chars = min(max(max_chars, 1), 20000)
@@ -1448,28 +1443,40 @@ class FileService:
if not await local_file.exists():
raise HTTPException(status_code=404, detail="文件不存在")
expired_at, expired_count, used_count, code = await get_expire_info(
item.expire_value, item.expire_style
)
name = local_file.name
suffix = Path(name).suffix
prefix = name[: len(name) - len(suffix)] if suffix else name
record = await FileCodes.create(
code=code,
prefix=prefix,
suffix=suffix,
uuid_file_name=local_file.file,
file_path=LOCAL_REF_MARKER,
size=local_file.size or 0,
expired_at=expired_at,
expired_count=expired_count,
used_count=used_count,
)
reservation_token = f"local:{uuid.uuid4().hex}"
await reserve_storage(reservation_token, local_file.size, ttl_seconds=3600)
try:
data = await local_file.read() # bytes(read 内部用 with 关闭句柄)
expired_at, expired_count, used_count, code = await get_expire_info(
item.expire_value, item.expire_style
)
path, suffix, prefix, uuid_file_name, save_path = await get_file_path_name(
item
)
await self.file_storage.save_file(io.BytesIO(data), save_path)
try:
await FileCodes.create(
code=code,
prefix=prefix,
suffix=suffix,
uuid_file_name=uuid_file_name,
file_path=path,
size=local_file.size,
expired_at=expired_at,
expired_count=expired_count,
used_count=used_count,
)
except Exception:
await self.file_storage.delete_file(
StoredFile(file_path=path, uuid_file_name=uuid_file_name)
)
raise
finally:
await release_storage(reservation_token)
return {
"code": code,
"name": name,
"path": local_file.file,
"id": record.id,
"name": local_file.file,
}
@@ -1560,16 +1567,10 @@ class ConfigService:
detail="storage_limit 不能小于 0",
)
# 只校验"发生变化"的值:升级前存入的旧格式 background(相对路径、含空格
# 或括号)在旧版本是合法的,若每次保存都重新校验,存量部署会连无关设置项
# 都保存不了(一律 400)。渲染侧仍然 html 转义,而任何修改都必须通过校验。
current_background = str(settings.background or "")
candidate_background = str(next_config.get("background") or "")
if candidate_background != current_background:
try:
validate_background_url(candidate_background)
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc))
try:
validate_background_url(next_config.get("background", ""))
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc))
if admin_password_changed:
next_config["jwt_secret"] = generate_jwt_secret()
@@ -1580,55 +1581,18 @@ class ConfigService:
class LocalFileService:
async def list_files(self, path: str = ""):
relpath = normalize_local_relpath(path, allow_empty=True)
directory = resolve_under_local(relpath)
if not directory.exists() or not directory.is_dir():
raise HTTPException(status_code=404, detail="目录不存在")
root = get_local_root()
items = []
try:
children = list(directory.iterdir())
except OSError as exc:
raise HTTPException(status_code=500, detail="无法读取目录") from exc
children.sort(key=lambda p: (not p.is_dir(), p.name.lower()))
truncated = False
for child in children:
if len(items) >= MAX_LIST_ENTRIES:
truncated = True
break
try:
resolved = child.resolve()
resolved.relative_to(root)
except (OSError, ValueError):
continue
if not resolved.is_file() and not resolved.is_dir():
continue
child_rel = child.name if not relpath else f"{relpath}/{child.name}"
is_dir = resolved.is_dir()
items.append(
{
"file": child.name,
"name": child.name,
"path": child_rel,
"type": "dir" if is_dir else "file",
"ctime": format_local_ctime(resolved),
"size": None if is_dir else resolved.stat().st_size,
}
)
parent = ""
if relpath:
parent_path = Path(relpath).parent.as_posix()
parent = "" if parent_path == "." else parent_path
return {
"path": relpath,
"parent": parent,
"truncated": truncated,
"items": items,
}
async def list_files(self):
files = []
if not os.path.exists(data_root / "local"):
os.makedirs(data_root / "local")
for file in os.listdir(data_root / "local"):
local_file = LocalFileClass(file)
files.append({
"file": local_file.file,
"ctime": local_file.ctime,
"size": local_file.size,
})
return files
async def delete_file(self, filename: str):
file = LocalFileClass(filename)
@@ -1640,13 +1604,39 @@ class LocalFileService:
class LocalFileClass:
def __init__(self, file):
relpath = normalize_local_relpath(file)
self.file = relpath
self.name = Path(relpath).name
self.path = resolve_under_local(relpath)
# 仅允许 data/local 目录下的单层文件名,阻断路径穿越与绝对路径访问。
raw_name = str(file or "")
normalized = Path(raw_name).as_posix()
# 输入本身不得包含路径分隔符或绝对路径形态。
if (
not raw_name
or raw_name in {".", ".."}
or normalized in {".", ".."}
or "/" in normalized
or normalized.startswith("~")
or Path(raw_name).is_absolute()
or Path(raw_name).name != raw_name
):
raise HTTPException(status_code=400, detail="非法文件名")
safe_name = Path(raw_name).name
if not safe_name or safe_name in {".", ".."}:
raise HTTPException(status_code=400, detail="非法文件名")
local_root = (data_root / "local").resolve()
candidate = (local_root / safe_name).resolve()
try:
candidate.relative_to(local_root)
except ValueError:
raise HTTPException(status_code=400, detail="非法文件路径")
self.file = safe_name
self.path = candidate
if self.path.is_file():
self.ctime = format_local_ctime(self.path)
self.size = self.path.stat().st_size
self.ctime = time.strftime(
"%Y-%m-%d %H:%M:%S", time.localtime(os.path.getctime(self.path))
)
self.size = os.path.getsize(self.path)
else:
self.ctime = None
self.size = None
@@ -1656,13 +1646,11 @@ class LocalFileClass:
return fh.read()
async def write(self, data):
with open(self.path, "wb") as f:
with open(self.path, "w") as f:
f.write(data)
async def delete(self):
if not self.path.is_file():
raise HTTPException(status_code=404, detail="文件不存在")
self.path.unlink()
os.remove(self.path)
async def exists(self):
return self.path.is_file()
+4 -10
View File
@@ -31,7 +31,6 @@ from apps.admin.schemas import (
)
from core.response import APIResponse
from apps.base.models import FileCodes, KeyValue
from apps.base.quota import owned_storage_queryset
from tortoise.expressions import Q
from tortoise.functions import Count, Sum
from apps.admin.dependencies import (
@@ -145,7 +144,7 @@ async def dashboard(file_service: FileService = Depends(get_file_service)):
# 健康摘要依赖逐行 rules engine(_build_file_status_insights),保留一次遍历。
total_files = await FileCodes.all().count()
expired_count = await FileCodes.filter(_expired_predicate(now)).count()
all_size = await _sum_size(owned_storage_queryset())
all_size = await _sum_size(FileCodes.all())
used_count = (
await FileCodes.all().annotate(total=Sum("used_count")).values("total")
)[0]["total"] or 0
@@ -155,14 +154,10 @@ async def dashboard(file_service: FileService = Depends(get_file_service)):
created_at__gte=yesterday_start, created_at__lte=yesterday_end
).count()
yesterday_size = await _sum_size(
owned_storage_queryset(
FileCodes.filter(created_at__gte=yesterday_start, created_at__lte=yesterday_end)
)
FileCodes.filter(created_at__gte=yesterday_start, created_at__lte=yesterday_end)
)
today_count = await FileCodes.filter(created_at__gte=today_start).count()
today_size = await _sum_size(
owned_storage_queryset(FileCodes.filter(created_at__gte=today_start))
)
today_size = await _sum_size(FileCodes.filter(created_at__gte=today_start))
suffix_rows = (
await FileCodes.filter(text__isnull=True)
@@ -572,10 +567,9 @@ async def file_preview(
@admin_api.get("/local/lists")
async def get_local_lists(
path: str = "",
local_file_service: LocalFileService = Depends(get_local_file_service),
):
files = await local_file_service.list_files(path)
files = await local_file_service.list_files()
return APIResponse(detail=files)
-106
View File
@@ -1,106 +0,0 @@
"""Local NAS-style share helpers.
Files under ``data/local`` can be browsed by admin and turned into extract
codes without copying them into ``data/share``. The original file is never
deleted when a share expires or is removed from the file list.
``FileCodes.file_path`` is the sentinel ``local-ref``; ``uuid_file_name``
stores the posix relative path under ``data/local``. Accidental storage
deletes therefore look at ``data/local-ref/...`` and cannot touch the
original NAS file.
"""
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
LOCAL_REF_MARKER = "local-ref"
MAX_RELPATH_LEN = 255
MAX_LIST_ENTRIES = 500
def is_local_ref(file_code) -> bool:
return (getattr(file_code, "file_path", None) or "") == LOCAL_REF_MARKER
def should_skip_storage_delete(file_code) -> bool:
return is_local_ref(file_code) or getattr(file_code, "text", None) is not None
def get_local_root() -> Path:
from core.settings import data_root
root = (Path(data_root) / "local").resolve()
root.mkdir(parents=True, exist_ok=True)
return root
def normalize_local_relpath(raw: str, *, allow_empty: bool = False) -> str:
text = str(raw or "").replace("\\", "/").strip()
if not text:
if allow_empty:
return ""
raise HTTPException(status_code=400, detail="非法文件名")
if (
text in {".", ".."}
or text.startswith("/")
or text.startswith("~")
or "\x00" in text
or ":" in text.split("/", 1)[0]
):
raise HTTPException(status_code=400, detail="非法文件名")
parts = [part for part in text.split("/") if part not in {"", "."}]
if not parts or any(part == ".." for part in parts):
raise HTTPException(status_code=400, detail="非法文件名")
relpath = "/".join(parts)
if len(relpath) > MAX_RELPATH_LEN:
raise HTTPException(status_code=400, detail="路径过长")
return relpath
def resolve_under_local(relpath: str) -> Path:
root = get_local_root()
if not relpath:
return root
candidate = (root / relpath).resolve()
try:
candidate.relative_to(root)
except ValueError as exc:
raise HTTPException(status_code=400, detail="非法文件路径") from exc
return candidate
def format_local_ctime(path: Path) -> str | None:
try:
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(path.stat().st_ctime))
except OSError:
return None
def build_local_ref_download(file_code) -> StoredDownload:
try:
relpath = normalize_local_relpath(getattr(file_code, "uuid_file_name", None) or "")
path = resolve_under_local(relpath)
except HTTPException as exc:
raise StorageError(status_code=404, detail="文件已过期删除") from exc
if not path.is_file():
raise StorageError(status_code=404, detail="文件已过期删除")
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}"}
try:
headers["Content-Length"] = str(path.stat().st_size)
except OSError:
pass
return StoredDownload(filename=filename, headers=headers, path=path)
+2 -13
View File
@@ -2,24 +2,13 @@ import datetime
from fastapi import HTTPException
from tortoise import connections
from tortoise.expressions import Q
from tortoise.functions import Sum
from apps.base.models import FileCodes, StorageReservation
from apps.base.local_share import LOCAL_REF_MARKER
from core.settings import settings
from core.utils import get_now
def owned_storage_queryset(queryset=None):
"""FileCodes that occupy FileCodeBox storage (exclude NAS local-ref rows).
SQL ``file_path != 'local-ref'`` drops NULLs, so keep null paths explicitly.
"""
qs = FileCodes.all() if queryset is None else queryset
return qs.filter(Q(file_path__isnull=True) | ~Q(file_path=LOCAL_REF_MARKER))
def _detect_sql_dialect() -> str:
"""识别当前默认连接的 SQL 方言。
@@ -67,7 +56,7 @@ def get_storage_limit() -> int:
async def get_storage_usage() -> dict[str, int | None]:
now = await get_now()
# SQL 聚合:此函数在每次上传配额检查时调用,禁止全表拉取(D3)
used_rows = await owned_storage_queryset().annotate(total=Sum("size")).values("total")
used_rows = await FileCodes.all().annotate(total=Sum("size")).values("total")
reserved_rows = await StorageReservation.filter(expires_at__gt=now).annotate(
total=Sum("size")
).values("total")
@@ -105,7 +94,7 @@ async def reserve_storage(token: str, size: int, ttl_seconds: int) -> None:
INSERT INTO storagereservation (token, size, expires_at)
SELECT {ph[0]}, {ph[1]}, {ph[2]}
WHERE (
COALESCE((SELECT SUM(size) FROM filecodes WHERE file_path IS NULL OR file_path != '{LOCAL_REF_MARKER}'), 0)
COALESCE((SELECT SUM(size) FROM filecodes), 0)
+ COALESCE((SELECT SUM(size) FROM storagereservation WHERE expires_at > {ph[3]}), 0)
+ {ph[4]}
) <= {ph[5]}
-8
View File
@@ -15,7 +15,6 @@ from core.settings import settings
from core.storage import FileStorageInterface, StoredDownload, StoredFile, storages
from apps.base.file_validation import validate_upload_file
from apps.base.local_share import build_local_ref_download, is_local_ref
from apps.base.models import FileCodes, PresignUploadSession, UploadChunk
from apps.base.quota import release_storage, reserve_storage
from apps.base.utils import build_file_path, get_expire_info
@@ -38,13 +37,6 @@ def stored_file_of(code: FileCodes) -> StoredFile:
)
async def get_stored_download(file_code, file_storage: FileStorageInterface | None = None) -> StoredDownload:
if is_local_ref(file_code):
return build_local_ref_download(file_code)
storage = file_storage or storages[settings.file_storage]()
return await storage.get_file_response(stored_file_of(file_code))
async def rollback_saved_file(
storage: FileStorageInterface,
file_path: str,
+2 -3
View File
@@ -20,9 +20,9 @@ from core.logger import logger
from core.settings import settings, data_root
from core.storage import FileStorageInterface, StoredFile, storages
from apps.base.services import stored_file_of
from apps.base.local_share import is_local_ref
from core.utils import get_now
async def delete_expire_files():
while True:
try:
@@ -42,8 +42,7 @@ async def delete_expire_files():
).all()
for exp in expire_data:
try:
if not is_local_ref(exp):
await file_storage.delete_file(stored_file_of(exp))
await file_storage.delete_file(stored_file_of(exp))
except Exception as e:
logger.error(f"删除过期文件失败 code={exp.code}: {e}")
try:
+4 -6
View File
@@ -18,7 +18,6 @@ from apps.base.quota import release_storage, reserve_storage
from apps.base.services import (
PRESIGN_SESSION_EXPIRES,
FileUploadService,
get_stored_download,
response_from_download,
stored_file_of,
validate_file_size,
@@ -37,7 +36,6 @@ from apps.base.utils import (
get_chunk_file_path_name,
validate_expire_style,
)
from apps.base.local_share import is_local_ref
from core.response import APIResponse
from core.settings import settings
from core.storage import storages, FileStorageInterface
@@ -153,8 +151,8 @@ async def build_select_detail(
metadata = build_file_metadata(file_code)
if file_code.text is not None:
download_url = None
elif file_code.expired_count >= 0 or is_local_ref(file_code):
# 次数限制与 NAS 引用都必须走下载接口,避免直链绕过次数或打到错误存储。
elif file_code.expired_count >= 0:
# 有次数限制的文件必须经过下载接口,第三方直链无法阻止重复使用。
download_url = await get_proxy_file_url(file_code.code)
else:
download_url = await file_storage.get_file_url(stored_file_of(file_code))
@@ -215,7 +213,7 @@ async def get_code_file(code: str, ip: str = Depends(ip_limit["error"])):
)
},
)
return response_from_download(await get_stored_download(file_code, file_storage))
return response_from_download(await file_storage.get_file_response(stored_file_of(file_code)))
@share_api.post("/select/")
@@ -260,7 +258,7 @@ async def download_file(key: str, code: str, ip: str = Depends(ip_limit["error"]
return (
APIResponse(detail=file_code.text)
if file_code.text
else response_from_download(await get_stored_download(file_code, file_storage))
else response_from_download(await file_storage.get_file_response(stored_file_of(file_code)))
)
+1 -3
View File
@@ -174,9 +174,7 @@ def verify_password(password: str, hashed: str) -> bool:
p=int(p),
maxmem=_SCRYPT_MAXMEM,
).hex()
# 默认解释器对超范围的 n/r/p 抛 TypeError/ValueError;部分构建会抛
# OverflowError,一并视为校验失败,避免坏掉的存量哈希影响登录路径。
except (ValueError, TypeError, OverflowError):
except (ValueError, TypeError):
return False
return hmac.compare_digest(password_hash, stored_hash)
+1 -3
View File
@@ -1,11 +1,9 @@
version: "3"
services:
file-code-box:
image: lanol/filecodebox:2.7.0 # x-release-please-version
image: lanol/filecodebox:2.6.0 # x-release-please-version
volumes:
- fcb-data:/app/data:rw
# NAS 已有文件:把共享目录挂到 /app/data/local,即可在后台「本地分享」里生成提取码,无需再上传
# - /volume1/share:/app/data/local:ro
restart: unless-stopped
ports:
- "12345:12345"
-15
View File
@@ -1,20 +1,5 @@
# Changelog
## [2.7.0](https://github.com/vastsa/FileCodeBox/compare/v2.6.1...v2.7.0) (2026-09-16)
### Features
* mint extract codes from data/local without re-upload ([93a30a2](https://github.com/vastsa/FileCodeBox/commit/93a30a257cc5b0ee23a374521e1e12602c3959da))
## [2.6.1](https://github.com/vastsa/FileCodeBox/compare/v2.6.0...v2.6.1) (2026-09-14)
### Bug Fixes
* don't let a legacy background value block settings saves ([eb2db8f](https://github.com/vastsa/FileCodeBox/commit/eb2db8f507e3198e0ae45b976bdd90eab2f7d1ed))
* 存量 background 值不再阻塞后台设置保存([#514](https://github.com/vastsa/FileCodeBox/issues/514) 升级回归) ([0d82fbe](https://github.com/vastsa/FileCodeBox/commit/0d82fbe26ad06ca77af7b2b5aa05f514e8362295))
## [2.6.0](https://github.com/vastsa/FileCodeBox/compare/v2.5.6...v2.6.0) (2026-09-14)
+1 -1
View File
@@ -1,6 +1,6 @@
## [Unreleased]
<!-- Current release: 2.7.0 x-release-please-version -->
<!-- Current release: 2.6.0 x-release-please-version -->
### Added
+31 -32
View File
@@ -166,51 +166,50 @@ When modifying extraction codes, the system checks if the new code is already in
## Local File Management
In addition to managing shared files, the admin panel has a Local Share page for browsing `data/local` (including subfolders) and minting extract codes.
Mount a NAS share at `/app/data/local` to generate extract codes for files already on disk, without downloading and re-uploading them.
```yaml
volumes:
- fcb-data:/app/data:rw
# Read-only NAS share for admin browsing and extract-code generation
- /volume1/share:/app/data/local:ro
```
In addition to managing shared files, the admin panel also provides local file management functionality for managing files in the `data/local` directory.
### View Local Files
The Local Share page supports nested folders. List API:
The local file list displays all files in the `data/local` directory:
```
GET /admin/local/lists?path=movies
```
`path` is relative to `data/local`; leave it empty for the root. Response:
| Field | Description |
|------|------|
| `path` | Current directory |
| `parent` | Parent directory |
| `truncated` | Whether the listing was truncated |
| `items[].type` | `dir` or `file` |
| `items[].path` | Path relative to `data/local`, used as `filename` when sharing |
| Information | Description |
|-------------|-------------|
| Filename | Complete filename |
| Creation Time | File creation time |
| File Size | File size (bytes) |
### Share Local Files
1. Open the target subfolder and pick a file
2. Set expiration method and value
3. Create an extract code
You can quickly share local files:
Sharing is a **zero-copy reference**: the file is not copied into `data/share` and does not count against storage quota. Expiring or deleting the share record **does not delete the original NAS file**.
1. Select the file to share in the local file list
2. Set expiration method and value
3. Click the share button
4. System generates extraction code
**Share parameters:**
| Parameter | Description |
|-----------|-------------|
| `filename` | Path relative to `data/local`, e.g. `movies/a.mp4` |
| `filename` | Filename to share |
| `expire_style` | Expiration method (day/hour/minute/forever/count) |
| `expire_value` | Expiration value (days/hours/minutes/download count) |
### Delete Local Files
You can delete files in the `data/local` directory:
1. Find the file to delete in the local file list
2. Click the delete button
3. Confirm deletion
::: tip Use Cases
Local file management is useful for:
- Sharing files after batch uploading to the server
- Managing files uploaded to the server through other means
- Cleaning up unnecessary local files
:::
## System Settings
<p align="center">
@@ -339,7 +338,7 @@ Content-Type: application/json
**Get Local File List**
```
GET /admin/local/lists?path=movies
GET /admin/local/lists
Authorization: Bearer <token>
```
@@ -350,7 +349,7 @@ Authorization: Bearer <token>
Content-Type: application/json
{
"filename": "movies/example.txt"
"filename": "example.txt"
}
```
@@ -361,7 +360,7 @@ Authorization: Bearer <token>
Content-Type: application/json
{
"filename": "movies/example.txt",
"filename": "example.txt",
"expire_style": "day",
"expire_value": 7
}
-1
View File
@@ -35,7 +35,6 @@ storage_path=
- Files are stored by default in `data/share/data/` directory
- Subdirectories are automatically created by date: `year/month/day/fileID/`
- In production, it's recommended to mount the `data/` directory to persistent storage
- Existing NAS files can be mounted at `data/local` (including subfolders). Admin Local Share references them in place to mint extract codes and does not copy them into `data/share`
## S3-Compatible Storage
+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.7.0 # x-release-please-version
lanol/filecodebox:2.6.0 # x-release-please-version
```
Open `http://localhost:12345` and complete the first-run setup.
+30 -31
View File
@@ -165,51 +165,50 @@ FileCodeBox 提供了功能完善的管理面板,让管理员可以方便地
## 本地文件管理
除了管理已分享的文件,管理面板还提供「本地分享」页面,用于浏览 `data/local`(含子目录)里的文件并生成提取码。
把 NAS 共享目录挂到容器的 `/app/data/local` 后,即可指定内部文件夹中的文件生成提取码,无需再下载后上传。
```yaml
volumes:
- fcb-data:/app/data:rw
# 只读挂载 NAS 分享目录,供后台浏览并生成提取码
- /volume1/share:/app/data/local:ro
```
除了管理已分享的文件,管理面板还提供了本地文件管理功能,用于管理 `data/local` 目录中的文件。
### 查看本地文件
后台「本地分享」支持进入子目录。列表接口:
本地文件列表显示 `data/local` 目录中的所有文件:
```
GET /admin/local/lists?path=movies
```
`path` 为相对 `data/local` 的目录,留空表示根目录。返回:
| 字段 | 说明 |
| 信息 | 说明 |
|------|------|
| `path` | 当前目录 |
| `parent` | 上级目录 |
| `truncated` | 是否因条目过多被截断 |
| `items[].type` | `dir` 或 `file` |
| `items[].path` | 相对 `data/local` 的路径,分享时作为 `filename` |
| 文件名 | 文件的完整名称 |
| 创建时间 | 文件的创建时间 |
| 文件大小 | 文件大小(字节) |
### 分享本地文件
1. 进入目标子目录,选择文件
2. 设置过期方式和过期值
3. 点击生成提取码
可以将本地文件快速分享:
分享是**零拷贝引用**:不会把文件复制进 `data/share`,也不占用快递柜存储配额。提取码过期或在文件管理中删除记录,**不会删除 NAS 原文件**。
1. 在本地文件列表中选择要分享的文件
2. 设置过期方式和过期值
3. 点击分享按钮
4. 系统生成提取码
**分享参数:**
| 参数 | 说明 |
|------|------|
| `filename` | 相对 `data/local` 的路径,如 `movies/a.mp4` |
| `filename` | 要分享的文件名 |
| `expire_style` | 过期方式(day/hour/minute/forever/count) |
| `expire_value` | 过期值(天数/小时数/分钟数/下载次数) |
### 删除本地文件
可以删除 `data/local` 目录中的文件:
1. 在本地文件列表中找到要删除的文件
2. 点击删除按钮
3. 确认删除
::: tip 使用场景
本地文件管理功能适用于:
- 批量上传文件到服务器后进行分享
- 管理通过其他方式上传到服务器的文件
- 清理不需要的本地文件
:::
## 系统设置
<p align="center">
@@ -338,7 +337,7 @@ Content-Type: application/json
**获取本地文件列表**
```
GET /admin/local/lists?path=movies
GET /admin/local/lists
Authorization: Bearer <token>
```
@@ -349,7 +348,7 @@ Authorization: Bearer <token>
Content-Type: application/json
{
"filename": "movies/example.txt"
"filename": "example.txt"
}
```
@@ -360,7 +359,7 @@ Authorization: Bearer <token>
Content-Type: application/json
{
"filename": "movies/example.txt",
"filename": "example.txt",
"expire_style": "day",
"expire_value": 7
}
-1
View File
@@ -35,7 +35,6 @@ storage_path=
- 文件默认存储在 `data/share/data/` 目录下
- 按日期自动创建子目录:`年/月/日/文件ID/`
- 建议在生产环境中将 `data/` 目录挂载到持久化存储
- NAS 已有文件可挂载到 `data/local`(含子目录)。后台「本地分享」会直接引用这些文件生成提取码,不会复制到 `data/share`
## S3 兼容存储
+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.7.0 # x-release-please-version
lanol/filecodebox:2.6.0 # x-release-please-version
```
打开 `http://localhost:12345`,按引导完成首次初始化。
+1 -1
View File
@@ -32,7 +32,7 @@ docker run -d --restart unless-stopped \
--log-opt max-size=10m \
--log-opt max-file=3 \
--name filecodebox \
lanol/filecodebox:2.7.0 # x-release-please-version
lanol/filecodebox:2.6.0 # x-release-please-version
```
访问 `http://localhost:12345`,完成首次初始化。生产环境建议固定版本号;`latest` 指向最新正式版。
+1 -1
View File
@@ -32,7 +32,7 @@ docker run -d --restart unless-stopped \
--log-opt max-size=10m \
--log-opt max-file=3 \
--name filecodebox \
lanol/filecodebox:2.7.0 # x-release-please-version
lanol/filecodebox:2.6.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.
-58
View File
@@ -73,63 +73,5 @@ class ConfigServiceBackgroundGuardTests(SettingsOverrideMixin, unittest.TestCase
await close_db()
class LegacyBackgroundUpgradeTests(SettingsOverrideMixin, unittest.TestCase):
"""存量部署升级:旧版本合法、新校验不接受的值,不得阻塞设置保存。"""
def test_unchanged_legacy_background_does_not_block_unrelated_save(self):
asyncio.run(self._unchanged_legacy_allows_save())
async def _unchanged_legacy_allows_save(self):
await init_memory_db()
try:
from apps.base.config import refresh_settings
from apps.base.models import KeyValue
# 旧版本合法(相对路径 + 空格),新的 http(s) 校验会拒绝
legacy = "/static/bg image.png"
await KeyValue.create(
key="settings", value={"background": legacy, "name": "old"}
)
await refresh_settings(force=True)
service = ConfigService()
# 修复前这里会 400,导致存量部署连无关设置项都保存不了
await service.update_config({"name": "new"})
record = await KeyValue.filter(key="settings").first()
self.assertEqual(record.value.get("background"), legacy, "旧值应原样保留")
self.assertEqual(record.value.get("name"), "new")
finally:
await close_db()
def test_changing_background_is_still_validated(self):
asyncio.run(self._change_still_validated())
async def _change_still_validated(self):
await init_memory_db()
try:
from fastapi import HTTPException
from apps.base.config import refresh_settings
from apps.base.models import KeyValue
await KeyValue.create(key="settings", value={"background": "/legacy/bg.png"})
await refresh_settings(force=True)
service = ConfigService()
# 修改为恶意值仍然被拒
with self.assertRaises(HTTPException) as ctx:
await service.update_config(
{"background": "x') ;background:url(https://evil.com/)"}
)
self.assertEqual(ctx.exception.status_code, 400)
# 修改为合法值可以通过
await service.update_config({"background": "https://ok.example/bg.png"})
record = await KeyValue.filter(key="settings").first()
self.assertEqual(record.value.get("background"), "https://ok.example/bg.png")
finally:
await close_db()
if __name__ == "__main__":
unittest.main()
+28 -236
View File
@@ -1,271 +1,63 @@
"""Local NAS share: nested browse + zero-copy extract codes (issue #532)."""
"""D2 回归:admin 本地文件分享此前必 500(read() 泄漏句柄 + save_file 误用
UploadFile 属性)。现在 save_file 接收 BinaryIO,read() 返回 bytes。"""
import asyncio
import datetime
import unittest
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest.mock import patch
from fastapi import HTTPException
from apps.admin.services import FileService, LocalFileClass, LocalFileService
from apps.base.local_share import LOCAL_REF_MARKER, is_local_ref
from apps.base.models import FileCodes
from apps.base.quota import get_storage_usage, reserve_storage
from apps.base.tasks import delete_expire_files
from tests.helpers import SettingsOverrideMixin, close_db, init_memory_db
from core.settings import settings
from core.utils import get_now
class SleepSentinel(Exception):
pass
from apps.admin.services import FileService, LocalFileClass
class LocalFileClassTests(unittest.TestCase):
def setUp(self):
self._tmpdir = TemporaryDirectory()
self.root = Path(self._tmpdir.name)
(self.root / "local").mkdir()
(self.root / "local" / "safe.txt").write_bytes(b"ok")
(self.root / "local" / "movies").mkdir()
(self.root / "local" / "movies" / "a.bin").write_bytes(b"nested")
self._patch = patch("core.settings.data_root", self.root)
self._patch.start()
def tearDown(self):
self._patch.stop()
self._tmpdir.cleanup()
def test_rejects_dotdot_filename(self):
with self.assertRaises(HTTPException) as ctx:
LocalFileClass("../etc/passwd")
self.assertEqual(ctx.exception.status_code, 400)
def test_rejects_nested_dotdot(self):
with self.assertRaises(HTTPException) as ctx:
LocalFileClass("movies/../../etc/passwd")
self.assertEqual(ctx.exception.status_code, 400)
def test_rejects_absolute_filename(self):
with self.assertRaises(HTTPException) as ctx:
LocalFileClass("/etc/passwd")
self.assertEqual(ctx.exception.status_code, 400)
def test_allows_basename_inside_local(self):
local_file = LocalFileClass("safe.txt")
self.assertTrue(asyncio.run(local_file.exists()))
self.assertEqual(local_file.file, "safe.txt")
self.assertEqual(local_file.path, (self.root / "local" / "safe.txt").resolve())
def test_allows_nested_relative(self):
local_file = LocalFileClass("movies/a.bin")
self.assertTrue(asyncio.run(local_file.exists()))
self.assertEqual(local_file.file, "movies/a.bin")
self.assertEqual(local_file.name, "a.bin")
self.assertEqual(local_file.size, 6)
class LocalFileServiceListTests(unittest.TestCase):
def test_lists_nested_dirs_and_files(self):
asyncio.run(self._scenario())
async def _scenario(self):
def test_read_returns_bytes_and_closes_handle(self):
with TemporaryDirectory() as tmpdir:
root = Path(tmpdir)
movies = root / "local" / "movies"
movies.mkdir(parents=True)
(root / "local" / "readme.txt").write_bytes(b"hi")
(movies / "a.bin").write_bytes(b"nested")
with patch("core.settings.data_root", root):
service = LocalFileService()
root_listing = await service.list_files("")
self.assertEqual(root_listing["path"], "")
names = {item["name"]: item for item in root_listing["items"]}
self.assertEqual(names["movies"]["type"], "dir")
self.assertEqual(names["readme.txt"]["type"], "file")
with patch("apps.admin.services.data_root", Path(tmpdir)), patch(
"core.storage.data_root", Path(tmpdir)
):
local = Path(tmpdir) / "local"
local.mkdir()
(local / "hello.txt").write_bytes(b"local-bytes")
nested = await service.list_files("movies")
self.assertEqual(nested["path"], "movies")
self.assertEqual(nested["parent"], "")
self.assertEqual(nested["items"][0]["path"], "movies/a.bin")
self.assertEqual(nested["items"][0]["file"], "a.bin")
lf = LocalFileClass("hello.txt")
self.assertEqual(lf.size, 11)
data = asyncio.run(lf.read())
self.assertEqual(data, b"local-bytes")
class ShareLocalFileTests(SettingsOverrideMixin, unittest.TestCase):
def test_share_local_file_is_zero_copy(self):
def test_share_local_file_creates_code_and_saves_file(self):
asyncio.run(self._scenario())
async def _scenario(self):
with TemporaryDirectory() as tmpdir:
await init_memory_db()
try:
root = Path(tmpdir)
movies = root / "local" / "movies"
movies.mkdir(parents=True)
source = movies / "doc.txt"
source.write_bytes(b"hello local share")
with patch("core.settings.data_root", root), patch(
"core.storage.data_root", root
with patch("apps.admin.services.data_root", Path(tmpdir)), patch(
"core.storage.data_root", Path(tmpdir)
):
local = Path(tmpdir) / "local"
local.mkdir()
(local / "doc.txt").write_bytes(b"hello local share")
class Item:
filename = "movies/doc.txt"
filename = "doc.txt"
expire_value = 1
expire_style = "day"
result = await FileService().share_local_file(Item())
service = FileService()
result = await service.share_local_file(Item())
self.assertIn("code", result)
self.assertEqual(result["name"], "doc.txt")
self.assertEqual(result["path"], "movies/doc.txt")
record = await FileCodes.get(code=result["code"])
self.assertTrue(is_local_ref(record))
self.assertEqual(record.file_path, LOCAL_REF_MARKER)
self.assertEqual(record.uuid_file_name, "movies/doc.txt")
self.assertTrue(source.exists())
share_files = [
p for p in (root / "share").rglob("*") if p.is_file()
] if (root / "share").exists() else []
self.assertFalse(share_files)
saved = Path(tmpdir) / "share" / "data"
saved_files = [p for p in saved.rglob("*") if p.is_file()]
self.assertTrue(saved_files, "分享后文件应已落盘")
self.assertEqual(saved_files[0].read_bytes(), b"hello local share")
finally:
await close_db()
class LocalRefCleanupTests(SettingsOverrideMixin, unittest.TestCase):
def test_expire_deletes_record_but_keeps_original(self):
asyncio.run(self._scenario())
async def _scenario(self):
with TemporaryDirectory() as tmpdir:
await init_memory_db()
try:
root = Path(tmpdir)
source = root / "local" / "keep.bin"
source.parent.mkdir(parents=True)
source.write_bytes(b"keep-me")
now = await get_now()
await FileCodes.create(
code="nas1",
prefix="keep",
suffix=".bin",
uuid_file_name="keep.bin",
file_path=LOCAL_REF_MARKER,
size=7,
expired_at=now - datetime.timedelta(days=1),
expired_count=-1,
)
def _sleep(_seconds):
raise SleepSentinel
with patch("core.settings.data_root", root), patch(
"core.storage.data_root", root
), patch("apps.base.tasks.data_root", root), patch(
"apps.base.tasks.asyncio.sleep", side_effect=_sleep
):
try:
await delete_expire_files()
except SleepSentinel:
pass
self.assertTrue(source.exists())
self.assertFalse(await FileCodes.filter(code="nas1").exists())
finally:
await close_db()
class LocalShareHttpTests(unittest.IsolatedAsyncioTestCase):
async def test_admin_browse_share_and_download(self):
import httpx
from tests.conftest import TEST_ADMIN_PASSWORD
from tests.test_api_contract import _login
import main
with TemporaryDirectory() as tmpdir:
root = Path(tmpdir)
local_dir = root / "local" / "movies"
local_dir.mkdir(parents=True)
source = local_dir / "clip.bin"
source.write_bytes(b"nas-bytes")
await init_memory_db()
try:
from apps.base.utils import ip_limit
for limiter in ip_limit.values():
limiter.ips.clear()
transport = httpx.ASGITransport(app=main.app)
async with httpx.AsyncClient(
transport=transport, base_url="http://test", follow_redirects=True
) as client:
setup = await client.post(
"/setup",
json={
"admin_password": TEST_ADMIN_PASSWORD,
"confirm_password": TEST_ADMIN_PASSWORD,
"site_name": "local-share-tests",
"expire_style": ["day", "forever", "count"],
},
headers={"Accept": "application/json"},
)
self.assertEqual(setup.status_code, 200, setup.text)
token = await _login(client)
headers = {"Authorization": f"Bearer {token}"}
with patch("core.settings.data_root", root):
listing = await client.get(
"/admin/local/lists", params={"path": "movies"}, headers=headers
)
self.assertEqual(listing.status_code, 200, listing.text)
payload = listing.json()["detail"]
self.assertEqual(payload["path"], "movies")
self.assertEqual(payload["items"][0]["path"], "movies/clip.bin")
share = await client.post(
"/admin/local/share",
json={
"filename": "movies/clip.bin",
"expire_style": "day",
"expire_value": 1,
},
headers=headers,
)
self.assertEqual(share.status_code, 200, share.text)
code = share.json()["detail"]["code"]
download = await client.get("/share/select/", params={"code": code})
self.assertEqual(download.status_code, 200, download.text)
self.assertEqual(download.content, b"nas-bytes")
self.assertTrue(source.exists())
finally:
await close_db()
class LocalRefQuotaTests(SettingsOverrideMixin, unittest.TestCase):
def test_local_ref_does_not_consume_quota(self):
asyncio.run(self._scenario())
async def _scenario(self):
settings.storage_limit = 100
await init_memory_db()
try:
await FileCodes.create(
code="nas-big",
size=10_000,
expired_count=-1,
file_path=LOCAL_REF_MARKER,
uuid_file_name="movies/huge.bin",
)
await FileCodes.create(code="owned", size=20, expired_count=-1)
usage = await get_storage_usage()
self.assertEqual(usage["used"], 20)
await reserve_storage("upload-ok", 50, 300)
usage = await get_storage_usage()
self.assertEqual(usage["reserved"], 50)
self.assertEqual(usage["available"], 30)
finally:
await close_db()
if __name__ == "__main__":
unittest.main()
-5
View File
@@ -53,11 +53,6 @@ class PasswordHashSchemeTests(unittest.TestCase):
self.assertFalse(verify_password("x", "scrypt$n$r$p$salt$zzz"))
self.assertFalse(verify_password("x", "sha256$only-two"))
self.assertFalse(verify_password("x", ""))
# 超范围的 n/r/p:解释器抛 TypeError/ValueError(部分构建抛 OverflowError),
# 都必须被吞掉并判为不匹配,而不是让登录及每个请求 500
self.assertFalse(
verify_password("x", "scrypt$99999999999999999999999999$8$1$aa$bb")
)
class TransparentRehashTests(SettingsOverrideMixin, unittest.TestCase):
+1 -1
View File
@@ -39,7 +39,7 @@ class LocalFilePathTraversalTests(unittest.TestCase):
self.local_root.mkdir(parents=True, exist_ok=True)
(self.local_root / "safe.txt").write_text("ok", encoding="utf-8")
self._data_root_patch = patch(
"core.settings.data_root", Path(self._tmpdir.name)
"apps.admin.services.data_root", Path(self._tmpdir.name)
)
self._data_root_patch.start()