Files
zzyxyz_go_api/internal/vfs/vfs_impl.go
zzy 35e79e54f1 refactor(vfs): 重构VFS模块,拆分数据访问逻辑与路径解析逻辑
将原先的 `vfs.go` 文件中的功能进行拆分,创建了独立的 DAO 层文件 `vfs_dao.go`
和路径处理文件 `vfs_path.go`,以提升代码结构清晰度和可维护性。

- 将数据库操作相关方法迁移至 `VfsDAO` 结构体中
- 新增 `vfs_dao.go` 文件用于管理底层数据访问对象
- 新增 `vfs_path.go` 文件专门处理路径解析逻辑
- 移除了原 `vfs.go` 中的数据库初始化、用户及节点操作等冗余代码
2025-09-29 00:42:45 +08:00

273 lines
8.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package vfs
import (
"context"
"fmt"
"log"
"net/http"
api "git.zzyxyz.com/zzy/zzyxyz_go_api/gen/vfs_server"
"git.zzyxyz.com/zzy/zzyxyz_go_api/internal/vfs/models"
vfs_service "git.zzyxyz.com/zzy/zzyxyz_go_api/internal/vfs/services"
)
// TODO 重命名名称冲突以及合法名称检测以及JSONBody and TextBody的检测
// TODO: 特殊字符过滤
// CreateVFSNode implements server.StrictServerInterface.
func (v *VfsImpl) CreateVFSNode(ctx context.Context, request api.CreateVFSNodeRequestObject) (api.CreateVFSNodeResponseObject, error) {
var content []byte
if request.Body != nil {
content = []byte(*request.Body)
}
res, err := v.Core.CreateVFSNode(request.Params.Path, content, func(this any, node *models.VfsNode, input []byte) ([]byte, error) {
if svc, ok := this.(*vfs_service.ProxyService); ok {
result, err := svc.StrictProxy2Service(http.MethodPost, input, node)
return result, err
} else {
// 可选日志记录或错误处理
log.Println("this is not of type ProxyService")
return nil, nil
}
}, v.Proxy)
if err != nil {
log.Printf("Error creating VFS node: %+v", err)
return api.CreateVFSNode500JSONResponse{
ServerInternalErrorJSONResponse: api.ServerInternalErrorJSONResponse{
Errtype: api.ErrorErrtypeInternalServerError,
Message: fmt.Errorf("failed to delete node: %+v", err).Error(),
},
}, nil
}
node := res.Node
// 返回JSON响应
return api.CreateVFSNode201JSONResponse{
Name: node.Name,
Type: ModelType2ResponseType(node.Type),
CreatedAt: node.CreatedAt,
UpdatedAt: node.UpdatedAt,
}, nil
}
// GetVFSNode implements server.StrictServerInterface.
func (v *VfsImpl) GetVFSNode(ctx context.Context, request api.GetVFSNodeRequestObject) (api.GetVFSNodeResponseObject, error) {
res, err := v.Core.GetVFSNode(request.Params.Path, func(this any, node *models.VfsNode, input []byte) ([]byte, error) {
if svc, ok := this.(*vfs_service.ProxyService); ok {
result, err := svc.StrictProxy2Service(http.MethodGet, nil, node)
return result, err
} else {
// 可选日志记录或错误处理
log.Println("this is not of type ProxyService")
return nil, nil
}
}, v.Proxy)
if err != nil {
return api.GetVFSNode500JSONResponse{
ServerInternalErrorJSONResponse: api.ServerInternalErrorJSONResponse{
Errtype: api.ErrorErrtypeInternalServerError,
Message: fmt.Errorf("failed to delete node: %+v", err).Error(),
},
}, nil
}
if res.Node.Type == models.VfsNodeTypeDirectory {
// 手动构造响应对象
entries := make([]api.VFSDirectoryEntry, len(res.Entries))
for i, entry := range res.Entries {
entries[i] = api.VFSDirectoryEntry{
Name: entry.Name,
Type: ModelType2ResponseType(entry.Type),
}
}
return api.GetVFSNode200JSONResponse(entries), nil
} else {
return api.GetVFSNode200TextResponse(res.Content), nil
}
}
// DeleteVFSNode implements server.StrictServerInterface.
func (v *VfsImpl) DeleteVFSNode(ctx context.Context, request api.DeleteVFSNodeRequestObject) (api.DeleteVFSNodeResponseObject, error) {
op := request.Params.Op
if op == nil {
_, err := v.Core.DeleteVFSNode(request.Params.Path, func(this any, node *models.VfsNode, input []byte) ([]byte, error) {
if svc, ok := this.(*vfs_service.ProxyService); ok {
result, err := svc.StrictProxy2Service(http.MethodDelete, nil, node)
return result, err
} else {
// 可选日志记录或错误处理
log.Println("this is not of type ProxyService")
return nil, nil
}
}, v.Proxy)
if err != nil {
return api.DeleteVFSNode500JSONResponse{
ServerInternalErrorJSONResponse: api.ServerInternalErrorJSONResponse{
Errtype: api.ErrorErrtypeInternalServerError,
Message: fmt.Errorf("failed to delete node: %+v", err).Error(),
},
}, nil
}
// 成功删除返回204状态
return api.DeleteVFSNode204Response{}, nil
} else if *op == api.Recursive {
_, err := v.Core.DeleteVFSNodeRecursively(request.Params.Path, func(this any, node *models.VfsNode, input []byte) ([]byte, error) {
if svc, ok := this.(*vfs_service.ProxyService); ok {
result, err := svc.StrictProxy2Service(http.MethodDelete, nil, node)
return result, err
} else {
// 可选日志记录或错误处理
log.Println("this is not of type ProxyService")
return nil, nil
}
}, v.Proxy)
if err != nil {
return api.DeleteVFSNode500JSONResponse{
ServerInternalErrorJSONResponse: api.ServerInternalErrorJSONResponse{
Errtype: api.ErrorErrtypeInternalServerError,
Message: fmt.Errorf("failed to delete node: %+v", err).Error(),
},
}, nil
}
// 成功删除返回204状态
return api.DeleteVFSNode204Response{}, nil
} else {
return api.DeleteVFSNode500JSONResponse{
ServerInternalErrorJSONResponse: api.ServerInternalErrorJSONResponse{
Errtype: api.ErrorErrtypeInternalServerError,
Message: fmt.Errorf("not suported op").Error(),
},
}, nil
}
}
// UpdateVFSNode implements server.StrictServerInterface.
func (v *VfsImpl) UpdateVFSNode(ctx context.Context, request api.UpdateVFSNodeRequestObject) (api.UpdateVFSNodeResponseObject, error) {
var content []byte
if request.JSONBody != nil {
content = []byte(*request.JSONBody)
} else if request.TextBody != nil {
content = []byte(*request.TextBody)
}
var op vfs_service.UpdateOperation
switch request.Params.Op {
case api.Change:
op = vfs_service.UpdateOperationChange
case api.Copy:
op = vfs_service.UpdateOperationCopy
case api.Move:
op = vfs_service.UpdateOperationMove
case api.Rename:
op = vfs_service.UpdateOperationRename
}
res, err := v.Core.UpdateVFSNode(request.Params.Path, op, content, func(this any, node *models.VfsNode, input []byte) ([]byte, error) {
if svc, ok := this.(*vfs_service.ProxyService); ok {
result, err := svc.StrictProxy2Service(http.MethodPut, input, node)
return result, err
} else {
// 可选日志记录或错误处理
log.Println("this is not of type ProxyService")
return nil, nil
}
}, v.Proxy)
if err != nil {
return api.UpdateVFSNode500JSONResponse{
ServerInternalErrorJSONResponse: api.ServerInternalErrorJSONResponse{
Errtype: api.ErrorErrtypeInternalServerError,
Message: fmt.Errorf("failed to delete node: %+v", err).Error(),
},
}, nil
}
// 返回更新后的节点信息
return api.UpdateVFSNode200JSONResponse{
Name: res.Name,
Type: ModelType2ResponseType(res.Type),
CreatedAt: res.CreatedAt,
UpdatedAt: res.UpdatedAt,
}, nil
}
func ModelType2ResponseType(nodeType models.VfsNodeType) api.VFSNodeType {
var reponseType api.VFSNodeType
switch nodeType {
case models.VfsNodeTypeFile:
reponseType = api.File
case models.VfsNodeTypeDirectory:
reponseType = api.Directory
case models.VfsNodeTypeService:
reponseType = api.Service
}
return reponseType
}
// CreateUser implements server.StrictServerInterface.
func (v *VfsImpl) CreateUser(ctx context.Context, request api.CreateUserRequestObject) (api.CreateUserResponseObject, error) {
// 创建用户
token, err := v.User.CreateUser(request.Username)
if err != nil || token == nil {
return api.CreateUser500JSONResponse{
ServerInternalErrorJSONResponse: api.ServerInternalErrorJSONResponse{
Errtype: api.ErrorErrtypeInternalServerError,
Message: fmt.Errorf("failed to delete node: %+v", err).Error(),
},
}, nil
}
v.Core.CreateVFSNode("/home/"+request.Username+"/", nil, nil, nil)
v.Core.CreateVFSNode("/home/"+request.Username+"/.Recycle_Bin/", nil, nil, nil)
// 返回带有token的响应
return api.CreateUser201Response{
Headers: api.CreateUser201ResponseHeaders{
XVFSToken: *token,
},
}, nil
}
// DeleteUser implements server.StrictServerInterface.
func (v *VfsImpl) DeleteUser(ctx context.Context, request api.DeleteUserRequestObject) (api.DeleteUserResponseObject, error) {
// 删除用户
if err := v.User.DeleteUser(request.Username); err != nil {
return api.DeleteUser500JSONResponse{
ServerInternalErrorJSONResponse: api.ServerInternalErrorJSONResponse{
Errtype: api.ErrorErrtypeInternalServerError,
Message: err.Error.Error(),
},
}, nil
}
// TODO: Remove User DIR
_, err := v.Core.DeleteVFSNodeRecursively("/home/"+request.Username+"/", func(this any, node *models.VfsNode, input []byte) ([]byte, error) {
if svc, ok := this.(*vfs_service.ProxyService); ok {
result, err := svc.StrictProxy2Service(http.MethodDelete, nil, node)
return result, err
} else {
// 可选日志记录或错误处理
log.Println("this is not of type ProxyService")
return nil, nil
}
}, v.Proxy)
if err != nil {
return api.DeleteUser500JSONResponse{
ServerInternalErrorJSONResponse: api.ServerInternalErrorJSONResponse{
Errtype: api.ErrorErrtypeInternalServerError,
Message: fmt.Errorf("failed to delete node: %+v", err).Error(),
},
}, nil
}
// 成功删除返回204状态
return api.DeleteUser204Response{}, nil
}
// Make sure we conform to ServerInterface
var _ api.StrictServerInterface = (*VfsImpl)(nil)