You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
879 B
41 lines
879 B
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
|
|
}
|