2 uploader -- simplest form file uploader
3 Copyright (C) 2018-2020 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
38 "golang.org/x/crypto/blake2b"
39 "golang.org/x/net/netutil"
43 WriteBufSize = 1 << 20
44 FileFieldName = "fileupload"
45 CommentFieldName = "comment"
47 SendmailCmd = "/usr/sbin/sendmail"
51 Index = template.Must(template.New("index").Parse(`<html>
52 <head><title>Upload</title></head><body>
54 Example command line usage:
55 curl -F fileupload=@somedata.tar.gpg [-F comment="optional comment"] http://.../upload/
56 b2sum -a blake2b somedata.tar.gpg
58 <form enctype="multipart/form-data" action="/upload/" method="post">
59 <input type="file" name="{{.}}" /><br/>
60 <label for="comment">Optional comment:</label>
61 <textarea name="comment" cols="80" rows="25" name="comment"></textarea><br/>
62 <input type="submit" />
63 </form></body></html>`))
64 NotifyFromAddr *string
68 func notify(filename, timestamp string, size int64, comment string) {
69 if *NotifyToAddr == "" {
72 cmd := exec.Command(SendmailCmd, *NotifyToAddr)
73 cmd.Stdin = io.MultiReader(
74 strings.NewReader(fmt.Sprintf(
79 Content-Type: text/plain; charset=utf-8
80 Content-Transfer-Encoding: base64
85 mime.BEncoding.Encode("UTF-8", fmt.Sprintf(
86 "%s (%d KiB)", filename, size/1024,
89 strings.NewReader(base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf(
90 "Timestamp: %s\nSize: %d bytes\nComment: %s\n",
99 func upload(w http.ResponseWriter, r *http.Request) {
100 log.Println(r.RemoteAddr, "connected")
101 if r.Method == http.MethodGet {
102 if err := Index.Execute(w, FileFieldName); err != nil {
103 log.Println(r.RemoteAddr, err)
107 mr, err := r.MultipartReader()
109 log.Println(r.RemoteAddr, err)
112 p, err := mr.NextPart()
114 log.Println(r.RemoteAddr, err)
117 if p.FormName() != FileFieldName {
118 log.Println(r.RemoteAddr, "non file form field")
121 h, err := blake2b.New512(nil)
125 fn := time.Now().Format(time.RFC3339Nano)
126 fnOrig := p.FileName()
127 fd, err := os.OpenFile(fn+".part", os.O_WRONLY|os.O_CREATE, 0600)
129 log.Println(r.RemoteAddr, fn, fnOrig, err)
132 fdBuf := bufio.NewWriterSize(fd, WriteBufSize)
133 mw := io.MultiWriter(fdBuf, h)
134 n, err := io.Copy(mw, p)
136 log.Println(r.RemoteAddr, fn, fnOrig, err)
141 log.Println(r.RemoteAddr, fn, fnOrig, "empty")
142 os.Remove(fn + ".part")
144 fmt.Fprintf(w, "Empty file")
147 if err = fdBuf.Flush(); err != nil {
148 log.Println(r.RemoteAddr, fn, fnOrig, err)
153 sum := hex.EncodeToString(h.Sum(nil))
154 if err = os.Rename(fn+".part", fn); err != nil {
155 log.Println(r.RemoteAddr, fn, fnOrig, n, sum, err)
158 fmt.Fprintf(w, "Timestamp: %s\nBytes: %d\nBLAKE2b: %s\n", fn, n, sum)
159 log.Println(r.RemoteAddr, fn, fnOrig, n, sum)
160 p, err = mr.NextPart()
161 if err != nil || p.FormName() != CommentFieldName {
162 go notify(fnOrig, fn, n, "")
165 comment, err := ioutil.ReadAll(p)
166 if err != nil || len(comment) == 0 {
167 go notify(fnOrig, fn, n, "")
170 ioutil.WriteFile(fn+".txt", comment, os.FileMode(0600))
171 go notify(fnOrig, fn, n, string(comment))
175 bind := flag.String("bind", "[::]:8086", "Address to bind to")
176 conns := flag.Int("conns", 2, "Maximal number of connections")
177 NotifyFromAddr = flag.String("notify-from", "uploader@example.com", "Address notifications are send to")
178 NotifyToAddr = flag.String("notify-to", "", "Address notifications are send from")
180 if len(*NotifyFromAddr) == 0 && len(*NotifyToAddr) > 0 {
181 log.Fatalln("notify-from address can not be empty, if notify-to is set")
183 ln, err := net.Listen("tcp", *bind)
187 log.Println("listening", *bind)
188 ln = netutil.LimitListener(ln, *conns)
190 ReadHeaderTimeout: 10 * time.Second,
191 MaxHeaderBytes: 10 * (1 << 10),
193 http.HandleFunc("/upload/", upload)