diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-01-14 11:51:51 +0300 |
|---|---|---|
| committer | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-01-14 11:51:51 +0300 |
| commit | 8598cfe1e8dd479484760f46ab33a111f97294c4 (patch) | |
| tree | 5eba4f82f5977ba12d3e61b5e5e7b2bae50c5295 /backend | |
| parent | record submission looks like its working! (#23, #20) (diff) | |
| download | lphub-8598cfe1e8dd479484760f46ab33a111f97294c4.tar.gz lphub-8598cfe1e8dd479484760f46ab33a111f97294c4.tar.bz2 lphub-8598cfe1e8dd479484760f46ab33a111f97294c4.zip | |
upload record fix, download demo success (#24)
Diffstat (limited to '')
| -rw-r--r-- | backend/controllers/recordController.go | 50 | ||||
| -rw-r--r-- | backend/routes/routes.go | 3 |
2 files changed, 48 insertions, 5 deletions
diff --git a/backend/controllers/recordController.go b/backend/controllers/recordController.go index 81eb465..50db168 100644 --- a/backend/controllers/recordController.go +++ b/backend/controllers/recordController.go | |||
| @@ -54,10 +54,14 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 54 | record.ScoreTime = score_time | 54 | record.ScoreTime = score_time |
| 55 | record.PartnerID = c.PostForm("partner_id") | 55 | record.PartnerID = c.PostForm("partner_id") |
| 56 | record.IsPartnerOrange = is_partner_orange | 56 | record.IsPartnerOrange = is_partner_orange |
| 57 | if record.PartnerID == "" { | ||
| 58 | c.JSON(http.StatusBadRequest, models.ErrorResponse("No partner id given.")) | ||
| 59 | return | ||
| 60 | } | ||
| 57 | // Multipart form | 61 | // Multipart form |
| 58 | form, err := c.MultipartForm() | 62 | form, err := c.MultipartForm() |
| 59 | if err != nil { | 63 | if err != nil { |
| 60 | c.String(http.StatusBadRequest, "get form err: %s", err.Error()) | 64 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 61 | return | 65 | return |
| 62 | } | 66 | } |
| 63 | files := form.File["demos"] | 67 | files := form.File["demos"] |
| @@ -91,7 +95,7 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 91 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 95 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 92 | return | 96 | return |
| 93 | } | 97 | } |
| 94 | file, err := createFile(srv, uuid, "application/octet-stream", f, os.Getenv("GOOGLE_FOLDER_ID")) | 98 | file, err := createFile(srv, uuid+".dem", "application/octet-stream", f, os.Getenv("GOOGLE_FOLDER_ID")) |
| 95 | if err != nil { | 99 | if err != nil { |
| 96 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 100 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 97 | return | 101 | return |
| @@ -124,7 +128,6 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 124 | } | 128 | } |
| 125 | _, err := database.DB.Exec(sql, mapId, record.ScoreCount, record.ScoreTime, hostID, partnerID, hostDemoUUID, partnerDemoUUID) | 129 | _, err := database.DB.Exec(sql, mapId, record.ScoreCount, record.ScoreTime, hostID, partnerID, hostDemoUUID, partnerDemoUUID) |
| 126 | if err != nil { | 130 | if err != nil { |
| 127 | _, err = database.DB.Exec(`DELETE FROM demos WHERE id = $1 OR id = $2;`, hostDemoUUID, partnerDemoUUID) | ||
| 128 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 131 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 129 | return | 132 | return |
| 130 | } | 133 | } |
| @@ -133,7 +136,6 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 133 | VALUES($1, $2, $3, $4, $5);` | 136 | VALUES($1, $2, $3, $4, $5);` |
| 134 | _, err := database.DB.Exec(sql, mapId, record.ScoreCount, record.ScoreTime, user.(models.User).SteamID, hostDemoUUID) | 137 | _, err := database.DB.Exec(sql, mapId, record.ScoreCount, record.ScoreTime, user.(models.User).SteamID, hostDemoUUID) |
| 135 | if err != nil { | 138 | if err != nil { |
| 136 | _, err = database.DB.Exec(`DELETE FROM demos WHERE id = $1;`, hostDemoUUID) | ||
| 137 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 139 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 138 | return | 140 | return |
| 139 | } | 141 | } |
| @@ -146,6 +148,46 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 146 | return | 148 | return |
| 147 | } | 149 | } |
| 148 | 150 | ||
| 151 | func DownloadDemoWithID(c *gin.Context) { | ||
| 152 | uuid := c.Query("uuid") | ||
| 153 | var locationID string | ||
| 154 | if uuid == "" { | ||
| 155 | c.JSON(http.StatusBadRequest, models.ErrorResponse("Invalid id given.")) | ||
| 156 | return | ||
| 157 | } | ||
| 158 | err := database.DB.QueryRow(`SELECT location_id FROM demos WHERE id = $1;`, uuid).Scan(&locationID) | ||
| 159 | if err != nil { | ||
| 160 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 161 | return | ||
| 162 | } | ||
| 163 | if locationID == "" { | ||
| 164 | c.JSON(http.StatusBadRequest, models.ErrorResponse("Invalid id given.")) | ||
| 165 | return | ||
| 166 | } | ||
| 167 | url := "https://drive.google.com/uc?export=download&id=" + locationID | ||
| 168 | fileName := uuid + ".dem" | ||
| 169 | output, err := os.Create(fileName) | ||
| 170 | defer output.Close() | ||
| 171 | response, err := http.Get(url) | ||
| 172 | if err != nil { | ||
| 173 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 174 | return | ||
| 175 | } | ||
| 176 | defer response.Body.Close() | ||
| 177 | _, err = io.Copy(output, response.Body) | ||
| 178 | if err != nil { | ||
| 179 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 180 | return | ||
| 181 | } | ||
| 182 | // Downloaded file | ||
| 183 | c.Header("Content-Description", "File Transfer") | ||
| 184 | c.Header("Content-Transfer-Encoding", "binary") | ||
| 185 | c.Header("Content-Disposition", "attachment; filename="+fileName) | ||
| 186 | c.Header("Content-Type", "application/octet-stream") | ||
| 187 | c.File(fileName) | ||
| 188 | // c.FileAttachment() | ||
| 189 | } | ||
| 190 | |||
| 149 | // Use Service account | 191 | // Use Service account |
| 150 | func serviceAccount() *http.Client { | 192 | func serviceAccount() *http.Client { |
| 151 | privateKey, _ := b64.StdEncoding.DecodeString(os.Getenv("GOOGLE_PRIVATE_KEY_BASE64")) | 193 | privateKey, _ := b64.StdEncoding.DecodeString(os.Getenv("GOOGLE_PRIVATE_KEY_BASE64")) |
diff --git a/backend/routes/routes.go b/backend/routes/routes.go index f9256ca..e9de891 100644 --- a/backend/routes/routes.go +++ b/backend/routes/routes.go | |||
| @@ -17,6 +17,7 @@ func InitRoutes(router *gin.Engine) { | |||
| 17 | v1.GET("/login", controllers.Login) | 17 | v1.GET("/login", controllers.Login) |
| 18 | v1.GET("/profile", middleware.CheckAuth, controllers.Profile) | 18 | v1.GET("/profile", middleware.CheckAuth, controllers.Profile) |
| 19 | v1.GET("/user/:id", middleware.CheckAuth, controllers.FetchUser) | 19 | v1.GET("/user/:id", middleware.CheckAuth, controllers.FetchUser) |
| 20 | v1.POST("/record/:id", middleware.CheckAuth, controllers.CreateRecordWithDemo) | 20 | v1.GET("/demo", controllers.DownloadDemoWithID) |
| 21 | v1.POST("/maps/:id/record", middleware.CheckAuth, controllers.CreateRecordWithDemo) | ||
| 21 | } | 22 | } |
| 22 | } | 23 | } |