]> Sergey Matveev's repositories - uploader.git/commitdiff
Ability to send notification about uploaded files
authorSergey Matveev <stargrave@stargrave.org>
Thu, 30 May 2019 17:59:09 +0000 (20:59 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Thu, 30 May 2019 18:04:51 +0000 (21:04 +0300)
README
src/uploader/main.go

diff --git a/README b/README
index 5d674a948daaca55439908757fb05ecb3b281f88..9f5cf193ca53f493d53b0d5bfdeec9a9c8687a96 100644 (file)
--- a/README
+++ b/README
@@ -11,3 +11,5 @@ You can upload files with curl:
 You can verify integrity locally:
 
     b2sum -a blake2b somedata.tar.gpg
+
+You can enable mail notification with -notify-to and -notify-from options.
index a1465cd663bfbe5b75807e2d06c1e697d5cdce48..788c2cafc7a43df9a907266ab2499fb36a951863 100644 (file)
@@ -7,15 +7,20 @@ 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 +31,8 @@ const (
        WriteBufSize     = 1 << 20
        FileFieldName    = "fileupload"
        CommentFieldName = "comment"
+
+       SendmailCmd = "/usr/sbin/sendmail"
 )
 
 var (
@@ -37,8 +44,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(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 {
@@ -101,10 +141,12 @@ func upload(w http.ResponseWriter, r *http.Request) {
        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))
@@ -114,10 +156,15 @@ func upload(w http.ResponseWriter, r *http.Request) {
 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)