From 494b05c7039c2c12f10794d1b2562cf6d73f5efa Mon Sep 17 00:00:00 2001
From: dev
Date: Fri, 15 Oct 2021 17:23:50 +0000
Subject: [PATCH] Print a report page when uploading files from a form
git-svn-id: file:///srv/svn/repo/marisa/trunk@12 d6811dac-2434-b64a-9ddc-f563ab233461
---
partage.go | 55 +++++++++++++++++++++++++++++++++++++++++--
templates/index.html | 7 +++---
templates/upload.html | 21 +++++++++++++++++
3 files changed, 78 insertions(+), 5 deletions(-)
create mode 100644 templates/upload.html
diff --git a/partage.go b/partage.go
index ce0af20..ebab500 100644
--- a/partage.go
+++ b/partage.go
@@ -14,6 +14,8 @@ import (
)
type templatedata struct {
+ Links []string
+ Size string
Maxsize string
}
@@ -115,7 +117,7 @@ func servetemplate(w http.ResponseWriter, f string, d templatedata) {
}
func uploaderPut(w http.ResponseWriter, r *http.Request) {
- // Max 15 Gb uploads
+ /* limit upload size */
if r.ContentLength > conf.maxsize {
w.WriteHeader(http.StatusRequestEntityTooLarge)
w.Write([]byte("File is too big"))
@@ -134,10 +136,57 @@ func uploaderPut(w http.ResponseWriter, r *http.Request) {
return
}
- resp := conf.baseuri + conf.filectx + filepath.Base(tmp.Name()) + "\r\n"
+ resp := conf.baseuri + conf.filectx + filepath.Base(tmp.Name())
w.Write([]byte(resp))
}
+func uploaderPost(w http.ResponseWriter, r *http.Request) {
+ /* read 32Mb at a time */
+ r.ParseMultipartForm(32 << 20)
+
+ links := []string{}
+ for _, h := range r.MultipartForm.File["uck"] {
+ if h.Size > conf.maxsize {
+ w.WriteHeader(http.StatusRequestEntityTooLarge)
+ w.Write([]byte("File is too big"))
+ return
+ }
+
+ post, err := h.Open()
+ if err != nil {
+ w.WriteHeader(http.StatusInternalServerError)
+ return
+ }
+ defer post.Close()
+
+ tmp, _ := ioutil.TempFile(conf.filepath, "*"+path.Ext(h.Filename))
+ f, err := os.Create(tmp.Name())
+ if err != nil {
+ w.WriteHeader(http.StatusInternalServerError)
+ return
+ }
+ defer f.Close()
+
+ if writefile(f, post, h.Size) < 0 {
+ w.WriteHeader(http.StatusInternalServerError)
+ return
+ }
+
+ link := conf.baseuri + conf.filectx + filepath.Base(tmp.Name())
+ links = append(links, link)
+ }
+
+ if (r.PostFormValue("output") == "html") {
+ data := templatedata{ Links: links }
+ servetemplate(w, "/upload.html", data)
+ return
+ } else {
+ for _, link := range links {
+ w.Write([]byte(link + "\r\n"))
+ }
+ }
+}
+
func uploaderGet(w http.ResponseWriter, r *http.Request) {
// r.URL.Path is sanitized regarding "." and ".."
filename := r.URL.Path
@@ -160,6 +209,8 @@ func uploaderGet(w http.ResponseWriter, r *http.Request) {
func uploader(w http.ResponseWriter, r *http.Request) {
switch r.Method {
+ case "POST":
+ uploaderPost(w, r)
case "PUT":
uploaderPut(w, r)
case "GET":
diff --git a/templates/index.html b/templates/index.html
index ed9ca1f..dbd831c 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -4,7 +4,6 @@
-
Partage
@@ -13,9 +12,11 @@
Use the box below to upload and share files. File size is
limited to {{.Maxsize}}.
-
+