From 2a4cec9465108c251be36b14e24a7db25bd4490a Mon Sep 17 00:00:00 2001 From: OneOfOne Date: Mon, 7 Nov 2016 22:30:16 +0200 Subject: [PATCH 1/2] move image helpers to auth and remove the uploadImages handler --- internal/auth/handlers.go | 6 +++ server/image.go => internal/auth/images.go | 38 ++++++++++--- server/handlers.go | 63 ++-------------------- server/helpers.go | 31 ++--------- server/server.go | 1 - 5 files changed, 46 insertions(+), 93 deletions(-) rename server/image.go => internal/auth/images.go (54%) diff --git a/internal/auth/handlers.go b/internal/auth/handlers.go index ae1b1d6c..1735204a 100644 --- a/internal/auth/handlers.go +++ b/internal/auth/handlers.go @@ -243,6 +243,11 @@ func (a *Auth) signUpHelper(c *gin.Context, sup *signupUser) (_ bool) { return } + if err := SaveUserImage(a.cfg, &sup.User); err != nil { + misc.AbortWithErr(c, http.StatusBadRequest, err) + return + } + if err := a.db.Update(func(tx *bolt.Tx) error { if err := a.CreateUserTx(tx, &sup.User, sup.Password); err != nil { return err @@ -252,6 +257,7 @@ func (a *Auth) signUpHelper(c *gin.Context, sup *signupUser) (_ bool) { misc.AbortWithErr(c, http.StatusBadRequest, err) return } + return true } diff --git a/server/image.go b/internal/auth/images.go similarity index 54% rename from server/image.go rename to internal/auth/images.go index d1778c56..38ef4fb1 100644 --- a/server/image.go +++ b/internal/auth/images.go @@ -1,4 +1,4 @@ -package server +package auth import ( "bytes" @@ -11,6 +11,8 @@ import ( "path/filepath" "strings" + "github.com/swayops/sway/config" + "image" _ "image/gif" _ "image/jpeg" @@ -27,7 +29,7 @@ var ( ErrInvalidImage = errors.New("Invalid image!") ) -func saveImageToDisk(fileNameBase, data, id, suffix string, minWidth, minHeight int) (string, error) { +func SaveImageToDisk(fileNameBase, data, id, suffix string, minWidth, minHeight int) (string, error) { idx := strings.Index(data, ";base64,") if idx < 0 { return "", ErrInvalidImage @@ -57,20 +59,44 @@ func saveImageToDisk(fileNameBase, data, id, suffix string, minWidth, minHeight return id + suffix + "." + fm, err } -func getImageUrl(s *Server, bucket, typ, filename string, addDomain bool) string { +func GetImageUrl(cfg *config.Config, bucket, typ, filename string, addDomain bool) string { var base string switch typ { case "dash": if addDomain { - base = s.Cfg.DashURL + base = cfg.DashURL } case "inf": if addDomain { - base = s.Cfg.InfAppURL + base = cfg.InfAppURL } default: log.Panicf("invalid type: %v", typ) } - return base + "/" + filepath.Join(s.Cfg.ImageUrlPath, bucket, filename) + return base + "/" + filepath.Join(cfg.ImageUrlPath, bucket, filename) +} + +// saveUserImage saves the user image to disk and sets User.ImageURL to the url for it if the image is a data:image/ +func SaveUserImage(cfg *config.Config, u *User) error { + if strings.HasPrefix(u.ImageURL, "data:image/") { + filename, err := SaveImageToDisk(filepath.Join(cfg.ImagesDir, cfg.Bucket.User, u.ID), u.ImageURL, u.ID, "", 300, 300) + if err != nil { + return err + } + + u.ImageURL = GetImageUrl(cfg, cfg.Bucket.User, "dash", filename, false) + } + + if strings.HasPrefix(u.CoverImageURL, "data:image/") { + filename, err := SaveImageToDisk(filepath.Join(cfg.ImagesDir, cfg.Bucket.User, u.ID), + u.CoverImageURL, u.ID, "-cover", 300, 300) + if err != nil { + return err + } + + u.CoverImageURL = GetImageUrl(cfg, cfg.Bucket.User, "dash", filename, false) + } + + return nil } diff --git a/server/handlers.go b/server/handlers.go index f188988f..044c0d3f 100644 --- a/server/handlers.go +++ b/server/handlers.go @@ -317,15 +317,15 @@ func postCampaign(s *Server) gin.HandlerFunc { c.JSON(400, misc.StatusErr("Please provide a valid campaign image")) return } - filename, err := saveImageToDisk(filepath.Join(s.Cfg.ImagesDir, s.Cfg.Bucket.Campaign, cmp.Id), cmp.ImageData, cmp.Id, "", 750, 389) + filename, err := auth.SaveImageToDisk(filepath.Join(s.Cfg.ImagesDir, s.Cfg.Bucket.Campaign, cmp.Id), cmp.ImageData, cmp.Id, "", 750, 389) if err != nil { c.JSON(400, misc.StatusErr(err.Error())) return } - cmp.ImageURL, cmp.ImageData = getImageUrl(s, s.Cfg.Bucket.Campaign, "dash", filename, false), "" + cmp.ImageURL, cmp.ImageData = auth.GetImageUrl(s.Cfg, s.Cfg.Bucket.Campaign, "dash", filename, false), "" } else { - cmp.ImageURL = getImageUrl(s, s.Cfg.Bucket.Campaign, "dash", DEFAULT_IMAGES[rand.Intn(len(DEFAULT_IMAGES))], false) + cmp.ImageURL = auth.GetImageUrl(s.Cfg, s.Cfg.Bucket.Campaign, "dash", DEFAULT_IMAGES[rand.Intn(len(DEFAULT_IMAGES))], false) } // Save the Campaign @@ -493,13 +493,13 @@ func putCampaign(s *Server) gin.HandlerFunc { return } - filename, err := saveImageToDisk(filepath.Join(s.Cfg.ImagesDir, s.Cfg.Bucket.Campaign, cmp.Id), upd.ImageData, cmp.Id, "", 750, 389) + filename, err := auth.SaveImageToDisk(filepath.Join(s.Cfg.ImagesDir, s.Cfg.Bucket.Campaign, cmp.Id), upd.ImageData, cmp.Id, "", 750, 389) if err != nil { c.JSON(400, misc.StatusErr(err.Error())) return } - cmp.ImageURL, upd.ImageData = getImageUrl(s, s.Cfg.Bucket.Campaign, "dash", filename, false), "" + cmp.ImageURL, upd.ImageData = auth.GetImageUrl(s.Cfg, s.Cfg.Bucket.Campaign, "dash", filename, false), "" } for _, g := range upd.Geos { @@ -2578,59 +2578,6 @@ func forceEmail(s *Server) gin.HandlerFunc { } } -func uploadImage(s *Server) gin.HandlerFunc { - return func(c *gin.Context) { - var upd UploadImage - if err := json.NewDecoder(c.Request.Body).Decode(&upd); err != nil { - c.JSON(400, misc.StatusErr("Error unmarshalling request body")) - return - } - - id := c.Param("id") - if id == "" { - c.JSON(400, misc.StatusErr("Invalid ID")) - return - } - - bucket := c.Param("bucket") - filename, err := saveImageToDisk(s.Cfg.ImagesDir+bucket+"/"+id, upd.Data, id, "", 750, 389) - if err != nil { - c.JSON(400, misc.StatusErr(err.Error())) - return - } - - var imageURL string - if bucket == "campaign" { - var ( - cmp common.Campaign - b []byte - ) - // Save image URL in campaign - s.db.View(func(tx *bolt.Tx) error { - b = tx.Bucket([]byte(s.Cfg.Bucket.Campaign)).Get([]byte(id)) - return nil - }) - - if err = json.Unmarshal(b, &cmp); err != nil { - c.JSON(400, misc.StatusErr("Error unmarshalling campaign")) - return - } - - imageURL = getImageUrl(s, s.Cfg.Bucket.Campaign, "dash", filename, false) - cmp.ImageURL = imageURL - - // Save the Campaign - if err = s.db.Update(func(tx *bolt.Tx) (err error) { - return saveCampaign(tx, &cmp, s) - }); err != nil { - c.JSON(500, misc.StatusErr(err.Error())) - return - } - } - c.JSON(200, UploadImage{ImageURL: imageURL}) - } -} - func getLatestGeo(s *Server) gin.HandlerFunc { return func(c *gin.Context) { inf, ok := s.auth.Influencers.Get(c.Param("influencerId")) diff --git a/server/helpers.go b/server/helpers.go index 44d7258e..2aa99334 100644 --- a/server/helpers.go +++ b/server/helpers.go @@ -137,13 +137,13 @@ func getUserImage(s *Server, data, suffix string, minW, minH int, user *auth.Use return data, nil } - filename, err := saveImageToDisk(filepath.Join(s.Cfg.ImagesDir, s.Cfg.Bucket.User, user.ID), + filename, err := auth.SaveImageToDisk(filepath.Join(s.Cfg.ImagesDir, s.Cfg.Bucket.User, user.ID), data, user.ID, suffix, minW, minH) if err != nil { return "", err } - return getImageUrl(s, s.Cfg.Bucket.User, "dash", filename, false), nil + return auth.GetImageUrl(s.Cfg, s.Cfg.Bucket.User, "dash", filename, false), nil } func savePassword(s *Server, tx *bolt.Tx, oldPass, pass, pass2 string, user *auth.User) (bool, error) { @@ -619,31 +619,6 @@ func emailList(s *Server, cid string, override []string) { ) } -// saveUserImage saves the user image to disk and sets User.ImageURL to the url for it if the image is a data:image/ -func saveUserImage(s *Server, u *auth.User) error { - if strings.HasPrefix(u.ImageURL, "data:image/") { - - filename, err := saveImageToDisk(filepath.Join(s.Cfg.ImagesDir, s.Cfg.Bucket.User, u.ID), u.ImageURL, u.ID, "", 300, 300) - if err != nil { - return err - } - - u.ImageURL = getImageUrl(s, s.Cfg.Bucket.User, "dash", filename, false) - } - - if strings.HasPrefix(u.CoverImageURL, "data:image/") { - filename, err := saveImageToDisk(filepath.Join(s.Cfg.ImagesDir, s.Cfg.Bucket.User, u.ID), - u.CoverImageURL, u.ID, "-cover", 300, 300) - if err != nil { - return err - } - - u.CoverImageURL = getImageUrl(s, s.Cfg.Bucket.User, "dash", filename, false) - } - - return nil -} - func saveUserHelper(s *Server, c *gin.Context, userType string) { var ( incUser struct { @@ -696,7 +671,7 @@ func saveUserHelper(s *Server, c *gin.Context, userType string) { incUser.ID = id // for saveImage } - if err := saveUserImage(s, &incUser.User); err != nil { + if err := auth.SaveUserImage(s.Cfg, &incUser.User); err != nil { misc.AbortWithErr(c, 400, err) return } diff --git a/server/server.go b/server/server.go index e709b864..9442a5ba 100644 --- a/server/server.go +++ b/server/server.go @@ -327,7 +327,6 @@ func (srv *Server) initializeRoutes(r gin.IRouter) { createRoutes(verifyGroup, srv, "/getCampaignsByAdvertiser", "id", scopes["adv"], auth.AdAgencyItem, getCampaignsByAdvertiser, nil, nil, nil) - verifyGroup.POST("/uploadImage/:id/:bucket", uploadImage(srv)) verifyGroup.GET("/getDealsForCampaign/:id", getDealsForCampaign(srv)) r.Static("images", srv.Cfg.ImagesDir) From b10599dcc29f1f551cdff1252089724865edc3fb Mon Sep 17 00:00:00 2001 From: OneOfOne Date: Mon, 7 Nov 2016 22:45:26 +0200 Subject: [PATCH 2/2] save the image after we get a user id --- internal/auth/handlers.go | 5 ----- internal/auth/user.go | 4 ++++ server/handlers.go | 2 ++ 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/internal/auth/handlers.go b/internal/auth/handlers.go index 1735204a..a3746d67 100644 --- a/internal/auth/handlers.go +++ b/internal/auth/handlers.go @@ -243,11 +243,6 @@ func (a *Auth) signUpHelper(c *gin.Context, sup *signupUser) (_ bool) { return } - if err := SaveUserImage(a.cfg, &sup.User); err != nil { - misc.AbortWithErr(c, http.StatusBadRequest, err) - return - } - if err := a.db.Update(func(tx *bolt.Tx) error { if err := a.CreateUserTx(tx, &sup.User, sup.Password); err != nil { return err diff --git a/internal/auth/user.go b/internal/auth/user.go index 3c31deca..9290fdec 100644 --- a/internal/auth/user.go +++ b/internal/auth/user.go @@ -259,6 +259,10 @@ func (a *Auth) CreateUserTx(tx *bolt.Tx, u *User, password string) (err error) { } SKIP: + if err = SaveUserImage(a.cfg, u); err != nil { + return + } + if err = misc.PutTxJson(tx, a.cfg.Bucket.User, u.ID, u); err != nil { return } diff --git a/server/handlers.go b/server/handlers.go index 044c0d3f..d7440c8c 100644 --- a/server/handlers.go +++ b/server/handlers.go @@ -2515,6 +2515,7 @@ func userProfile(srv *Server) gin.HandlerFunc { checkTalentAgency(c) default: misc.AbortWithErr(c, http.StatusUnauthorized, auth.ErrUnauthorized) + return } if c.IsAborted() { return @@ -2525,6 +2526,7 @@ func userProfile(srv *Server) gin.HandlerFunc { } SKIP: + cu = cu.Trim() if cu.Advertiser == nil { // return the user if it isn't an advertiser