Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

moving things around to allow saving images on signup #48

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/auth/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ func (a *Auth) signUpHelper(c *gin.Context, sup *signupUser) (_ bool) {
misc.AbortWithErr(c, http.StatusBadRequest, err)
return
}

return true
}

Expand Down
38 changes: 32 additions & 6 deletions server/image.go → internal/auth/images.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server
package auth

import (
"bytes"
Expand All @@ -11,6 +11,8 @@ import (
"path/filepath"
"strings"

"github.com/swayops/sway/config"

"image"
_ "image/gif"
_ "image/jpeg"
Expand All @@ -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
Expand Down Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions internal/auth/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
65 changes: 7 additions & 58 deletions server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -2578,59 +2580,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"))
Expand Down
31 changes: 3 additions & 28 deletions server/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
1 change: 0 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down