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
This commit is contained in:
dev 2021-10-15 17:23:50 +00:00
parent cdc6f2e416
commit 494b05c703
3 changed files with 78 additions and 5 deletions

View File

@ -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":

View File

@ -4,7 +4,6 @@
<meta charset="utf-8">
<meta name="author" content="z3bra">
<meta name="viewport" content="width=device-width">
<link rel="icon" type="image/png" href="/favicon.png" />
<title>Partage</title>
</head>
<body>
@ -13,9 +12,11 @@
Use the box below to upload and share files. File size is
limited to {{.Maxsize}}.
</p>
<form>
<form enctype="multipart/form-data" method="post">
<label for="uck">Select file(s) to share</label>
<input id="uck" type="file" multiple=true/>
<input id="uck" name="uck" type="file" multiple/>
<input id="output" name="output" type="hidden" value='html' />
<input type="submit" value="Upload!"/>
</form>
</body>
</html>

21
templates/upload.html Normal file
View File

@ -0,0 +1,21 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="z3bra">
<meta name="viewport" content="width=device-width">
<title>Partage</title>
</head>
<body>
<h1>File uploaded!</h1>
<p>
Your files have been uploaded. Access and download it from the
following links:
</p>
<ul>
{{range .Links}}
<li><a href="{{.}}">{{.}}</a></li>
{{end}}
</ul>
</body>
</html>