添加文件夹操作相关内容

master
dfz 8 months ago
parent e86d34ef00
commit 7e59848958

@ -375,4 +375,47 @@ file_path|form-data|string|必填
"msg": "Success"
}
}
```
#### 打开文件夹
> <font color=#FF4500>*post*/admin/file/folder_get </font>
参数字段|参数属性|类型|选项
---|---|---|---|---
user_name|form-data|string|必填
folder_path|form-data|string|必填
> 返回示例:
```json
{
"code": 200,
"data": [
{
"name": "001",
"isDir": false,
"path": "001"
},
{
"name": "002",
"isDir": false,
"path": "002"
},
{
"name": "003",
"isDir": true,
"path": "003"
},
{
"name": "222",
"isDir": true,
"path": "222"
},
{
"name": "test",
"isDir": false,
"path": "test"
}
],
"msg": "Success - 文件夹内容获取成功"
}
```

@ -62,6 +62,10 @@ const (
CurdFileUploadErrorMsg string = "文件上传失败"
CurdFileTypeErrorCode int = -400213
CurdFileTypeErrorMsg string = "文件类型错误"
CurdFolderPathErrorCode int = -400214
CurdFolderPathErrorMsg string = "文件夹路径错误"
CurdFolderNotExistCode int = -400215
CurdFolderNotExistMsg string = "文件夹不存在"
//文件上传
FilesUploadFailCode int = -400250
FilesUploadFailMsg string = "文件上传失败, 获取上传文件发生错误!"

@ -206,3 +206,26 @@ func (f *File) Delete(context *gin.Context) {
// 文件删除成功,返回成功响应给客户端
response.Success(context, consts.CurdStatusOkMsg, "文件删除成功")
}
// 打开文件夹
func (f *File) FolderGet(context *gin.Context) {
user_name := context.GetString(consts.ValidatorPrefix + "user_name")
folder_path := context.GetString(consts.ValidatorPrefix + "folder_path")
folderContents, err := file.GetFolder(user_name, folder_path)
if err != nil {
// 根据不同的错误类型,返回相应的错误响应给客户端
switch err := err.(type) {
case *os.PathError:
response.Fail(context, consts.CurdFolderPathErrorCode, consts.CurdFolderPathErrorMsg, err.Error())
default:
if err == os.ErrNotExist {
response.Fail(context, consts.CurdFolderNotExistCode, consts.CurdFolderNotExistMsg, err.Error())
}
}
return
}
// 文件夹获取成功,返回成功响应给客户端,携带文件夹内容信息
response.Success(context, consts.CurdStatusOkMsg+" - "+"文件夹内容获取成功", folderContents)
}

@ -87,5 +87,8 @@ func WebRegisterValidator() {
//删除文件
key = consts.ValidatorPrefix + "FileDelete"
containers.Set(key, file.FileDelete{})
//打开文件夹
key = consts.ValidatorPrefix + "FolderGet"
containers.Set(key, file.FolderGet{})
}

@ -0,0 +1,37 @@
package file
import (
"goskeleton/app/global/consts"
"goskeleton/app/http/controller/web"
"goskeleton/app/http/validator/core/data_transfer"
"goskeleton/app/utils/response"
"github.com/gin-gonic/gin"
)
type FolderGet struct {
// 表单参数验证结构体支持匿名结构体嵌套
BaseField
FolderPath string `form:"folder_path" json:"folder_path" binding:"required"` // 文件夹路径必填
}
// 验证器语法,参见 Register.go文件有详细说明
func (f FolderGet) CheckParams(context *gin.Context) {
//1.基本的验证规则没有通过
if err := context.ShouldBind(&f); err != nil {
response.ValidatorError(context, err)
return
}
// 该函数主要是将本结构体的字段(成员)按照 consts.ValidatorPrefix+ json标签对应的 键 => 值 形式绑定在上下文,便于下一步(控制器)可以直接通过 context.Get(键) 获取相关值
extraAddBindDataContext := data_transfer.DataAddContext(f, consts.ValidatorPrefix, context)
if extraAddBindDataContext == nil {
response.ErrorSystem(context, "FileGet表单验证器json化失败", "")
} else {
// 验证完成,调用控制器,并将验证器成员(字段)递给控制器,保持上下文数据一致性
(&web.File{}).FolderGet(extraAddBindDataContext)
}
}

@ -0,0 +1,40 @@
package file
import (
"os"
"path/filepath"
)
// 存储文件夹中每个文件或文件夹的相关信息
type FolderContentInfo struct {
Name string `json:"name"`
IsDir bool `json:"isDir"`
Path string `json:"path"`
}
// 遍历并返回指定文件夹下的内容
func GetFolder(userName, folderPath string) ([]FolderContentInfo, error) {
var contents []FolderContentInfo
baseDir, err := os.Getwd()
if err != nil {
return nil, err
}
fullFolderPath := filepath.Join(baseDir, "file_library", userName, folderPath)
fileInfos, err := os.ReadDir(fullFolderPath)
if err != nil {
return nil, err
}
for _, fileInfo := range fileInfos {
relativePath := fileInfo.Name()
contentInfo := FolderContentInfo{
Name: fileInfo.Name(),
IsDir: fileInfo.IsDir(),
Path: relativePath,
}
contents = append(contents, contentInfo)
}
return contents, nil
}

@ -127,6 +127,8 @@ func InitWebRouter_Co() *gin.Engine {
file.POST("file_delete", validatorFactory.Create(consts.ValidatorPrefix+"FileDelete"))
//创建文件夹
file.POST("folder_creat", validatorFactory.Create(consts.ValidatorPrefix+"FolderCreat"))
//打开文件夹
file.GET("folder_get", validatorFactory.Create(consts.ValidatorPrefix+"FolderGet"))
}
}

Loading…
Cancel
Save