diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-01-12 11:49:23 +0300 |
|---|---|---|
| committer | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-01-12 11:49:23 +0300 |
| commit | 24f3c154665f777e985d29263ed99a7b968949cc (patch) | |
| tree | 2e8f8e29623c885d1068ec554afbf081057d4bc1 /backend/controllers/demoController.go | |
| parent | remove cutscene maps (#22) (diff) | |
| download | lphub-24f3c154665f777e985d29263ed99a7b968949cc.tar.gz lphub-24f3c154665f777e985d29263ed99a7b968949cc.tar.bz2 lphub-24f3c154665f777e985d29263ed99a7b968949cc.zip | |
demo to google drive is worknig properly (#20)
Diffstat (limited to 'backend/controllers/demoController.go')
| -rw-r--r-- | backend/controllers/demoController.go | 91 |
1 files changed, 0 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 | } | ||