fix: Window Explorer liệt kê ổ đĩa thật + xử lý lỗi API

This commit is contained in:
2026-08-02 14:27:03 +07:00
parent d1995307f5
commit 1989bfb105
5 changed files with 79 additions and 27 deletions
+43 -10
View File
@@ -24,42 +24,75 @@ def _safe_path(path: str) -> str:
return os.path.normpath(path)
REAL_FS_TYPES = {
"ext2", "ext3", "ext4", "xfs", "btrfs", "jfs", "reiserfs",
"ntfs", "ntfs3", "vfat", "exfat", "fat", "hfs", "hfsplus", "apfs",
"zfs", "f2fs", "iso9660", "udf", "nfs", "nfs4", "cifs", "smb3", "fuseblk",
}
PSEUDO_FS_TYPES = {
"proc", "sysfs", "devpts", "tmpfs", "devtmpfs", "overlay", "squashfs",
"cgroup", "cgroup2", "pstore", "securityfs", "debugfs", "tracefs",
"configfs", "fusectl", "hugetlbfs", "mqueue", "binfmt_misc", "nsfs",
"autofs", "ramfs", "efivarfs", "rpc_pipefs", "fuse", "fusefs",
}
@router.get("/computer")
async def list_computer_roots():
"""Liệt kê các ổ đĩa / mount point của máy (My Computer)."""
"""Liệt kê các ổ đĩa / mount point thật của máy (My Computer)."""
system = platform.system()
roots = []
if system == "Windows":
import string
from ctypes import windll
for drive in string.ascii_uppercase:
root = drive + ":\\"
if os.path.exists(root):
roots.append({"path": root, "name": drive + ":", "is_dir": True})
try:
if os.path.exists(root):
roots.append({"path": root, "name": drive + ":", "is_dir": True})
except OSError:
continue
else:
# Unix/Linux/macOS: liệt kê mount points từ /proc/mounts
# Unix/Linux/macOS: chỉ liệt kê filesystem thật, bỏ pseudo/docker/systemd mounts
seen = set()
try:
with open("/proc/mounts", "r") as f:
for line in f:
parts = line.split()
if len(parts) < 2:
if len(parts) < 3:
continue
mount = parts[1]
device, mount, fstype = parts[0], parts[1], parts[2]
if fstype in PSEUDO_FS_TYPES:
continue
if fstype not in REAL_FS_TYPES:
# giữ mount point root "/" nếu không thuộc pseudo
if mount != "/":
continue
if mount in seen:
continue
seen.add(mount)
if mount.startswith("/dev") or mount.startswith("/sys") or mount.startswith("/proc"):
# lọc mount point rác kiểu /run/credentials/...
if mount.startswith("/run/") or mount.startswith("/var/lib/docker"):
continue
try:
if os.path.isdir(mount):
roots.append({"path": mount, "name": mount, "is_dir": True})
label = mount if mount != "/" else "Root (/)"
roots.append({"path": mount, "name": label, "is_dir": True})
except OSError:
pass
except OSError:
pass
# macOS fallback: liệt kê /Volumes
if system == "Darwin":
try:
for name in sorted(os.listdir("/Volumes")):
full = os.path.join("/Volumes", name)
if os.path.isdir(full):
roots.append({"path": full, "name": name, "is_dir": True})
except OSError:
pass
if not roots:
roots = [{"path": "/", "name": "/", "is_dir": True}]
roots = [{"path": "/", "name": "Root (/)", "is_dir": True}]
return {"system": system, "roots": roots}