+++ /dev/null
-/*
-uploader -- simplest form file uploader
-Copyright (C) 2018-2019 Sergey Matveev <stargrave@stargrave.org>
-
-This program is free software: you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation, version 3 of the License.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program. If not, see <http://www.gnu.org/licenses/>.
-*/
-
-package main
-
-import (
- "bufio"
- "encoding/base64"
- "encoding/hex"
- "flag"
- "fmt"
- "html/template"
- "io"
- "io/ioutil"
- "log"
- "mime"
- "net"
- "net/http"
- "os"
- "os/exec"
- "strings"
- "time"
-
- "golang.org/x/crypto/blake2b"
- "golang.org/x/net/netutil"
-)
-
-const (
- WriteBufSize = 1 << 20
- FileFieldName = "fileupload"
- CommentFieldName = "comment"
-
- SendmailCmd = "/usr/sbin/sendmail"
-)
-
-var (
- Index = template.Must(template.New("index").Parse(`<html>
-<head><title>Upload</title></head><body>
-<form enctype="multipart/form-data" action="/upload/" method="post">
-<input type="file" name="{{.}}" /><br/>
-<label for="comment">Optional comment:</label>
-<textarea name="comment" cols="80" rows="25" name="comment"></textarea><br/>
-<input type="submit" />
-</form></body></html>`))
- NotifyFromAddr *string
- NotifyToAddr *string
-)
-
-func notify(filename, timestamp string, size int64, comment string) {
- if *NotifyToAddr == "" {
- return
- }
- cmd := exec.Command(SendmailCmd, *NotifyToAddr)
- cmd.Stdin = io.MultiReader(
- strings.NewReader(fmt.Sprintf(
- `From: %s
-To: %s
-Subject: %s
-MIME-Version: 1.0
-Content-Type: text/plain; charset=utf-8
-Content-Transfer-Encoding: base64
-
-`,
- *NotifyFromAddr,
- *NotifyToAddr,
- mime.BEncoding.Encode("UTF-8", fmt.Sprintf(
- "%s (%d KiB)", filename, size/1024,
- )),
- )),
- strings.NewReader(base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf(
- "Timestamp: %s\nSize: %d bytes\nComment: %s\n",
- timestamp,
- size,
- comment,
- )))),
- )
- cmd.Run()
-}
-
-func upload(w http.ResponseWriter, r *http.Request) {
- log.Println(r.RemoteAddr, "connected")
- if r.Method == http.MethodGet {
- if err := Index.Execute(w, FileFieldName); err != nil {
- log.Println(r.RemoteAddr, err)
- }
- return
- }
- mr, err := r.MultipartReader()
- if err != nil {
- log.Println(r.RemoteAddr, err)
- return
- }
- p, err := mr.NextPart()
- if err != nil {
- log.Println(r.RemoteAddr, err)
- return
- }
- if p.FormName() != FileFieldName {
- log.Println(r.RemoteAddr, "non file form field")
- return
- }
- h, err := blake2b.New512(nil)
- if err != nil {
- panic(err)
- }
- fn := time.Now().Format(time.RFC3339Nano)
- fnOrig := p.FileName()
- fd, err := os.OpenFile(fn+".part", os.O_WRONLY|os.O_CREATE, 0600)
- if err != nil {
- log.Println(r.RemoteAddr, fn, fnOrig, err)
- return
- }
- fdBuf := bufio.NewWriterSize(fd, WriteBufSize)
- mw := io.MultiWriter(fdBuf, h)
- n, err := io.Copy(mw, p)
- if err != nil {
- log.Println(r.RemoteAddr, fn, fnOrig, err)
- fd.Close()
- return
- }
- if n == 0 {
- log.Println(r.RemoteAddr, fn, fnOrig, "empty")
- os.Remove(fn + ".part")
- fd.Close()
- fmt.Fprintf(w, "Empty file")
- return
- }
- if err = fdBuf.Flush(); err != nil {
- log.Println(r.RemoteAddr, fn, fnOrig, err)
- fd.Close()
- return
- }
- fd.Close()
- sum := hex.EncodeToString(h.Sum(nil))
- if err = os.Rename(fn+".part", fn); err != nil {
- log.Println(r.RemoteAddr, fn, fnOrig, n, sum, err)
- return
- }
- fmt.Fprintf(w, "Timestamp: %s\nBytes: %d\nBLAKE2b: %s\n", fn, n, sum)
- log.Println(r.RemoteAddr, fn, fnOrig, n, sum)
- p, err = mr.NextPart()
- if err != nil || p.FormName() != CommentFieldName {
- go notify(fnOrig, fn, n, "")
- return
- }
- comment, err := ioutil.ReadAll(p)
- if err != nil || len(comment) == 0 {
- go notify(fnOrig, fn, n, "")
- return
- }
- ioutil.WriteFile(fn+".txt", comment, os.FileMode(0600))
- go notify(fnOrig, fn, n, string(comment))
-}
-
-func main() {
- bind := flag.String("bind", "[::]:8086", "Address to bind to")
- conns := flag.Int("conns", 2, "Maximal number of connections")
- NotifyFromAddr = flag.String("notify-from", "uploader@example.com", "Address notifications are send to")
- NotifyToAddr = flag.String("notify-to", "", "Address notifications are send from")
- flag.Parse()
- if len(*NotifyFromAddr) == 0 && len(*NotifyToAddr) > 0 {
- log.Fatalln("notify-from address can not be empty, if notify-to is set")
- }
- ln, err := net.Listen("tcp", *bind)
- if err != nil {
- log.Fatalln(err)
- }
- log.Println("listening", *bind)
- ln = netutil.LimitListener(ln, *conns)
- s := &http.Server{
- ReadHeaderTimeout: 10 * time.Second,
- MaxHeaderBytes: 10 * (1 << 10),
- }
- http.HandleFunc("/upload/", upload)
- s.Serve(ln)
-}