Changeset 12 in code for trunk


Ignore:
Timestamp:
Oct 15, 2021, 5:23:50 PM (4 years ago)
Author:
dev
Message:

Print a report page when uploading files from a form

Location:
trunk
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/partage.go

    r11 r12  
    1515
    1616type templatedata struct {
     17        Links []string
     18        Size string
    1719        Maxsize string
    1820}
     
    116118
    117119func uploaderPut(w http.ResponseWriter, r *http.Request) {
    118         // Max 15 Gb uploads
     120        /* limit upload size */
    119121        if r.ContentLength > conf.maxsize {
    120122                w.WriteHeader(http.StatusRequestEntityTooLarge)
     
    135137        }
    136138
    137         resp := conf.baseuri + conf.filectx + filepath.Base(tmp.Name()) + "\r\n"
     139        resp := conf.baseuri + conf.filectx + filepath.Base(tmp.Name())
    138140        w.Write([]byte(resp))
     141}
     142
     143func uploaderPost(w http.ResponseWriter, r *http.Request) {
     144        /* read 32Mb at a time */
     145        r.ParseMultipartForm(32 << 20)
     146
     147        links := []string{}
     148        for _, h := range r.MultipartForm.File["uck"] {
     149                if h.Size > conf.maxsize {
     150                        w.WriteHeader(http.StatusRequestEntityTooLarge)
     151                        w.Write([]byte("File is too big"))
     152                        return
     153                }
     154
     155                post, err := h.Open()
     156                if err != nil {
     157                        w.WriteHeader(http.StatusInternalServerError)
     158                        return
     159                }
     160                defer post.Close()
     161
     162                tmp, _ := ioutil.TempFile(conf.filepath, "*"+path.Ext(h.Filename))
     163                f, err := os.Create(tmp.Name())
     164                if err != nil {
     165                        w.WriteHeader(http.StatusInternalServerError)
     166                        return
     167                }
     168                defer f.Close()
     169
     170                if writefile(f, post, h.Size) < 0 {
     171                        w.WriteHeader(http.StatusInternalServerError)
     172                        return
     173                }
     174
     175                link := conf.baseuri + conf.filectx + filepath.Base(tmp.Name())
     176                links = append(links, link)
     177        }
     178
     179        if (r.PostFormValue("output") == "html") {
     180                data := templatedata{ Links: links }
     181                servetemplate(w, "/upload.html", data)
     182                return
     183        } else {
     184                for _, link := range links {
     185                        w.Write([]byte(link + "\r\n"))
     186                }
     187        }
    139188}
    140189
     
    161210func uploader(w http.ResponseWriter, r *http.Request) {
    162211        switch r.Method {
     212        case "POST":
     213                uploaderPost(w, r)
    163214        case "PUT":
    164215                uploaderPut(w, r)
  • trunk/templates/index.html

    r11 r12  
    55        <meta name="author" content="z3bra">
    66        <meta name="viewport" content="width=device-width">
    7         <link rel="icon" type="image/png" href="/favicon.png" />
    87        <title>Partage</title>
    98</head>
     
    1413                limited to {{.Maxsize}}.
    1514        </p>
    16         <form>
     15        <form enctype="multipart/form-data" method="post">
    1716                <label for="uck">Select file(s) to share</label>
    18                 <input id="uck" type="file" multiple=true/>
     17                <input id="uck" name="uck" type="file" multiple/>
     18                <input id="output" name="output" type="hidden" value='html' />
    1919                <input type="submit" value="Upload!"/>
    2020        </form>
    2121</body>
     22</html>
Note: See TracChangeset for help on using the changeset viewer.