From 96dd4949c253ba09533cb32300a71b126cb566e1 Mon Sep 17 00:00:00 2001 From: Brian Rogers Date: Sat, 9 May 2026 09:31:04 -0600 Subject: [PATCH] finally getting info on files --- internal/app/main.go | 68 ++++++++++++++++++++++++++++++++------------ 1 file changed, 50 insertions(+), 18 deletions(-) diff --git a/internal/app/main.go b/internal/app/main.go index 7447e49..dfd5f09 100644 --- a/internal/app/main.go +++ b/internal/app/main.go @@ -4,39 +4,71 @@ import ( "fmt" "log" "os" + "path/filepath" + "time" "github.com/joho/godotenv" ) -func resolveFileStorePath(file_store_path string) { - err := os.MkdirAll(file_store_path, os.ModePerm) +func resolveFileStorePath(fileStorePath string) { + err := os.MkdirAll(fileStorePath, os.ModePerm) if err != nil { log.Fatal(err) } } type StoredFileNode struct { - Name string `json:"name"` // full file name with extension - Path string `json:"path"` // path to file - Size int64 `json:"size"` // byte size of file - Extension string `json:"extension"` // the file name extension - MimeType string `json:"mime_type"` // mime type regarless of extension - Permissions string `json:"permissions"` // UNIX permissions - IsFolder bool `json:"is_folder"` // directory flag - Modified time.Time `json:"modified_at"` // last modification date + Name string `json:"name"` // full file name with extension + Path string `json:"path"` // path to file + Size int64 `json:"size"` // byte size of file + Extension string `json:"extension"` // the file name extension + MimeType string `json:"mime_type"` // mime type regarless of extension + Permissions string `json:"permissions"` // UNIX permissions + IsFolder bool `json:"is_folder"` // directory flag + Modified time.Time `json:"modified_at"` // last modification date } -func readFilesFromFileStorePath(file_store_path string) { - files, err := os.ReadDir(file_store_path) +func readFilesFromFileStorePath(fileStorePath string) { + files, err := os.ReadDir(fileStorePath) if err != nil { log.Fatal(err) } + shownNodes := []StoredFileNode{} + for _, file := range files { - if !file.IsDir() { - fmt.Println(file.Name()) + fullPath := filepath.Join(fileStorePath, file.Name()) + fileStat, err := os.Stat(fullPath) + if err != nil { + log.Fatal(err) } + fmt.Println(fileStat.Size()) + + fileInfo, err := file.Info() // This returns an os.FileInfo + if err != nil { + continue + } + + // generate new StoredFileNode + newNode := StoredFileNode{ + Name: file.Name(), + Path: fileStorePath, + Size: fileStat.Size(), + Extension: "--", + MimeType: "--", + Permissions: "--", + IsFolder: false, + Modified: fileInfo.ModTime(), + } + + if file.IsDir() { + newNode.IsFolder = true + } + + shownNodes = append(shownNodes, newNode) } + + fmt.Println(shownNodes) } func main() { @@ -47,12 +79,12 @@ func main() { port := os.Getenv("PORT") - file_store_path := os.Getenv("STORAGE_PATH") + fileStorePath := os.Getenv("STORAGE_PATH") - resolveFileStorePath(file_store_path) + resolveFileStorePath(fileStorePath) fmt.Printf("in the future I will listen on port: %s\n", port) - fmt.Printf("Reading files from: %s\n", file_store_path) + fmt.Printf("Reading files from: %s\n", fileStorePath) - readFilesFromFileStorePath(file_store_path) + readFilesFromFileStorePath(fileStorePath) }