Connect FE with BE api

This commit is contained in:
2024-09-25 22:03:32 +02:00
parent a135f28fd5
commit b2e99fa39e
7 changed files with 222 additions and 64 deletions

View File

@@ -1,7 +1,6 @@
package filesystem
import (
"io/ioutil"
"os"
"path/filepath"
)
@@ -10,28 +9,52 @@ type FileSystem struct {
RootDir string
}
type FileNode struct {
Type string `json:"type"`
Name string `json:"name"`
Files []FileNode `json:"files,omitempty"`
}
func New(rootDir string) *FileSystem {
return &FileSystem{RootDir: rootDir}
}
func (fs *FileSystem) ListFilesRecursively() ([]string, error) {
var files []string
func (fs *FileSystem) ListFilesRecursively() ([]FileNode, error) {
return fs.walkDirectory(fs.RootDir)
}
err := filepath.Walk(fs.RootDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
relPath, _ := filepath.Rel(fs.RootDir, path)
files = append(files, relPath)
}
return nil
})
func (fs *FileSystem) walkDirectory(dir string) ([]FileNode, error) {
var nodes []FileNode
return files, err
entries, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
for _, entry := range entries {
if entry.IsDir() {
subdir := filepath.Join(dir, entry.Name())
subFiles, err := fs.walkDirectory(subdir)
if err != nil {
return nil, err
}
nodes = append(nodes, FileNode{
Type: "directory",
Name: entry.Name(),
Files: subFiles,
})
} else {
nodes = append(nodes, FileNode{
Type: "file",
Name: entry.Name(),
})
}
}
return nodes, nil
}
func (fs *FileSystem) GetFileContent(filePath string) ([]byte, error) {
fullPath := filepath.Join(fs.RootDir, filePath)
return ioutil.ReadFile(fullPath)
return os.ReadFile(fullPath)
}