diff options
Diffstat (limited to 'main.go')
| -rw-r--r-- | main.go | 36 |
1 files changed, 36 insertions, 0 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) |
