feat: 存储设置改为宿主机路径 — relay挂载宿主机根(-v /:/hostfs), 管理后台直接修改宿主机目录(vol1↔vol2), 数据宿主机级自动迁移, 系统目录黑名单
This commit is contained in:
1 parent
80a220f386
commit
4281566db9
4 files changed
+52
-27
No files matched your search
@@ -610,20 +610,30 @@ import sqlite3
|
||||
import uuid
|
||||
|
||||
MAX_UPLOAD = int(os.environ.get("MAX_UPLOAD_MB", "50")) * 1024 * 1024
|
||||
# storage root: /app/data/config.json may override it (admin panel -> storage settings)
|
||||
# storage model: admin panel edits the HOST path; the container sees it under HOST_PREFIX (-v /:/hostfs)
|
||||
HOST_PREFIX = os.environ.get("HOST_PREFIX", "/hostfs")
|
||||
DEFAULT_HOST_ROOT = os.environ.get("DEFAULT_HOST_ROOT", "/vol1/1000/HD1/docker/it-tools-relay/data")
|
||||
_BOOT_DATA_DIR = "/app/data"
|
||||
|
||||
|
||||
def _host_to_container(host_path: str) -> str:
|
||||
if os.path.isdir(HOST_PREFIX):
|
||||
return (HOST_PREFIX + host_path).rstrip("/")
|
||||
return host_path # fallback: legacy deployment without host root mount
|
||||
|
||||
|
||||
def _load_storage_root() -> str:
|
||||
cfg = os.path.join(_BOOT_DATA_DIR, "config.json")
|
||||
cfg = os.path.join(_host_to_container(DEFAULT_HOST_ROOT), "config.json")
|
||||
try:
|
||||
with open(cfg) as f:
|
||||
root = json.load(f).get("storage_root")
|
||||
if root and os.path.isdir(root):
|
||||
return root.rstrip("/")
|
||||
if root:
|
||||
container = _host_to_container(root)
|
||||
if os.path.isdir(container):
|
||||
return container
|
||||
except Exception:
|
||||
pass
|
||||
return _BOOT_DATA_DIR
|
||||
return _host_to_container(DEFAULT_HOST_ROOT)
|
||||
|
||||
|
||||
DATA_DIR = _load_storage_root()
|
||||
@@ -1081,23 +1091,30 @@ def admin_api(method: str, sub: str, payload: dict, handler):
|
||||
total += os.path.getsize(fp)
|
||||
except OSError:
|
||||
pass
|
||||
# current host path (strip prefix)
|
||||
host_root = DATA_DIR
|
||||
if os.path.isdir(HOST_PREFIX) and DATA_DIR.startswith(HOST_PREFIX):
|
||||
host_root = DATA_DIR[len(HOST_PREFIX):] or "/"
|
||||
return 200, {"code": 200, "message": "ok", "data": {
|
||||
"root": DATA_DIR,
|
||||
"root": host_root,
|
||||
"container_root": DATA_DIR,
|
||||
"users_dir": USERS_ROOT,
|
||||
"size": total,
|
||||
"writable": os.access(os.path.dirname(DATA_DIR) if DATA_DIR != _BOOT_DATA_DIR else _BOOT_DATA_DIR, os.W_OK),
|
||||
"boot_root": _BOOT_DATA_DIR,
|
||||
"host_mounted": os.path.isdir(HOST_PREFIX),
|
||||
}}
|
||||
|
||||
if method == "POST" and sub == "/storage":
|
||||
new_root = str(payload.get("root") or "").strip().rstrip("/")
|
||||
if not new_root or not new_root.startswith("/"):
|
||||
return 400, {"code": 400, "message": "请输入绝对路径"}
|
||||
new_host_root = str(payload.get("root") or "").strip().rstrip("/")
|
||||
if not new_host_root or not new_host_root.startswith("/"):
|
||||
return 400, {"code": 400, "message": "请输入宿主机绝对路径,如 /vol1/xxx 或 /vol2/xxx"}
|
||||
# block dangerous roots
|
||||
banned = ("/bin", "/sbin", "/usr", "/etc", "/proc", "/sys", "/dev", "/boot", "/lib")
|
||||
banned = ("/bin", "/sbin", "/usr", "/etc", "/proc", "/sys", "/dev", "/boot", "/lib", "/root", "/home")
|
||||
for b in banned:
|
||||
if new_root == b or new_root.startswith(b + "/"):
|
||||
if new_host_root == b or new_host_root.startswith(b + "/"):
|
||||
return 400, {"code": 400, "message": "不允许使用系统目录"}
|
||||
if not os.path.isdir(HOST_PREFIX):
|
||||
return 400, {"code": 400, "message": "容器未挂载宿主机根目录(缺少 -v /:/hostfs 挂载)"}
|
||||
new_root = _host_to_container(new_host_root)
|
||||
if new_root == DATA_DIR:
|
||||
return 400, {"code": 400, "message": "与当前路径相同,无需修改"}
|
||||
try:
|
||||
@@ -1108,7 +1125,7 @@ def admin_api(method: str, sub: str, payload: dict, handler):
|
||||
os.remove(probe)
|
||||
except Exception as e:
|
||||
return 400, {"code": 400, "message": f"目标路径不可写: {e}"}
|
||||
# migrate existing data (users dir + db files + config)
|
||||
# migrate existing data (users dir + db files)
|
||||
moved = []
|
||||
old_root = DATA_DIR
|
||||
for item in ("users",):
|
||||
@@ -1123,11 +1140,12 @@ def admin_api(method: str, sub: str, payload: dict, handler):
|
||||
if os.path.exists(src) and not os.path.exists(dst):
|
||||
shutil.move(src, dst)
|
||||
moved.append(item)
|
||||
# persist config (always in boot dir so it survives)
|
||||
cfg = os.path.join(_BOOT_DATA_DIR, "config.json")
|
||||
# persist config in the default host root (survives data root moves)
|
||||
cfg = os.path.join(_host_to_container(DEFAULT_HOST_ROOT), "config.json")
|
||||
try:
|
||||
os.makedirs(os.path.dirname(cfg), exist_ok=True)
|
||||
with open(cfg, "w") as f:
|
||||
json.dump({"storage_root": new_root}, f)
|
||||
json.dump({"storage_root": new_host_root}, f)
|
||||
except Exception:
|
||||
return 500, {"code": 500, "message": "配置写入失败"}
|
||||
# restart to take effect (restart always brings the container back)
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
<script setup lang="ts">
|
||||
import { useStyleStore } from '@/stores/style.store';
|
||||
|
||||
withDefaults(defineProps<{ hideSider?: boolean }>(), { hideSider: false });
|
||||
|
||||
const styleStore = useStyleStore();
|
||||
const { isMenuCollapsed, isSmallScreen } = toRefs(styleStore);
|
||||
const siderPosition = computed(() => (isSmallScreen.value ? 'absolute' : 'static'));
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<n-layout has-sider>
|
||||
<n-layout :has-sider="!hideSider">
|
||||
<n-layout-sider
|
||||
v-if="!hideSider"
|
||||
bordered
|
||||
collapse-mode="width"
|
||||
:collapsed-width="0"
|
||||
|
||||
@@ -27,6 +27,10 @@ const { t } = useI18n();
|
||||
const toolStore = useToolStore();
|
||||
const { favoriteTools, toolsByCategory } = storeToRefs(toolStore);
|
||||
|
||||
// 全屏页面:不显示工具侧边栏
|
||||
const NO_SIDEBAR = new Set(['/user-center', '/admin']);
|
||||
const fullBleed = computed(() => NO_SIDEBAR.has(route.path));
|
||||
|
||||
const tools = computed<ToolCategory[]>(() => [
|
||||
...(favoriteTools.value.length > 0 ? [{ name: t('tools.categories.favorite-tools'), components: favoriteTools.value }] : []),
|
||||
...toolsByCategory.value,
|
||||
|
||||
@@ -454,32 +454,32 @@ function switchTab(id: 'overview' | 'files' | 'usage' | 'storage') {
|
||||
<h3 class="adm-h3">💾 文件存储设置 <span class="adm-sub">用户上传文件与云端数据的实际存放位置</span></h3>
|
||||
<div class="adm-storage-info" v-if="storage">
|
||||
<div class="adm-storage-row">
|
||||
<span class="adm-storage-label">当前存储根目录</span>
|
||||
<span class="adm-storage-label">当前存储目录</span>
|
||||
<code class="adm-storage-path">{{ storage.root }}</code>
|
||||
</div>
|
||||
<div class="adm-storage-row">
|
||||
<span class="adm-storage-label">用户文件目录</span>
|
||||
<code class="adm-storage-path">{{ storage.users_dir }}/<用户名>/files/<工具名>/</code>
|
||||
<span class="adm-storage-label">用户文件结构</span>
|
||||
<code class="adm-storage-path">{{ storage.root }}/users/<用户名>/files/<工具名>/</code>
|
||||
</div>
|
||||
<div class="adm-storage-row">
|
||||
<span class="adm-storage-label">当前总占用</span>
|
||||
<b>{{ fmtSize(storage.size) }}</b>
|
||||
</div>
|
||||
<div class="adm-storage-tip">
|
||||
💡 说明:容器内路径由宿主机挂载而来(当前部署为宿主机
|
||||
<code>/vol1/1000/HD1/docker/it-tools-relay/data</code> 挂载到 <code>/app/data</code>)。
|
||||
若需切换到更大磁盘,请先给容器挂载新目录(docker run -v 新目录:/app/data2),再在下方填写容器内路径。
|
||||
💡 这里显示的是<strong>宿主机真实路径</strong>,在宿主机上可直接访问(如 <code>ls {{ storage.root }}/users</code>)。
|
||||
常用场景:系统盘(/vol1)空间不足时,把存储切到数据盘(/vol2)。
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="adm-storage-edit">
|
||||
<b>修改存储路径</b>
|
||||
<input v-model="newRoot" class="adm-storage-input" placeholder="容器内绝对路径,如 /app/data2">
|
||||
<b>修改宿主机存储目录</b>
|
||||
<input v-model="newRoot" class="adm-storage-input" placeholder="宿主机绝对路径,如 /vol2/1000/HD2/it-tools-data">
|
||||
<button class="adm-btn adm-btn-sm" @click="saveStorage">保存并重启</button>
|
||||
<span v-if="adminMsg" class="adm-ok">{{ adminMsg }}</span>
|
||||
</div>
|
||||
<div class="adm-storage-note">
|
||||
修改后:现有用户文件与数据库自动迁移到新路径,服务自动重启生效(约几秒)。系统目录(/etc、/usr 等)禁止使用。
|
||||
修改后:现有用户文件与数据库<strong>自动迁移</strong>到新目录(宿主机上直接移动),服务自动重启生效(约几秒)。
|
||||
系统目录(/etc、/usr、/root 等)禁止使用;请确保目标磁盘已挂载(如 /vol1、/vol2)。
|
||||
</div>
|
||||
</template>
|
||||
|
||||
|
||||
Reference in new issue
Block a user