|
1 | 1 | package internals |
2 | 2 |
|
3 | | -import ( |
4 | | - "context" |
5 | | - "io" |
6 | | - "os" |
7 | | - "path/filepath" |
8 | | - |
9 | | - trace "github.com/rs/zerolog/log" |
10 | | - "google.golang.org/api/drive/v3" |
11 | | - "google.golang.org/api/option" |
12 | | - |
13 | | - _ "embed" |
14 | | -) |
15 | | - |
16 | 3 | const SHEETS_MIME_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" |
17 | 4 |
|
18 | 5 | type DownloadConfig struct { |
19 | 6 | Id string |
20 | 7 | Path string |
21 | 8 | Name string |
22 | 9 | } |
23 | | - |
24 | | -//go:embed secret.json |
25 | | -var apiKey []byte |
26 | | - |
27 | | -func DownloadFile(config DownloadConfig) error { |
28 | | - trace.Trace().Str("id", config.Id).Str("path", config.Path).Str("name", config.Name).Msg("download file") |
29 | | - client, errClient := getClient(apiKey) |
30 | | - if errClient != nil { |
31 | | - trace.Error().Str("id", config.Id).Str("path", config.Path).Str("name", config.Name).Stack().Err(errClient).Msg("") |
32 | | - return errClient |
33 | | - } |
34 | | - |
35 | | - file, errFile := getFile(client, config.Id, SHEETS_MIME_TYPE) |
36 | | - if errFile != nil { |
37 | | - trace.Error().Str("id", config.Id).Str("path", config.Path).Str("name", config.Name).Stack().Err(errFile).Msg("") |
38 | | - return errFile |
39 | | - } |
40 | | - |
41 | | - errSaving := saveFile(file, config.Path, config.Name) |
42 | | - if errSaving != nil { |
43 | | - trace.Error().Str("id", config.Id).Str("path", config.Path).Str("name", config.Name).Stack().Err(errSaving).Msg("") |
44 | | - } |
45 | | - |
46 | | - return errSaving |
47 | | -} |
48 | | - |
49 | | -func getClient(apiKey []byte) (*drive.Service, error) { |
50 | | - trace.Trace().Msg("get client") |
51 | | - ctx := context.Background() |
52 | | - |
53 | | - // client, err := drive.NewService(ctx, option.WithCredentialsFile(credentials)) |
54 | | - client, err := drive.NewService(ctx, option.WithCredentialsJSON(apiKey)) |
55 | | - |
56 | | - if err != nil { |
57 | | - return nil, err |
58 | | - } |
59 | | - |
60 | | - return client, nil |
61 | | -} |
62 | | - |
63 | | -func getFile(client *drive.Service, id string, mimeType string) ([]byte, error) { |
64 | | - trace.Trace().Str("id", id).Msg("get file") |
65 | | - resp, err := client.Files.Export(id, mimeType).Download() |
66 | | - if err != nil { |
67 | | - return nil, err |
68 | | - } |
69 | | - defer resp.Body.Close() |
70 | | - |
71 | | - data, err := io.ReadAll(resp.Body) |
72 | | - if err != nil { |
73 | | - return nil, err |
74 | | - } |
75 | | - |
76 | | - return data, nil |
77 | | -} |
78 | | - |
79 | | -func saveFile(content []byte, path string, name string) error { |
80 | | - trace.Trace().Str("path", path).Str("name", name).Msg("save file") |
81 | | - |
82 | | - err := os.Mkdir(path, 0777) |
83 | | - |
84 | | - if !os.IsExist(err) { |
85 | | - return err |
86 | | - } |
87 | | - |
88 | | - err = os.WriteFile(filepath.Join(path, name), content, 0644) // rw-r--r-- |
89 | | - if err != nil { |
90 | | - return err |
91 | | - } |
92 | | - return nil |
93 | | -} |
0 commit comments