]> Sergey Matveev's repositories - uploader.git/blobdiff - src/uploader/main.go
Copyleft
[uploader.git] / src / uploader / main.go
index ed39c3d8ada682bdd511d124186ca9d6527a9c97..036b6cb316f9d3107aecb65a1afd3dd9deafd5c2 100644 (file)
@@ -1,40 +1,97 @@
 /*
 uploader -- simplest form file uploader
-Copyright (C) 2018 Sergey Matveev <stargrave@stargrave.org>
+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, either version 3 of the License, or
+(at your option) any later version.
+
+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/blake2s"
+       "golang.org/x/crypto/blake2b"
        "golang.org/x/net/netutil"
 )
 
 const (
-       WriteBufSize  = 1 << 20
-       FileFieldName = "fileupload"
+       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="{{.}}" /><input type="submit" />
+<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(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)", timestamp, 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 {
@@ -57,7 +114,7 @@ func upload(w http.ResponseWriter, r *http.Request) {
                log.Println(r.RemoteAddr, "non file form field")
                return
        }
-       h, err := blake2s.New256(nil)
+       h, err := blake2b.New256(nil)
        if err != nil {
                panic(err)
        }
@@ -75,6 +132,13 @@ func upload(w http.ResponseWriter, r *http.Request) {
                fd.Close()
                return
        }
+       if n == 0 {
+               log.Println(r.RemoteAddr, fn, p.FileName(), "empty")
+               os.Remove(fn + ".part")
+               fd.Close()
+               fmt.Fprintf(w, "Empty file")
+               return
+       }
        if err = fdBuf.Flush(); err != nil {
                log.Println(r.RemoteAddr, fn, p.FileName(), err)
                fd.Close()
@@ -86,17 +150,34 @@ func upload(w http.ResponseWriter, r *http.Request) {
                log.Println(r.RemoteAddr, fn, p.FileName(), n, sum, err)
                return
        }
-       fmt.Fprintf(w, "bytes: %d\nBLAKE2s: %s\n", n, sum)
+       fmt.Fprintf(w, "Timestamp: %s\nBytes: %d\nBLAKE2b: %s\n", fn, n, sum)
        log.Println(r.RemoteAddr, fn, p.FileName(), n, sum)
+       p, err = mr.NextPart()
+       if err != nil || p.FormName() != CommentFieldName {
+               go notify(fn, n, "")
+               return
+       }
+       comment, err := ioutil.ReadAll(p)
+       if err != nil || len(comment) == 0 {
+               go notify(fn, n, "")
+               return
+       }
+       ioutil.WriteFile(fn+".txt", comment, os.FileMode(0600))
+       go notify(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 {
-               panic(err)
+               log.Fatalln(err)
        }
        log.Println("listening", *bind)
        ln = netutil.LimitListener(ln, *conns)