| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- package inout
- import (
- "fmt"
- "os"
- "path/filepath"
- "strings"
- )
- type Folder struct {
- Display string
- Nume string
- Fisiere []File
- Subfoldere []Folder
- }
- type File struct {
- Nume string
- Path string
- }
- func SafeDir(path string) error {
- path = filepath.Clean(path)
- if strings.HasPrefix(path, "..") || filepath.IsAbs(path) {
- return fmt.Errorf("Invalid Path")
- }
- return nil
- }
- func GetFolderStructure(path string, f *Folder) error {
- entries, err := os.ReadDir(path)
- if err != nil {
- return err
- }
- if filepath.Clean(path) != "notes_folder" {
- f.Nume = strings.TrimPrefix(path, "notes_folder/")
- } else {
- f.Nume = "notes_folder"
- }
- f.Display = filepath.Base(path)
- for _, entry := range entries {
- if entry.IsDir() {
- sub := Folder{}
- err = GetFolderStructure(filepath.Join(path, entry.Name()), &sub)
- if err != nil {
- return err
- }
- f.Subfoldere = append(f.Subfoldere, sub)
- } else {
- fisier := File{
- Nume: entry.Name(),
- Path: filepath.Join(path, entry.Name()),
- }
- f.Fisiere = append(f.Fisiere, fisier)
- }
- }
- return nil
- }
|