Spaces:
Sleeping
Sleeping
Upload 21 files
Browse files- backend/app.py +12 -2
- backend/server.py +13 -3
- frontend/assets/app.js +10 -2
backend/app.py
CHANGED
|
@@ -78,9 +78,19 @@ def api_browse_dir(path: str = "") -> Dict[str, Any]:
|
|
| 78 |
drive_path = f"{letter}:\\"
|
| 79 |
if os.path.exists(drive_path):
|
| 80 |
drives.append({"name": f"{letter}:", "path": drive_path, "type": "drive"})
|
| 81 |
-
return {"current": "", "parent": "", "items": drives, "is_root": True}
|
| 82 |
else:
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
# 规范化路径
|
| 86 |
try:
|
|
|
|
| 78 |
drive_path = f"{letter}:\\"
|
| 79 |
if os.path.exists(drive_path):
|
| 80 |
drives.append({"name": f"{letter}:", "path": drive_path, "type": "drive"})
|
| 81 |
+
return {"current": "", "parent": "", "items": drives, "is_root": True, "is_drive_list": True}
|
| 82 |
else:
|
| 83 |
+
# Linux/HF Space 环境:使用主目录
|
| 84 |
+
home_dir = os.path.expanduser("~")
|
| 85 |
+
# 如果是 HF Space 环境,检查 /home/user 或工作目录
|
| 86 |
+
if os.environ.get("SPACE_ID"):
|
| 87 |
+
# HF Space 通常在 /home/user 目录
|
| 88 |
+
if os.path.exists("/home/user"):
|
| 89 |
+
path = "/home/user"
|
| 90 |
+
else:
|
| 91 |
+
path = home_dir
|
| 92 |
+
else:
|
| 93 |
+
path = home_dir
|
| 94 |
|
| 95 |
# 规范化路径
|
| 96 |
try:
|
backend/server.py
CHANGED
|
@@ -15,11 +15,21 @@ if __name__ == "__main__":
|
|
| 15 |
|
| 16 |
# 检测是否在 HF Space 环境
|
| 17 |
space_id = os.environ.get("SPACE_ID", "")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
if space_id:
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
else:
|
| 22 |
-
print(f"[New NAI HF]
|
| 23 |
|
| 24 |
# 使用 0.0.0.0 监听以便外部访问
|
| 25 |
uvicorn.run(app, host=host, port=port, reload=False)
|
|
|
|
| 15 |
|
| 16 |
# 检测是否在 HF Space 环境
|
| 17 |
space_id = os.environ.get("SPACE_ID", "")
|
| 18 |
+
space_host = os.environ.get("SPACE_HOST", "")
|
| 19 |
+
|
| 20 |
+
print(f"[New NAI HF] 服务运行于 http://{host}:{port}")
|
| 21 |
+
|
| 22 |
if space_id:
|
| 23 |
+
# HF Space 环境
|
| 24 |
+
if space_host:
|
| 25 |
+
public_url = f"https://{space_host}"
|
| 26 |
+
else:
|
| 27 |
+
# 根据 SPACE_ID 构造 URL (格式: username-spacename)
|
| 28 |
+
public_url = f"https://{space_id.replace('/', '-')}.hf.space"
|
| 29 |
+
print(f"[New NAI HF] HF Space ID: {space_id}")
|
| 30 |
+
print(f"[New NAI HF] 公共访问地址: {public_url}")
|
| 31 |
else:
|
| 32 |
+
print(f"[New NAI HF] 本地模式 - 请使用 http://localhost:{port} 访问")
|
| 33 |
|
| 34 |
# 使用 0.0.0.0 监听以便外部访问
|
| 35 |
uvicorn.run(app, host=host, port=port, reload=False)
|
frontend/assets/app.js
CHANGED
|
@@ -974,6 +974,7 @@
|
|
| 974 |
|
| 975 |
// ===== 网页版目录浏览器 =====
|
| 976 |
let currentBrowsePath = "";
|
|
|
|
| 977 |
|
| 978 |
async function loadDirList(path = "") {
|
| 979 |
try {
|
|
@@ -994,7 +995,8 @@
|
|
| 994 |
if (!listEl || !pathInput) return;
|
| 995 |
|
| 996 |
currentBrowsePath = data.current || "";
|
| 997 |
-
|
|
|
|
| 998 |
|
| 999 |
if (!data.items || data.items.length === 0) {
|
| 1000 |
listEl.innerHTML = '<div class="dir-browser-empty">此目录为空或无子目录</div>';
|
|
@@ -1067,8 +1069,14 @@
|
|
| 1067 |
const dirConfirmBtn = byId("dir-confirm");
|
| 1068 |
if (dirConfirmBtn) {
|
| 1069 |
dirConfirmBtn.addEventListener("click", () => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1070 |
const input = byId("cfg-output-dir");
|
| 1071 |
-
if (input
|
| 1072 |
input.value = currentBrowsePath;
|
| 1073 |
autoSaveConfig(); // 自动保存
|
| 1074 |
toast("已选择保存目录: " + currentBrowsePath, "success");
|
|
|
|
| 974 |
|
| 975 |
// ===== 网页版目录浏览器 =====
|
| 976 |
let currentBrowsePath = "";
|
| 977 |
+
let isDriveListMode = false; // 标记是否在驱动器选择界面
|
| 978 |
|
| 979 |
async function loadDirList(path = "") {
|
| 980 |
try {
|
|
|
|
| 995 |
if (!listEl || !pathInput) return;
|
| 996 |
|
| 997 |
currentBrowsePath = data.current || "";
|
| 998 |
+
isDriveListMode = !!data.is_drive_list; // 是否在驱动器列表模式
|
| 999 |
+
pathInput.value = currentBrowsePath || (isDriveListMode ? "(请选择驱动器)" : "");
|
| 1000 |
|
| 1001 |
if (!data.items || data.items.length === 0) {
|
| 1002 |
listEl.innerHTML = '<div class="dir-browser-empty">此目录为空或无子目录</div>';
|
|
|
|
| 1069 |
const dirConfirmBtn = byId("dir-confirm");
|
| 1070 |
if (dirConfirmBtn) {
|
| 1071 |
dirConfirmBtn.addEventListener("click", () => {
|
| 1072 |
+
// 检查是否在驱动器列表模式(没有选择具体目录)
|
| 1073 |
+
if (isDriveListMode || !currentBrowsePath) {
|
| 1074 |
+
toast("请先进入一个目录再选择", "error");
|
| 1075 |
+
return;
|
| 1076 |
+
}
|
| 1077 |
+
|
| 1078 |
const input = byId("cfg-output-dir");
|
| 1079 |
+
if (input) {
|
| 1080 |
input.value = currentBrowsePath;
|
| 1081 |
autoSaveConfig(); // 自动保存
|
| 1082 |
toast("已选择保存目录: " + currentBrowsePath, "success");
|