]> Sergey Matveev's repositories - uploader.git/blob - main.go
Example command line usage
[uploader.git] / main.go
1 /*
2 uploader -- simplest form file uploader
3 Copyright (C) 2018-2020 Sergey Matveev <stargrave@stargrave.org>
4
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.
8
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.
13
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/>.
16 */
17
18 package main
19
20 import (
21         "bufio"
22         "encoding/base64"
23         "encoding/hex"
24         "flag"
25         "fmt"
26         "html/template"
27         "io"
28         "io/ioutil"
29         "log"
30         "mime"
31         "net"
32         "net/http"
33         "os"
34         "os/exec"
35         "strings"
36         "time"
37
38         "golang.org/x/crypto/blake2b"
39         "golang.org/x/net/netutil"
40 )
41
42 const (
43         WriteBufSize     = 1 << 20
44         FileFieldName    = "fileupload"
45         CommentFieldName = "comment"
46
47         SendmailCmd = "/usr/sbin/sendmail"
48 )
49
50 var (
51         Index = template.Must(template.New("index").Parse(`<html>
52 <head><title>Upload</title></head><body>
53 <pre>
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
57 </pre>
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
65         NotifyToAddr   *string
66 )
67
68 func notify(filename, timestamp string, size int64, comment string) {
69         if *NotifyToAddr == "" {
70                 return
71         }
72         cmd := exec.Command(SendmailCmd, *NotifyToAddr)
73         cmd.Stdin = io.MultiReader(
74                 strings.NewReader(fmt.Sprintf(
75                         `From: %s
76 To: %s
77 Subject: %s
78 MIME-Version: 1.0
79 Content-Type: text/plain; charset=utf-8
80 Content-Transfer-Encoding: base64
81
82 `,
83                         *NotifyFromAddr,
84                         *NotifyToAddr,
85                         mime.BEncoding.Encode("UTF-8", fmt.Sprintf(
86                                 "%s (%d KiB)", filename, size/1024,
87                         )),
88                 )),
89                 strings.NewReader(base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf(
90                         "Timestamp: %s\nSize: %d bytes\nComment: %s\n",
91                         timestamp,
92                         size,
93                         comment,
94                 )))),
95         )
96         cmd.Run()
97 }
98
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)
104                 }
105                 return
106         }
107         mr, err := r.MultipartReader()
108         if err != nil {
109                 log.Println(r.RemoteAddr, err)
110                 return
111         }
112         p, err := mr.NextPart()
113         if err != nil {
114                 log.Println(r.RemoteAddr, err)
115                 return
116         }
117         if p.FormName() != FileFieldName {
118                 log.Println(r.RemoteAddr, "non file form field")
119                 return
120         }
121         h, err := blake2b.New512(nil)
122         if err != nil {
123                 panic(err)
124         }
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)
128         if err != nil {
129                 log.Println(r.RemoteAddr, fn, fnOrig, err)
130                 return
131         }
132         fdBuf := bufio.NewWriterSize(fd, WriteBufSize)
133         mw := io.MultiWriter(fdBuf, h)
134         n, err := io.Copy(mw, p)
135         if err != nil {
136                 log.Println(r.RemoteAddr, fn, fnOrig, err)
137                 fd.Close()
138                 return
139         }
140         if n == 0 {
141                 log.Println(r.RemoteAddr, fn, fnOrig, "empty")
142                 os.Remove(fn + ".part")
143                 fd.Close()
144                 fmt.Fprintf(w, "Empty file")
145                 return
146         }
147         if err = fdBuf.Flush(); err != nil {
148                 log.Println(r.RemoteAddr, fn, fnOrig, err)
149                 fd.Close()
150                 return
151         }
152         fd.Close()
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)
156                 return
157         }
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, "")
163                 return
164         }
165         comment, err := ioutil.ReadAll(p)
166         if err != nil || len(comment) == 0 {
167                 go notify(fnOrig, fn, n, "")
168                 return
169         }
170         ioutil.WriteFile(fn+".txt", comment, os.FileMode(0600))
171         go notify(fnOrig, fn, n, string(comment))
172 }
173
174 func main() {
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")
179         flag.Parse()
180         if len(*NotifyFromAddr) == 0 && len(*NotifyToAddr) > 0 {
181                 log.Fatalln("notify-from address can not be empty, if notify-to is set")
182         }
183         ln, err := net.Listen("tcp", *bind)
184         if err != nil {
185                 log.Fatalln(err)
186         }
187         log.Println("listening", *bind)
188         ln = netutil.LimitListener(ln, *conns)
189         s := &http.Server{
190                 ReadHeaderTimeout: 10 * time.Second,
191                 MaxHeaderBytes:    10 * (1 << 10),
192         }
193         http.HandleFunc("/upload/", upload)
194         s.Serve(ln)
195 }