parent
e86d34ef00
commit
7e59848958
@ -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
|
||||
}
|
Loading…
Reference in new issue