From 7dd31bad1c7dde02644acfb454de5a149c389b5b Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Mon, 29 Apr 2019 11:34:13 +0200 Subject: [PATCH] Add base64 encoding of image Signed-off-by: Knut Ahlers --- mjpeg.go | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/mjpeg.go b/mjpeg.go index 8f9da58..b4cf85c 100644 --- a/mjpeg.go +++ b/mjpeg.go @@ -1,11 +1,13 @@ package main import ( + "encoding/base64" "fmt" "mime/multipart" "net/http" "net/textproto" + "github.com/pkg/errors" log "github.com/sirupsen/logrus" ) @@ -34,18 +36,26 @@ func handleMJPEG(res http.ResponseWriter, r *http.Request, imgs chan []byte, uid return case img := <-imgs: - partHeader := make(textproto.MIMEHeader) - partHeader.Add("Content-Type", "image/jpeg") + err := func() error { + partHeader := make(textproto.MIMEHeader) + partHeader.Add("Content-Type", "image/jpeg") + partHeader.Add("Content-Transfer-Encoding", "base64") - partWriter, err := mimeWriter.CreatePart(partHeader) - if err != nil { - logger.WithError(err).Error("Unable to create mime part") - continue - } + partWriter, err := mimeWriter.CreatePart(partHeader) + if err != nil { + return errors.Wrap(err, "Unable to create mime part") + } + + b64w := base64.NewEncoder(base64.StdEncoding, partWriter) + defer b64w.Close() + + _, err = b64w.Write(img) + + return errors.Wrap(err, "Unable to write image") + }() - _, err = partWriter.Write(img) if err != nil { - logger.WithError(err).Error("Unable to write image") + logger.WithError(err).Error("Unable to process image") errC++ if errC > 5 {