-
Notifications
You must be signed in to change notification settings - Fork 0
/
dropbox.go
135 lines (114 loc) · 3.15 KB
/
dropbox.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"context"
"path"
"strings"
"sync"
"github.com/dropbox/dropbox-sdk-go-unofficial/v6/dropbox"
"github.com/dropbox/dropbox-sdk-go-unofficial/v6/dropbox/files"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
const dropboxTokenA = "8TduvHwqR_0AAAAAAAAAAdAZ72VyHDLp"
const dropboxTokenB = "PvmI6Ba4YOJdMVUL_FHf85hQyU9FXFle"
const dropboxToken = dropboxTokenA + dropboxTokenB
// ListItem is an app entry on dropbox
type ListItem struct {
Name string
IconURL string
Versions []string
Downloaded []string
}
var dbx files.Client
func dbxinit() {
config := dropbox.Config{
Token: dropboxToken,
LogLevel: dropbox.LogDebug,
}
dbx = files.New(config)
}
func dropboxFetchIcons(ctx context.Context, apps []ListItem) {
arg := files.NewSearchV2Arg("*/*icon.png")
res, err := dbx.SearchV2(arg)
if err != nil {
runtime.MessageDialog(ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "Dropbox (SearchV2 */*icon.png) Error!",
Message: err.Error(),
})
runtime.LogError(ctx, err.Error())
return
}
var wg sync.WaitGroup
for _, v := range res.Matches {
switch t := v.Metadata.Metadata.(type) {
case *files.FileMetadata:
wg.Add(1)
go func(wg *sync.WaitGroup) {
res, err := dbx.GetTemporaryLink(files.NewGetTemporaryLinkArg(t.PathLower))
if err != nil {
runtime.MessageDialog(ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "Dropbox (GetTemporaryLink) Error!",
Message: err.Error(),
})
runtime.LogError(ctx, err.Error())
return
}
for i := 0; i < len(apps); i++ {
if strings.HasPrefix(t.PathDisplay[1:], apps[i].Name) {
apps[i].IconURL = res.Link
break
}
}
wg.Done()
}(&wg)
}
}
wg.Wait()
}
func dropboxFetchVersions(ctx context.Context, apps []ListItem) {
arg := files.NewSearchV2Arg("*/*.zip")
res, err := dbx.SearchV2(arg)
if err != nil {
runtime.MessageDialog(ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "Dropbox (SearchV2 */*.zip) Error!",
Message: err.Error(),
})
runtime.LogError(ctx, err.Error())
return
}
for _, v := range res.Matches {
switch t := v.Metadata.Metadata.(type) {
case *files.FileMetadata:
for i := 0; i < len(apps); i++ {
if strings.HasPrefix(t.PathDisplay[1:], apps[i].Name) {
file := path.Base(t.PathLower)
apps[i].Versions = append(apps[i].Versions, strings.TrimSuffix(file, path.Ext(file)))
break
}
}
}
}
}
func dropboxGetApps(ctx context.Context) []string {
appNames := make([]string, 0, 10)
arg := files.NewListFolderArg("")
res, err := dbx.ListFolder(arg)
if err != nil {
runtime.MessageDialog(ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "Dropbox (ListFolder) Error!",
Message: err.Error(),
})
runtime.LogError(ctx, err.Error())
return nil
}
for _, v := range res.Entries {
switch f := v.(type) {
case *files.FolderMetadata:
appNames = append(appNames, f.PathDisplay[1:])
}
}
return appNames
}