starting over after doing some planning
This commit is contained in:
@@ -1,120 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"drogobox.com/drogobox/cmd"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func readFilesFromFileStorePath(fileStorePath string) {
|
||||
files, err := os.ReadDir(fileStorePath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
shownNodes := []StoredFileNode{}
|
||||
|
||||
for _, file := range files {
|
||||
fullPath := filepath.Join(fileStorePath, file.Name())
|
||||
fileStat, err := os.Stat(fullPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fileInfo, err := file.Info() // This returns an os.FileInfo
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
if !file.IsDir() {
|
||||
newNode.Extension = filepath.Ext(fullPath)
|
||||
|
||||
fileHandler, err := os.Open(fullPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer fileHandler.Close()
|
||||
|
||||
// Only need the first 512 bytes for mimetype detection
|
||||
buffer := make([]byte, 512)
|
||||
_, err = fileHandler.Read(buffer)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Always returns a valid MIME type, defaults to "application/octet-stream"
|
||||
newNode.MimeType = http.DetectContentType(buffer)
|
||||
}
|
||||
|
||||
shownNodes = append(shownNodes, newNode)
|
||||
}
|
||||
|
||||
fmt.Println(shownNodes)
|
||||
}
|
||||
|
||||
func init() {
|
||||
// Lshortfile: prints "main.go:15"
|
||||
// Llongfile: prints the full path "/path/to/main.go:15"
|
||||
log.SetFlags(log.LstdFlags | log.Llongfile)
|
||||
}
|
||||
|
||||
func main() {
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
log.Fatal("Error loading .env file")
|
||||
}
|
||||
|
||||
/*port := os.Getenv("PORT")
|
||||
|
||||
fileStorePath := os.Getenv("STORAGE_PATH")
|
||||
|
||||
resolveFileStorePath(fileStorePath)
|
||||
|
||||
fmt.Printf("in the future I will listen on port: %s\n", port)
|
||||
fmt.Printf("Reading files from: %s\n", fileStorePath)
|
||||
|
||||
readFilesFromFileStorePath(fileStorePath)*/
|
||||
|
||||
cmd.Execute()
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var listCmd = &cobra.Command{
|
||||
Use: "list [directory]",
|
||||
Short: "List the contents of a directory",
|
||||
Long: "List the contents of a directory. Only shows one level deep.",
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
|
||||
fileStorePath := os.Getenv("STORAGE_PATH")
|
||||
resolveFileStorePath(fileStorePath)
|
||||
readFilesFromFileStorePath(fileStorePath)
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user