diff options
| -rw-r--r-- | main.go | 36 | ||||
| -rw-r--r-- | templates/user.html | 55 |
2 files changed, 67 insertions, 24 deletions
@@ -16,6 +16,7 @@ import ( "encoding/json" "encoding/base64" "sync" + "archive/zip" "github.com/satori/go.uuid" ) @@ -153,10 +154,13 @@ func index(w http.ResponseWriter, r *http.Request) { path.Join(baseDocDir, u.User, file.Name()), }) } + flasherr := consumeFlashError(w, r) data := struct { Docs []Doc + Error string }{ docs, + flasherr, } serveTemplate(w, r, "templates/user.html", data) return @@ -349,6 +353,37 @@ func handleFileServer(dir, prefix string) http.HandlerFunc { } } +func download(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case http.MethodPost: + r.ParseForm() + selection := r.Form["selection"] + if len(selection) == 0 { + sendFlashError(w, r, "/", errors.New("Aucun fichier sélectionné")) + return + } + contentDisposition := fmt.Sprintf("attachment; filename=\"Documents.zip\"") + w.Header().Set("Content-Disposition", contentDisposition) + wr := zip.NewWriter(w) + defer wr.Close() + for _, sel := range selection { + wrc, err := wr.Create(filepath.Base(sel)) + if err != nil { + sendError(w, r, err.Error(), http.StatusInternalServerError) + return + } + f, err := os.Open(sel) + if err != nil { + sendError(w, r, err.Error(), http.StatusInternalServerError) + return + } + io.Copy(wrc, f) + } + default: + sendInvalidMethod(w, r) + } +} + func upload(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodPost: @@ -408,6 +443,7 @@ func main() { http.HandleFunc("/login", login) http.HandleFunc("/logout", logout) http.HandleFunc("/upload", upload) + http.HandleFunc("/download", download) http.HandleFunc("/admin", admin) http.HandleFunc("/admin/users", adminUsers) log.Printf("Serving http://localhost:%d\n", *p) diff --git a/templates/user.html b/templates/user.html index 5e0aa57..f37c2fb 100644 --- a/templates/user.html +++ b/templates/user.html @@ -2,34 +2,41 @@ {{define "content"}} <h1>Espace utilisateur</h1> <h2>Documents</h2> +{{if .Error}} +<p class="error">{{.Error}}</p> +{{end}} <div> <form action="/upload" method="POST" enctype="multipart/form-data"> <input type="file" name="files" multiple /> <input type="submit" value="Upload" /> </form> - <table> - <tr> - <th></th> - <th>Nom du fichier</th> - <th>Date de modification</th> - <th>Taille</th> - </tr> - {{range .Docs}} - <tr> - <td> - <input type="checkbox"> - </td> - <td> - <a href="{{.Link}}">{{.Name}}</a> - </td> - <td> - {{.ModTime}} - </td> - <td> - {{.Size}} - </td> - </tr> - {{end}} - </table> + <form action="/download" method="POST"> + <table> + <tr> + <th></th> + <th>Nom du fichier</th> + <th>Date de modification</th> + <th>Taille</th> + </tr> + {{range .Docs}} + <tr> + <td> + <input type="checkbox" name="selection" value="{{.Link}}"> + </td> + <td> + <a href="{{.Link}}">{{.Name}}</a> + </td> + <td> + {{.ModTime}} + </td> + <td> + {{.Size}} + </td> + </tr> + {{end}} + </table> + <br/> + <input type="submit" value="Télécharger les fichiers sélectionnés"> + </form> </div> {{end}} |
