diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2024-11-22 13:11:50 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-11-22 13:11:50 +0300 |
| commit | 00711cab5bb8f3cdd1b86d062ef067587e3fc18a (patch) | |
| tree | fbbb6b095ca91f8189425d05c7cdfd8986e61280 /backend/handlers/record.go | |
| parent | feat/frontend: remove map select from upload run dialog (#239) (diff) | |
| download | lphub-00711cab5bb8f3cdd1b86d062ef067587e3fc18a.tar.gz lphub-00711cab5bb8f3cdd1b86d062ef067587e3fc18a.tar.bz2 lphub-00711cab5bb8f3cdd1b86d062ef067587e3fc18a.zip | |
feat/backend: local demos path for testing (#243)
Co-authored-by: NeKz <nekzor@users.noreply.github.com>
Diffstat (limited to '')
| -rw-r--r-- | backend/handlers/record.go | 92 |
1 files changed, 72 insertions, 20 deletions
diff --git a/backend/handlers/record.go b/backend/handlers/record.go index 91e74b9..25a6c6d 100644 --- a/backend/handlers/record.go +++ b/backend/handlers/record.go | |||
| @@ -22,7 +22,6 @@ import ( | |||
| 22 | type RecordRequest struct { | 22 | type RecordRequest struct { |
| 23 | HostDemo *multipart.FileHeader `json:"host_demo" form:"host_demo" binding:"required" swaggerignore:"true"` | 23 | HostDemo *multipart.FileHeader `json:"host_demo" form:"host_demo" binding:"required" swaggerignore:"true"` |
| 24 | PartnerDemo *multipart.FileHeader `json:"partner_demo" form:"partner_demo" swaggerignore:"true"` | 24 | PartnerDemo *multipart.FileHeader `json:"partner_demo" form:"partner_demo" swaggerignore:"true"` |
| 25 | PartnerID string `json:"partner_id" form:"partner_id"` | ||
| 26 | } | 25 | } |
| 27 | 26 | ||
| 28 | type RecordResponse struct { | 27 | type RecordResponse struct { |
| @@ -197,6 +196,45 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 197 | return | 196 | return |
| 198 | } | 197 | } |
| 199 | } | 198 | } |
| 199 | if os.Getenv("ENV") == "DEV" { | ||
| 200 | if localPath := os.Getenv("LOCAL_DEMOS_PATH"); localPath != "" { | ||
| 201 | for i, header := range demoFileHeaders { | ||
| 202 | f, err := header.Open() | ||
| 203 | if err != nil { | ||
| 204 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | ||
| 205 | return | ||
| 206 | } | ||
| 207 | defer f.Close() | ||
| 208 | var objectName string | ||
| 209 | if i == 0 { | ||
| 210 | objectName = hostDemoUUID + ".dem" | ||
| 211 | } else if i == 1 { | ||
| 212 | objectName = partnerDemoUUID + ".dem" | ||
| 213 | } | ||
| 214 | demo, err := os.Create(localPath + objectName) | ||
| 215 | if err != nil { | ||
| 216 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | ||
| 217 | return | ||
| 218 | } | ||
| 219 | defer demo.Close() | ||
| 220 | _, err = io.Copy(demo, f) | ||
| 221 | if err != nil { | ||
| 222 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | ||
| 223 | return | ||
| 224 | } | ||
| 225 | } | ||
| 226 | if err = tx.Commit(); err != nil { | ||
| 227 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | ||
| 228 | return | ||
| 229 | } | ||
| 230 | c.JSON(http.StatusOK, models.Response{ | ||
| 231 | Success: true, | ||
| 232 | Message: "Successfully created record.", | ||
| 233 | Data: RecordResponse{ScoreCount: hostDemoScoreCount, ScoreTime: hostDemoScoreTime}, | ||
| 234 | }) | ||
| 235 | return | ||
| 236 | } | ||
| 237 | } | ||
| 200 | // Everything is good, upload the demo files. | 238 | // Everything is good, upload the demo files. |
| 201 | client, err := b2.NewClient(context.Background(), os.Getenv("B2_KEY_ID"), os.Getenv("B2_API_KEY")) | 239 | client, err := b2.NewClient(context.Background(), os.Getenv("B2_KEY_ID"), os.Getenv("B2_API_KEY")) |
| 202 | if err != nil { | 240 | if err != nil { |
| @@ -347,32 +385,46 @@ func DownloadDemoWithID(c *gin.Context) { | |||
| 347 | return | 385 | return |
| 348 | } | 386 | } |
| 349 | 387 | ||
| 350 | fileName := uuid + ".dem" | 388 | localPath := "" |
| 351 | url := os.Getenv("B2_DOWNLOAD_URL") + fileName | 389 | if os.Getenv("ENV") == "DEV" { |
| 352 | output, err := os.Create(fileName) | 390 | localPath = os.Getenv("LOCAL_DEMOS_PATH") |
| 353 | if err != nil { | ||
| 354 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | ||
| 355 | return | ||
| 356 | } | ||
| 357 | defer os.Remove(fileName) | ||
| 358 | defer output.Close() | ||
| 359 | response, err := http.Get(url) | ||
| 360 | if err != nil { | ||
| 361 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | ||
| 362 | return | ||
| 363 | } | 391 | } |
| 364 | defer response.Body.Close() | 392 | |
| 365 | _, err = io.Copy(output, response.Body) | 393 | fileName := uuid + ".dem" |
| 366 | if err != nil { | 394 | if localPath == "" { |
| 367 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 395 | url := os.Getenv("B2_DOWNLOAD_URL") + fileName |
| 368 | return | 396 | output, err := os.Create(fileName) |
| 397 | if err != nil { | ||
| 398 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | ||
| 399 | return | ||
| 400 | } | ||
| 401 | defer os.Remove(fileName) | ||
| 402 | defer output.Close() | ||
| 403 | response, err := http.Get(url) | ||
| 404 | if err != nil { | ||
| 405 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | ||
| 406 | return | ||
| 407 | } | ||
| 408 | defer response.Body.Close() | ||
| 409 | _, err = io.Copy(output, response.Body) | ||
| 410 | if err != nil { | ||
| 411 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | ||
| 412 | return | ||
| 413 | } | ||
| 369 | } | 414 | } |
| 415 | |||
| 370 | // Downloaded file | 416 | // Downloaded file |
| 371 | c.Header("Content-Description", "File Transfer") | 417 | c.Header("Content-Description", "File Transfer") |
| 372 | c.Header("Content-Transfer-Encoding", "binary") | 418 | c.Header("Content-Transfer-Encoding", "binary") |
| 373 | c.Header("Content-Disposition", "attachment; filename="+fileName) | 419 | c.Header("Content-Disposition", "attachment; filename="+fileName) |
| 374 | c.Header("Content-Type", "application/octet-stream") | 420 | c.Header("Content-Type", "application/octet-stream") |
| 375 | c.File(fileName) | 421 | |
| 422 | if localPath == "" { | ||
| 423 | c.File(fileName) | ||
| 424 | } else { | ||
| 425 | c.File(localPath + fileName) | ||
| 426 | } | ||
| 427 | |||
| 376 | // c.FileAttachment() | 428 | // c.FileAttachment() |
| 377 | } | 429 | } |
| 378 | 430 | ||