aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorJulian Hurst <ark@mansus.space>2023-01-13 12:03:54 +0100
committerJulian Hurst <ark@mansus.space>2023-01-13 12:03:54 +0100
commit061b0402fcee0763b2eb87e5ad2a31687f95b307 (patch)
tree328b4e4b6e65a74f418db2be9f8d4b3fa4ec0b72 /main.go
parentc3de52dff91b81cfd07e00fb7fc6dd701bf8b7d7 (diff)
downloaddocspace-061b0402fcee0763b2eb87e5ad2a31687f95b307.tar.gz
Add support for downloading files in zip
Diffstat (limited to 'main.go')
-rw-r--r--main.go36
1 files changed, 36 insertions, 0 deletions
diff --git a/main.go b/main.go
index 1eacfa9..b7e8e53 100644
--- a/main.go
+++ b/main.go
@@ -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)