diff options
| -rw-r--r-- | backend/.env.example | 27 | ||||
| -rw-r--r-- | backend/handlers/record.go | 92 |
2 files changed, 86 insertions, 33 deletions
diff --git a/backend/.env.example b/backend/.env.example index 90ca8b4..b0a7101 100644 --- a/backend/.env.example +++ b/backend/.env.example | |||
| @@ -1,13 +1,14 @@ | |||
| 1 | PORT= | 1 | PORT=4000 |
| 2 | SECRET_KEY= | 2 | SECRET_KEY=123456789ABCDEF |
| 3 | API_KEY= | 3 | API_KEY=123456789ABCDEF |
| 4 | ENV= | 4 | ENV=DEV |
| 5 | DB_HOST= | 5 | DB_HOST=localhost |
| 6 | DB_PORT= | 6 | DB_PORT=5432 |
| 7 | DB_USER= | 7 | DB_USER=postgres |
| 8 | DB_PASS= | 8 | DB_PASS=postgres |
| 9 | DB_NAME= | 9 | DB_NAME=postgres |
| 10 | B2_BUCKET_NAME= | 10 | B2_BUCKET_NAME=lphub |
| 11 | B2_KEY_ID= | 11 | B2_KEY_ID=123456789ABCDEF |
| 12 | B2_API_KEY= | 12 | B2_API_KEY=123456789ABCDEF |
| 13 | B2_DOWNLOAD_URL= | 13 | B2_DOWNLOAD_URL=https://lphub.s3.eu-central-001.backblazeb2.com/ |
| 14 | LOCAL_DEMOS_PATH=/path/to/demos/ | ||
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 | ||