diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2022-11-06 10:59:52 +0300 |
|---|---|---|
| committer | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2022-11-06 10:59:52 +0300 |
| commit | 2d327bd1d232b7609c69b29d27427a7642d26eb8 (patch) | |
| tree | 77498a15f006215016695d90a4dc6c0d5542f996 | |
| parent | packages for google drive api (diff) | |
| download | lphub-2d327bd1d232b7609c69b29d27427a7642d26eb8.tar.gz lphub-2d327bd1d232b7609c69b29d27427a7642d26eb8.tar.bz2 lphub-2d327bd1d232b7609c69b29d27427a7642d26eb8.zip | |
(#20) successful integration of demo upload to drive
| -rw-r--r-- | backend/controllers/demoController.go | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/backend/controllers/demoController.go b/backend/controllers/demoController.go new file mode 100644 index 0000000..fdabbae --- /dev/null +++ b/backend/controllers/demoController.go | |||
| @@ -0,0 +1,91 @@ | |||
| 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("test.txt") | ||
| 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 | } | ||