diff options
| -rw-r--r-- | backend/controllers/recordController.go | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/backend/controllers/recordController.go b/backend/controllers/recordController.go index ca863bb..1cfaa56 100644 --- a/backend/controllers/recordController.go +++ b/backend/controllers/recordController.go | |||
| @@ -82,6 +82,13 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 82 | } | 82 | } |
| 83 | var hostDemoUUID string | 83 | var hostDemoUUID string |
| 84 | var partnerDemoUUID string | 84 | var partnerDemoUUID string |
| 85 | client := serviceAccount() | ||
| 86 | srv, err := drive.New(client) | ||
| 87 | if err != nil { | ||
| 88 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 89 | return | ||
| 90 | } | ||
| 91 | fileID := "" | ||
| 85 | for i, header := range files { | 92 | for i, header := range files { |
| 86 | uuid := uuid.New().String() | 93 | uuid := uuid.New().String() |
| 87 | // Upload & insert into demos | 94 | // Upload & insert into demos |
| @@ -96,17 +103,12 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 96 | return | 103 | return |
| 97 | } | 104 | } |
| 98 | defer f.Close() | 105 | defer f.Close() |
| 99 | client := serviceAccount() | ||
| 100 | srv, err := drive.New(client) | ||
| 101 | if err != nil { | ||
| 102 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 103 | return | ||
| 104 | } | ||
| 105 | file, err := createFile(srv, uuid+".dem", "application/octet-stream", f, os.Getenv("GOOGLE_FOLDER_ID")) | 106 | file, err := createFile(srv, uuid+".dem", "application/octet-stream", f, os.Getenv("GOOGLE_FOLDER_ID")) |
| 106 | if err != nil { | 107 | if err != nil { |
| 107 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 108 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 108 | return | 109 | return |
| 109 | } | 110 | } |
| 111 | fileID = file.Id | ||
| 110 | if i == 0 { | 112 | if i == 0 { |
| 111 | hostDemoUUID = uuid | 113 | hostDemoUUID = uuid |
| 112 | } | 114 | } |
| @@ -115,6 +117,7 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 115 | } | 117 | } |
| 116 | _, err = database.DB.Exec(`INSERT INTO demos (id,location_id) VALUES ($1,$2)`, uuid, file.Id) | 118 | _, err = database.DB.Exec(`INSERT INTO demos (id,location_id) VALUES ($1,$2)`, uuid, file.Id) |
| 117 | if err != nil { | 119 | if err != nil { |
| 120 | deleteFile(srv, file.Id) | ||
| 118 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 121 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 119 | return | 122 | return |
| 120 | } | 123 | } |
| @@ -135,6 +138,7 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 135 | } | 138 | } |
| 136 | _, err := database.DB.Exec(sql, mapId, record.ScoreCount, record.ScoreTime, hostID, partnerID, hostDemoUUID, partnerDemoUUID) | 139 | _, err := database.DB.Exec(sql, mapId, record.ScoreCount, record.ScoreTime, hostID, partnerID, hostDemoUUID, partnerDemoUUID) |
| 137 | if err != nil { | 140 | if err != nil { |
| 141 | deleteFile(srv, fileID) | ||
| 138 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 142 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 139 | return | 143 | return |
| 140 | } | 144 | } |
| @@ -151,6 +155,7 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 151 | VALUES($1, $2, $3, $4, $5);` | 155 | VALUES($1, $2, $3, $4, $5);` |
| 152 | _, err := database.DB.Exec(sql, mapId, record.ScoreCount, record.ScoreTime, user.(models.User).SteamID, hostDemoUUID) | 156 | _, err := database.DB.Exec(sql, mapId, record.ScoreCount, record.ScoreTime, user.(models.User).SteamID, hostDemoUUID) |
| 153 | if err != nil { | 157 | if err != nil { |
| 158 | deleteFile(srv, fileID) | ||
| 154 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 159 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 155 | return | 160 | return |
| 156 | } | 161 | } |
| @@ -242,3 +247,7 @@ func createFile(service *drive.Service, name string, mimeType string, content io | |||
| 242 | 247 | ||
| 243 | return file, nil | 248 | return file, nil |
| 244 | } | 249 | } |
| 250 | |||
| 251 | func deleteFile(service *drive.Service, fileId string) { | ||
| 252 | service.Files.Delete(fileId) | ||
| 253 | } | ||