diff --git a/main.go b/main.go index 56a7e87..dde5e7e 100644 --- a/main.go +++ b/main.go @@ -6,11 +6,20 @@ import ( "net/http" "os" "path/filepath" + "strings" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" ) +const DATA_DIR = "files" +const DATA_URI = "/files" + +type File struct { + Filename string + URL string +} + type Template struct { templates *template.Template } @@ -36,8 +45,8 @@ func upload(c echo.Context) error { defer src.Close() // Destination - destinationPath := filepath.Join("files", file.Filename) - destinationUrl := filepath.Join("/files", file.Filename) + destinationPath := filepath.Join(DATA_DIR, file.Filename) + destinationUrl := filepath.Join(DATA_URI, file.Filename) dst, err := os.Create(destinationPath) if err != nil { return err @@ -52,6 +61,40 @@ func upload(c echo.Context) error { return c.Render(http.StatusOK, "uploaded", map[string]string{"URI": destinationUrl}) } +func listFiles() ([]File, error) { + files := make([]File, 0) + + dirEntries, err := os.ReadDir(DATA_DIR) + if err != nil { + return files, err + } + + for _, dirEntry := range dirEntries { + if dirEntry.IsDir() { + continue + } + + if strings.HasPrefix(dirEntry.Name(), ".") { + continue + } + + files = append(files, File{ + Filename: dirEntry.Name(), + URL: filepath.Join(DATA_URI, dirEntry.Name()), + }) + } + + return files, nil +} + +func index(c echo.Context) error { + files, err := listFiles() + if err != nil { + return err + } + return c.Render(http.StatusOK, "index", map[string][]File{"Files": files}) +} + func main() { t := &Template{ templates: template.Must(template.ParseGlob("views/templates/*.html")), @@ -63,9 +106,9 @@ func main() { e.Use(middleware.Logger()) e.Use(middleware.Recover()) - e.Static("/files", "files") + e.Static(DATA_URI, DATA_DIR) e.Static("/assets", "assets") - e.File("/admin", "views/index.html") + e.GET("/admin", index) e.POST("/admin/upload", upload) e.Logger.Fatal(e.Start(":1323")) diff --git a/views/index.html b/views/templates/index.html similarity index 68% rename from views/index.html rename to views/templates/index.html index 0ed7663..fb01471 100644 --- a/views/index.html +++ b/views/templates/index.html @@ -1,3 +1,4 @@ +{{define "index"}}
@@ -10,7 +11,7 @@Enfin une solution simple que vous gérez vous même
@@ -36,6 +37,31 @@| Nom de fichier | +URL | +Actions | +
|---|---|---|
| {{ .Filename }} | +{{ .URL }} | ++ |