Skip to content

Commit

Permalink
Fix logo fetch flow from backend (#278)
Browse files Browse the repository at this point in the history
* feat: fix logo replace workflow

* fix: conflicts with previous logo route

* remove previous route

* Add docs for updateCompetitionInfoHandler

* Fix vendor

Co-authored-by: shubhamgupta2956 <shubham.gupta2956@gmail.com>
  • Loading branch information
ayanchoudhary and wryonik authored Mar 2, 2021
1 parent c18e6ac commit a6b0f80
Show file tree
Hide file tree
Showing 14 changed files with 322 additions and 37 deletions.
51 changes: 47 additions & 4 deletions api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"fmt"
"net/http"
"os"
"path/filepath"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -36,7 +37,26 @@ func reloadBeastConfig(c *gin.Context) {
})
}

// This updates competition info in the beast global configuration
// @Summary Updates competition info in the beast global configuration, located at ~/.beast/config.toml.
// @Description Populates beast gobal config map by reparsing the config file $HOME/.beast/config.toml.
// @Tags config
// @Accept json
// @Produce json
// @Param name query string true "Competition Name"
// @Param about query string true "Some information about competition"
// @Param prizes query string false "Competitions Prizes for the winners"
// @Param starting_time query string true "Competition's starting time"
// @Param ending_time query string true "Competition's ending time"
// @Param timezone query string true "Competition's timezone"
// @Param logo query string false "Competition's logo"
// @Success 200 {object} api.HTTPPlainResp
// @Failure 400 {object} api.HTTPPlainResp
// @Failure 500 {object} api.HTTPErrorResp
// @Router /api/config/competition-info [post]
func updateCompetitionInfoHandler(c *gin.Context) {
var logoFilePath string

name := c.PostForm("name")
about := c.PostForm("about")
prizes := c.PostForm("prizes")
Expand All @@ -45,17 +65,40 @@ func updateCompetitionInfoHandler(c *gin.Context) {
timezone := c.PostForm("timezone")
logo, err := c.FormFile("logo")

logoFilePath := ""

// The file cannot be received.
if err != nil {
log.Info("No file recieved from the user")
} else {
logoFilePath = filepath.Join(core.BEAST_GLOBAL_DIR, core.BEAST_ASSETS_DIR, logo.Filename)
logoFilePath = filepath.Join(
core.BEAST_GLOBAL_DIR,
core.BEAST_ASSETS_DIR,
core.BEAST_LOGO_DIR,
logo.Filename,
)

competitionInfo, err := config.GetCompetitionInfo()
if err != nil {
log.Info("Unable to load previous config")
c.JSON(http.StatusInternalServerError, HTTPErrorResp{
Error: fmt.Sprintf("Unable to load previous config: %s", err),
})
return
}

// Delete previously uploaded logo file
if competitionInfo.LogoURL != "" {
if err := os.Remove(competitionInfo.LogoURL); err != nil {
log.Info("Unable to delete previous logo file")
c.JSON(http.StatusInternalServerError, HTTPErrorResp{
Error: fmt.Sprintf("Unable to delete previous logo file: %s", err),
})
return
}
}

// The file is received, save it
if err := c.SaveUploadedFile(logo, logoFilePath); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, HTTPErrorResp{
c.JSON(http.StatusInternalServerError, HTTPErrorResp{
Error: fmt.Sprintf("Unable to save file: %s", err),
})
return
Expand Down
5 changes: 4 additions & 1 deletion api/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package api
import (
"fmt"
"net/http"
"path/filepath"
"strconv"
"strings"

"github.com/gin-gonic/gin"
"github.com/sdslabs/beastv4/core"
Expand Down Expand Up @@ -515,14 +517,15 @@ func competitionInfoHandler(c *gin.Context) {
return
}

logoPath := strings.ReplaceAll(competitionInfo.LogoURL, filepath.Join(core.BEAST_GLOBAL_DIR, core.BEAST_ASSETS_DIR, core.BEAST_LOGO_DIR), "")
c.JSON(http.StatusOK, CompetitionInfoResp{
Name: competitionInfo.Name,
About: competitionInfo.About,
Prizes: competitionInfo.Prizes,
StartingTime: competitionInfo.StartingTime,
EndingTime: competitionInfo.EndingTime,
TimeZone: competitionInfo.TimeZone,
LogoURL: "/api/info/logo",
LogoURL: strings.Trim(logoPath, "/"),
})
return
}
8 changes: 7 additions & 1 deletion api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package api

import (
"net/http"
"path/filepath"
"time"

"github.com/gin-contrib/cors"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"github.com/sdslabs/beastv4/core"
)

func dummyHandler(c *gin.Context) {
Expand Down Expand Up @@ -35,7 +38,10 @@ func initGinRouter() *gin.Engine {
}

// For serving static files
router.StaticFile("/api/info/logo", getLogoPath())
router.Use(static.Serve("/api/info/logo", static.LocalFile(
filepath.Join(core.BEAST_GLOBAL_DIR, core.BEAST_ASSETS_DIR, core.BEAST_LOGO_DIR),
false)),
)
router.GET("/api/info/competition-info", competitionInfoHandler)

// API routes group
Expand Down
1 change: 1 addition & 0 deletions core/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const ( //paths
BEAST_STAGING_AREA_MOUNT_POINT string = "/beast"
BEAST_UPLOADS_DIR string = "uploads"
BEAST_ASSETS_DIR string = "assets"
BEAST_LOGO_DIR string = "logo"
)

const ( //chall types
Expand Down
7 changes: 1 addition & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ require (
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 // indirect
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 // indirect
github.com/containerd/containerd v1.3.4 // indirect
github.com/cpuguy83/go-md2man v1.0.10 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand All @@ -22,11 +21,11 @@ require (
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 // indirect
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 // indirect
github.com/gin-contrib/cors v1.3.1
github.com/gin-contrib/static v0.0.0-20200916080430-d45d9a37d28e
github.com/gin-gonic/gin v1.5.0
github.com/gliderlabs/ssh v0.1.1 // indirect
github.com/go-openapi/spec v0.20.3 // indirect
github.com/go-sql-driver/mysql v1.4.0
github.com/gogo/protobuf v1.3.1 // indirect
github.com/golang/protobuf v1.3.2
github.com/google/go-cmp v0.2.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
Expand All @@ -39,8 +38,6 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/onsi/gomega v1.4.2 // indirect
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/pelletier/go-buffruneio v0.2.0 // indirect
github.com/pkg/errors v0.8.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand All @@ -62,8 +59,6 @@ require (
google.golang.org/grpc v1.19.0
gopkg.in/airbrake/gobrake.v2 v2.0.9 // indirect
gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2 // indirect
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
gopkg.in/go-playground/validator.v8 v8.18.2 // indirect
gopkg.in/src-d/go-billy.v4 v4.3.0 // indirect
gopkg.in/src-d/go-git-fixtures.v3 v3.3.0 // indirect
gopkg.in/src-d/go-git.v4 v4.7.0
Expand Down
Loading

0 comments on commit a6b0f80

Please sign in to comment.