From 24f3c154665f777e985d29263ed99a7b968949cc Mon Sep 17 00:00:00 2001 From: Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> Date: Thu, 12 Jan 2023 11:49:23 +0300 Subject: demo to google drive is worknig properly (#20) --- backend/controllers/demoController.go | 91 ----------------------- backend/controllers/recordController.go | 126 ++++++++++++++++++++++++++++++++ backend/database/init.sql | 11 +-- backend/models/models.go | 17 ++++- backend/routes/routes.go | 2 +- 5 files changed, 146 insertions(+), 101 deletions(-) delete mode 100644 backend/controllers/demoController.go create mode 100644 backend/controllers/recordController.go (limited to 'backend') 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 @@ -package controllers - -import ( - "context" - b64 "encoding/base64" - "fmt" - "io" - "log" - "net/http" - "os" - - "github.com/gin-gonic/gin" - "golang.org/x/oauth2/google" - "golang.org/x/oauth2/jwt" - "google.golang.org/api/drive/v3" -) - -func UploadDemo(c *gin.Context) { - // Check if user exists - /*user, exists := c.Get("user") - if !exists { - c.JSON(http.StatusUnauthorized, gin.H{ - "code": http.StatusUnauthorized, - "output": gin.H{ - "error": "User not logged in. Could be invalid token.", - }, - }) - return - } else { - user := user.(models.User) - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "output": gin.H{ - "avatar": user.AvatarLink, - "country": user.CountryCode, - "types": user.TypeToString(), - "username": user.Username, - }, - "profile": true, - }) - return - }*/ - f, err := os.Open("pgun_2280.dem") - if err != nil { - panic(fmt.Sprintf("cannot open file: %v", err)) - } - defer f.Close() - client := serviceAccount() - srv, err := drive.New(client) - if err != nil { - log.Fatalf("Unable to retrieve drive Client %v", err) - } - file, err := createFile(srv, f.Name(), "application/octet-stream", f, os.Getenv("GOOGLE_FOLDER_ID")) - if err != nil { - panic(fmt.Sprintf("Could not create file: %v\n", err)) - } - - fmt.Printf("File '%s' successfully uploaded", file.Name) - fmt.Printf("\nFile Id: '%s' ", file.Id) -} - -// Use Service account -func serviceAccount() *http.Client { - privateKey, _ := b64.StdEncoding.DecodeString(os.Getenv("GOOGLE_PRIVATE_KEY_BASE64")) - config := &jwt.Config{ - Email: os.Getenv("GOOGLE_CLIENT_EMAIL"), - PrivateKey: []byte(privateKey), - Scopes: []string{ - drive.DriveScope, - }, - TokenURL: google.JWTTokenURL, - } - client := config.Client(context.Background()) - return client -} - -func createFile(service *drive.Service, name string, mimeType string, content io.Reader, parentId string) (*drive.File, error) { - f := &drive.File{ - MimeType: mimeType, - Name: name, - Parents: []string{parentId}, - } - file, err := service.Files.Create(f).Media(content).Do() - - if err != nil { - log.Println("Could not create file: " + err.Error()) - return nil, err - } - - return file, nil -} 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 @@ +package controllers + +import ( + "context" + b64 "encoding/base64" + "fmt" + "io" + "log" + "net/http" + "os" + "time" + + "github.com/gin-gonic/gin" + "github.com/pektezol/leastportals/backend/database" + "github.com/pektezol/leastportals/backend/models" + "golang.org/x/oauth2/google" + "golang.org/x/oauth2/jwt" + "google.golang.org/api/drive/v3" +) + +func CreateRecordWithDemo(c *gin.Context) { + mapId := c.Param("id") + // Check if user exists + user, exists := c.Get("user") + if !exists { + c.JSON(http.StatusUnauthorized, gin.H{ + "code": http.StatusUnauthorized, + "output": gin.H{ + "error": "User not logged in. Could be invalid token.", + }, + }) + return + } + var record models.Record + err := c.Bind(&record) + if err != nil { + c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) + return + } + var recordCount int + err = database.DB.QueryRow(`SELECT COUNT(id) FROM records;`).Scan(&recordCount) + if err != nil { + c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) + return + } + // var mapName string + // err = database.DB.QueryRow(`SELECT map_name FROM maps WHERE id = $1;`, mapId).Scan(&mapName) + // if err != nil { + // c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) + // return + // } + outputDemoName := fmt.Sprintf("%s_%s_%d", time.Now().UTC().Format("2006-01-02"), user.(models.User).SteamID, recordCount) + header, err := c.FormFile("file") + if err != nil { + c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) + return + } + err = c.SaveUploadedFile(header, "docs/"+header.Filename) + if err != nil { + c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) + return + } + f, err := os.Open("docs/" + header.Filename) + if err != nil { + c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) + return + } + defer f.Close() + client := serviceAccount() + srv, err := drive.New(client) + if err != nil { + c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) + return + } + file, err := createFile(srv, outputDemoName, "application/octet-stream", f, os.Getenv("GOOGLE_FOLDER_ID")) + if err != nil { + c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) + return + } + os.Remove("docs/" + header.Filename) + // Demo upload success + // Insert record into database + sql := `INSERT INTO records(map_id,host_id,score_count,score_time,is_coop,partner_id,demo_id) + VALUES ($1, $2, $3, $4, $5, $6, $7);` + _, err = database.DB.Exec(sql, mapId, user.(models.User).SteamID, record.ScoreCount, record.ScoreTime, record.IsCoop, record.PartnerID, file.Id) + if err != nil { + c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) + return + } + c.JSON(http.StatusOK, models.Response{ + Success: true, + Message: "Successfully created record.", + }) + return +} + +// Use Service account +func serviceAccount() *http.Client { + privateKey, _ := b64.StdEncoding.DecodeString(os.Getenv("GOOGLE_PRIVATE_KEY_BASE64")) + config := &jwt.Config{ + Email: os.Getenv("GOOGLE_CLIENT_EMAIL"), + PrivateKey: []byte(privateKey), + Scopes: []string{ + drive.DriveScope, + }, + TokenURL: google.JWTTokenURL, + } + client := config.Client(context.Background()) + return client +} + +func createFile(service *drive.Service, name string, mimeType string, content io.Reader, parentId string) (*drive.File, error) { + f := &drive.File{ + MimeType: mimeType, + Name: name, + Parents: []string{parentId}, + } + file, err := service.Files.Create(f).Media(content).Do() + + if err != nil { + log.Println("Could not create file: " + err.Error()) + return nil, err + } + + return file, nil +} diff --git a/backend/database/init.sql b/backend/database/init.sql index 4f68e0d..f69bf10 100644 --- a/backend/database/init.sql +++ b/backend/database/init.sql @@ -25,12 +25,14 @@ CREATE TABLE maps ( CREATE TABLE records ( id SERIAL, - map_id SMALLINT, + map_id SMALLINT NOT NULL, host_id TEXT NOT NULL, score_count SMALLINT NOT NULL, score_time INTEGER NOT NULL, is_coop BOOLEAN NOT NULL DEFAULT false, partner_id TEXT NOT NULL DEFAULT '', + demo_id TEXT NOT NULL, + record_date TIMESTAMP NOT NULL DEFAULT now(), PRIMARY KEY (id), FOREIGN KEY (map_id) REFERENCES maps(id), FOREIGN KEY (host_id) REFERENCES users(steam_id), @@ -42,11 +44,4 @@ CREATE TABLE titles ( title_name TEXT NOT NULL, PRIMARY KEY (user_id), FOREIGN KEY (user_id) REFERENCES users(steam_id) -); - -CREATE TABLE showcases ( - record_id INT, - video_id TEXT NOT NULL, - PRIMARY KEY (record_id), - FOREIGN KEY (record_id) REFERENCES records(id) ); \ No newline at end of file diff --git a/backend/models/models.go b/backend/models/models.go index c49eaeb..994a1e7 100644 --- a/backend/models/models.go +++ b/backend/models/models.go @@ -1,6 +1,9 @@ package models -import "time" +import ( + "mime/multipart" + "time" +) type User struct { SteamID string `json:"steam_id"` @@ -10,3 +13,15 @@ type User struct { CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` } + +type Record struct { + MapID int `json:"map_id" binding:"required"` + ScoreCount int `json:"score_count" binding:"required"` + ScoreTime int `json:"score_time" binding:"required"` + IsCoop bool `json:"is_coop" binding:"required"` + PartnerID string `json:"partner_id"` +} + +type ds struct { + File *multipart.FileHeader `form:"file" binding:"required"` +} diff --git a/backend/routes/routes.go b/backend/routes/routes.go index 6ca1d46..f9256ca 100644 --- a/backend/routes/routes.go +++ b/backend/routes/routes.go @@ -17,6 +17,6 @@ func InitRoutes(router *gin.Engine) { v1.GET("/login", controllers.Login) v1.GET("/profile", middleware.CheckAuth, controllers.Profile) v1.GET("/user/:id", middleware.CheckAuth, controllers.FetchUser) - v1.POST("/demo/", middleware.CheckAuth, controllers.UploadDemo) + v1.POST("/record/:id", middleware.CheckAuth, controllers.CreateRecordWithDemo) } } -- cgit v1.2.3