Allow to remove files

This commit is contained in:
Laurent Evrard 2025-12-05 06:47:51 +01:00
parent e92b19351b
commit 0253c7ef25
2 changed files with 19 additions and 7 deletions

11
main.go
View file

@ -29,10 +29,6 @@ func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Con
}
func upload(c echo.Context) error {
//-----------
// Read file
//-----------
// Source
file, err := c.FormFile("file")
if err != nil {
@ -95,6 +91,12 @@ func index(c echo.Context) error {
return c.Render(http.StatusOK, "index", map[string][]File{"Files": files})
}
func remove(c echo.Context) error {
filename := c.FormValue("Filename")
os.Remove(filepath.Join(DATA_DIR, filename))
return c.Redirect(http.StatusTemporaryRedirect, "/admin")
}
func main() {
t := &Template{
templates: template.Must(template.ParseGlob("views/templates/*.html")),
@ -110,6 +112,7 @@ func main() {
e.Static("/assets", "assets")
e.GET("/admin", index)
e.POST("/admin/upload", upload)
e.GET("/admin/remove", remove)
e.Logger.Fatal(e.Start(":1323"))
}

View file

@ -11,7 +11,7 @@
<body>
<section class="section">
<div class="container">
<h1 class="title is-1">Upload de fichiers</h1>
<h1 class="title is-1">Hébergement de fichiers</h1>
<p class="subtitle">
Enfin une solution simple que vous gérez vous même
</p>
@ -31,7 +31,7 @@
</div>
<div class="field is-grouped">
<div class="control">
<input type="submit" class="button is-link"/>
<input type="submit" value="Uploader" class="button is-link"/>
</div>
</div>
</form>
@ -54,7 +54,16 @@
<tr>
<th>{{ .Filename }}</th>
<td><a href="{{ .URL }}">{{ .URL }}</a></td>
<td></td>
<td>
<form action="/admin/remove" method="get" enctype="multipart/form-data" onsubmit="return confirm('Êtes-vous certain ? Cette action est irréversible');">
<input type="hidden" name="Filename" value="{{ .Filename }}"/>
<div class="field is-grouped">
<div class="control">
<input type="submit" value="Supprimer" class="button is-danger is-small"/>
</div>
</div>
</form>
</td>
</tr>
{{ end }}
</tbody>