refactor(vfs): 重构VFS模块,拆分数据访问逻辑与路径解析逻辑
将原先的 `vfs.go` 文件中的功能进行拆分,创建了独立的 DAO 层文件 `vfs_dao.go` 和路径处理文件 `vfs_path.go`,以提升代码结构清晰度和可维护性。 - 将数据库操作相关方法迁移至 `VfsDAO` 结构体中 - 新增 `vfs_dao.go` 文件用于管理底层数据访问对象 - 新增 `vfs_path.go` 文件专门处理路径解析逻辑 - 移除了原 `vfs.go` 中的数据库初始化、用户及节点操作等冗余代码
This commit is contained in:
44
internal/vfs/models/vfs_path.go
Normal file
44
internal/vfs/models/vfs_path.go
Normal file
@ -0,0 +1,44 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"path"
|
||||
)
|
||||
|
||||
func ParsePathComponents(pathStr string) (parentPath, nodeName string, nodeType VfsNodeType, err error) {
|
||||
// FIXME: 路径判断以及update的类型判断
|
||||
abspath := path.Clean(pathStr)
|
||||
if !path.IsAbs(abspath) {
|
||||
return "", "", 0, errors.New("path must be absolute")
|
||||
}
|
||||
|
||||
// 特殊处理根路径
|
||||
if abspath == "/" {
|
||||
return "", "", VfsNodeTypeDirectory, nil
|
||||
}
|
||||
|
||||
// 判断是文件还是目录
|
||||
nodeType = VfsNodeTypeFile
|
||||
// 如果原始路径以 / 结尾,则为目录
|
||||
if len(pathStr) > 1 && pathStr[len(pathStr)-1] == '/' {
|
||||
nodeType = VfsNodeTypeDirectory
|
||||
}
|
||||
if nodeType == VfsNodeTypeFile && path.Ext(pathStr) == ".api" {
|
||||
nodeType = VfsNodeTypeService
|
||||
}
|
||||
|
||||
// 分割路径
|
||||
parentPath, nodeName = path.Split(abspath)
|
||||
|
||||
// 清理父路径
|
||||
if parentPath != "/" && parentPath != "" {
|
||||
parentPath = path.Clean(parentPath)
|
||||
}
|
||||
|
||||
// 处理特殊情况
|
||||
if parentPath == "." {
|
||||
parentPath = "/"
|
||||
}
|
||||
|
||||
return parentPath, nodeName, nodeType, nil
|
||||
}
|
Reference in New Issue
Block a user