]> Sergey Matveev's repositories - uploader.git/blobdiff - src/uploader/main.go
Forbid any later GNU GPL versions autousage
[uploader.git] / src / uploader / main.go
index c3efd614da06491b530624e2a3dc29456ca7a14d..3363fce2415d1c475e2857cd0f84d0446c68cb82 100644 (file)
@@ -1,21 +1,38 @@
 /*
 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"
@@ -26,6 +43,8 @@ const (
        WriteBufSize     = 1 << 20
        FileFieldName    = "fileupload"
        CommentFieldName = "comment"
+
+       SendmailCmd = "/usr/sbin/sendmail"
 )
 
 var (
@@ -37,8 +56,41 @@ var (
 <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 {
@@ -61,56 +113,71 @@ func upload(w http.ResponseWriter, r *http.Request) {
                log.Println(r.RemoteAddr, "non file form field")
                return
        }
-       h, err := blake2b.New256(nil)
+       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, p.FileName(), err)
+               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, p.FileName(), err)
+               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, p.FileName(), err)
+               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, p.FileName(), n, sum, err)
+               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, p.FileName(), 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(fn, n, string(comment))
+       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 {
-               panic(err)
+               log.Fatalln(err)
        }
        log.Println("listening", *bind)
        ln = netutil.LimitListener(ln, *conns)