diff options
Diffstat (limited to '')
| -rw-r--r-- | backend/controllers/demoController.go | 91 | ||||
| -rw-r--r-- | backend/controllers/recordController.go | 126 |
2 files changed, 126 insertions, 91 deletions
diff --git a/backend/controllers/demoController.go b/backend/controllers/demoController.go deleted file mode 100644 index 85f6ede..0000000 --- a/backend/controllers/demoController.go +++ /dev/null | |||
| @@ -1,91 +0,0 @@ | |||
| 1 | package controllers | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "context" | ||
| 5 | b64 "encoding/base64" | ||
| 6 | "fmt" | ||
| 7 | "io" | ||
| 8 | "log" | ||
| 9 | "net/http" | ||
| 10 | "os" | ||
| 11 | |||
| 12 | "github.com/gin-gonic/gin" | ||
| 13 | "golang.org/x/oauth2/google" | ||
| 14 | "golang.org/x/oauth2/jwt" | ||
| 15 | "google.golang.org/api/drive/v3" | ||
| 16 | ) | ||
| 17 | |||
| 18 | func UploadDemo(c *gin.Context) { | ||
| 19 | // Check if user exists | ||
| 20 | /*user, exists := c.Get("user") | ||
| 21 | if !exists { | ||
| 22 | c.JSON(http.StatusUnauthorized, gin.H{ | ||
| 23 | "code": http.StatusUnauthorized, | ||
| 24 | "output": gin.H{ | ||
| 25 | "error": "User not logged in. Could be invalid token.", | ||
| 26 | }, | ||
| 27 | }) | ||
| 28 | return | ||
| 29 | } else { | ||
| 30 | user := user.(models.User) | ||
| 31 | c.JSON(http.StatusOK, gin.H{ | ||
| 32 | "code": http.StatusOK, | ||
| 33 | "output": gin.H{ | ||
| 34 | "avatar": user.AvatarLink, | ||
| 35 | "country": user.CountryCode, | ||
| 36 | "types": user.TypeToString(), | ||
| 37 | "username": user.Username, | ||
| 38 | }, | ||
| 39 | "profile": true, | ||
| 40 | }) | ||
| 41 | return | ||
| 42 | }*/ | ||
| 43 | f, err := os.Open("pgun_2280.dem") | ||
| 44 | if err != nil { | ||
| 45 | panic(fmt.Sprintf("cannot open file: %v", err)) | ||
| 46 | } | ||
| 47 | defer f.Close() | ||
| 48 | client := serviceAccount() | ||
| 49 | srv, err := drive.New(client) | ||
| 50 | if err != nil { | ||
| 51 | log.Fatalf("Unable to retrieve drive Client %v", err) | ||
| 52 | } | ||
| 53 | file, err := createFile(srv, f.Name(), "application/octet-stream", f, os.Getenv("GOOGLE_FOLDER_ID")) | ||
| 54 | if err != nil { | ||
| 55 | panic(fmt.Sprintf("Could not create file: %v\n", err)) | ||
| 56 | } | ||
| 57 | |||
| 58 | fmt.Printf("File '%s' successfully uploaded", file.Name) | ||
| 59 | fmt.Printf("\nFile Id: '%s' ", file.Id) | ||
| 60 | } | ||
| 61 | |||
| 62 | // Use Service account | ||
| 63 | func serviceAccount() *http.Client { | ||
| 64 | privateKey, _ := b64.StdEncoding.DecodeString(os.Getenv("GOOGLE_PRIVATE_KEY_BASE64")) | ||
| 65 | config := &jwt.Config{ | ||
| 66 | Email: os.Getenv("GOOGLE_CLIENT_EMAIL"), | ||
| 67 | PrivateKey: []byte(privateKey), | ||
| 68 | Scopes: []string{ | ||
| 69 | drive.DriveScope, | ||
| 70 | }, | ||
| 71 | TokenURL: google.JWTTokenURL, | ||
| 72 | } | ||
| 73 | client := config.Client(context.Background()) | ||
| 74 | return client | ||
| 75 | } | ||
| 76 | |||
| 77 | func createFile(service *drive.Service, name string, mimeType string, content io.Reader, parentId string) (*drive.File, error) { | ||
| 78 | f := &drive.File{ | ||
| 79 | MimeType: mimeType, | ||
| 80 | Name: name, | ||
| 81 | Parents: []string{parentId}, | ||
| 82 | } | ||
| 83 | file, err := service.Files.Create(f).Media(content).Do() | ||
| 84 | |||
| 85 | if err != nil { | ||
| 86 | log.Println("Could not create file: " + err.Error()) | ||
| 87 | return nil, err | ||
| 88 | } | ||
| 89 | |||
| 90 | return file, nil | ||
| 91 | } | ||
diff --git a/backend/controllers/recordController.go b/backend/controllers/recordController.go new file mode 100644 index 0000000..a44d6f6 --- /dev/null +++ b/backend/controllers/recordController.go | |||
| @@ -0,0 +1,126 @@ | |||
| 1 | package controllers | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "context" | ||
| 5 | b64 "encoding/base64" | ||
| 6 | "fmt" | ||
| 7 | "io" | ||
| 8 | "log" | ||
| 9 | "net/http" | ||
| 10 | "os" | ||
| 11 | "time" | ||
| 12 | |||
| 13 | "github.com/gin-gonic/gin" | ||
| 14 | "github.com/pektezol/leastportals/backend/database" | ||
| 15 | "github.com/pektezol/leastportals/backend/models" | ||
| 16 | "golang.org/x/oauth2/google" | ||
| 17 | "golang.org/x/oauth2/jwt" | ||
| 18 | "google.golang.org/api/drive/v3" | ||
| 19 | ) | ||
| 20 | |||
| 21 | func CreateRecordWithDemo(c *gin.Context) { | ||
| 22 | mapId := c.Param("id") | ||
| 23 | // Check if user exists | ||
| 24 | user, exists := c.Get("user") | ||
| 25 | if !exists { | ||
| 26 | c.JSON(http.StatusUnauthorized, gin.H{ | ||
| 27 | "code": http.StatusUnauthorized, | ||
| 28 | "output": gin.H{ | ||
| 29 | "error": "User not logged in. Could be invalid token.", | ||
| 30 | }, | ||
| 31 | }) | ||
| 32 | return | ||
| 33 | } | ||
| 34 | var record models.Record | ||
| 35 | err := c.Bind(&record) | ||
| 36 | if err != nil { | ||
| 37 | c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) | ||
| 38 | return | ||
| 39 | } | ||
| 40 | var recordCount int | ||
| 41 | err = database.DB.QueryRow(`SELECT COUNT(id) FROM records;`).Scan(&recordCount) | ||
| 42 | if err != nil { | ||
| 43 | c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) | ||
| 44 | return | ||
| 45 | } | ||
| 46 | // var mapName string | ||
| 47 | // err = database.DB.QueryRow(`SELECT map_name FROM maps WHERE id = $1;`, mapId).Scan(&mapName) | ||
| 48 | // if err != nil { | ||
| 49 | // c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) | ||
| 50 | // return | ||
| 51 | // } | ||
| 52 | outputDemoName := fmt.Sprintf("%s_%s_%d", time.Now().UTC().Format("2006-01-02"), user.(models.User).SteamID, recordCount) | ||
| 53 | header, err := c.FormFile("file") | ||
| 54 | if err != nil { | ||
| 55 | c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) | ||
| 56 | return | ||
| 57 | } | ||
| 58 | err = c.SaveUploadedFile(header, "docs/"+header.Filename) | ||
| 59 | if err != nil { | ||
| 60 | c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) | ||
| 61 | return | ||
| 62 | } | ||
| 63 | f, err := os.Open("docs/" + header.Filename) | ||
| 64 | if err != nil { | ||
| 65 | c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) | ||
| 66 | return | ||
| 67 | } | ||
| 68 | defer f.Close() | ||
| 69 | client := serviceAccount() | ||
| 70 | srv, err := drive.New(client) | ||
| 71 | if err != nil { | ||
| 72 | c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) | ||
| 73 | return | ||
| 74 | } | ||
| 75 | file, err := createFile(srv, outputDemoName, "application/octet-stream", f, os.Getenv("GOOGLE_FOLDER_ID")) | ||
| 76 | if err != nil { | ||
| 77 | c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) | ||
| 78 | return | ||
| 79 | } | ||
| 80 | os.Remove("docs/" + header.Filename) | ||
| 81 | // Demo upload success | ||
| 82 | // Insert record into database | ||
| 83 | sql := `INSERT INTO records(map_id,host_id,score_count,score_time,is_coop,partner_id,demo_id) | ||
| 84 | VALUES ($1, $2, $3, $4, $5, $6, $7);` | ||
| 85 | _, err = database.DB.Exec(sql, mapId, user.(models.User).SteamID, record.ScoreCount, record.ScoreTime, record.IsCoop, record.PartnerID, file.Id) | ||
| 86 | if err != nil { | ||
| 87 | c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) | ||
| 88 | return | ||
| 89 | } | ||
| 90 | c.JSON(http.StatusOK, models.Response{ | ||
| 91 | Success: true, | ||
| 92 | Message: "Successfully created record.", | ||
| 93 | }) | ||
| 94 | return | ||
| 95 | } | ||
| 96 | |||
| 97 | // Use Service account | ||
| 98 | func serviceAccount() *http.Client { | ||
| 99 | privateKey, _ := b64.StdEncoding.DecodeString(os.Getenv("GOOGLE_PRIVATE_KEY_BASE64")) | ||
| 100 | config := &jwt.Config{ | ||
| 101 | Email: os.Getenv("GOOGLE_CLIENT_EMAIL"), | ||
| 102 | PrivateKey: []byte(privateKey), | ||
| 103 | Scopes: []string{ | ||
| 104 | drive.DriveScope, | ||
| 105 | }, | ||
| 106 | TokenURL: google.JWTTokenURL, | ||
| 107 | } | ||
| 108 | client := config.Client(context.Background()) | ||
| 109 | return client | ||
| 110 | } | ||
| 111 | |||
| 112 | func createFile(service *drive.Service, name string, mimeType string, content io.Reader, parentId string) (*drive.File, error) { | ||
| 113 | f := &drive.File{ | ||
| 114 | MimeType: mimeType, | ||
| 115 | Name: name, | ||
| 116 | Parents: []string{parentId}, | ||
| 117 | } | ||
| 118 | file, err := service.Files.Create(f).Media(content).Do() | ||
| 119 | |||
| 120 | if err != nil { | ||
| 121 | log.Println("Could not create file: " + err.Error()) | ||
| 122 | return nil, err | ||
| 123 | } | ||
| 124 | |||
| 125 | return file, nil | ||
| 126 | } | ||