/
action_site_sync_storage.go
86 lines (68 loc) · 2.67 KB
/
action_site_sync_storage.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
package resource
import (
"github.com/artpar/rclone/cmd"
log "github.com/sirupsen/logrus"
"github.com/artpar/api2go"
"github.com/artpar/rclone/fs/config"
"github.com/artpar/rclone/fs/sync"
"github.com/gin-gonic/gin/json"
"strings"
)
type SyncSiteStorageActionPerformer struct {
cruds map[string]*DbResource
}
func (d *SyncSiteStorageActionPerformer) Name() string {
return "site.storage.sync"
}
func (d *SyncSiteStorageActionPerformer) DoAction(request ActionRequest, inFields map[string]interface{}) (api2go.Responder, []ActionResponse, []error) {
responses := make([]ActionResponse, 0)
cloudStoreId := inFields["cloud_store_id"].(string)
tempDirectoryPath := inFields["path"].(string)
cloudStore, err := d.cruds["cloud_store"].GetCloudStoreByReferenceId(cloudStoreId)
if err != nil {
return nil, nil, []error{err}
}
oauthTokenId := cloudStore.OAutoTokenId
token, oauthConf, err := d.cruds["oauth_token"].GetTokenByTokenReferenceId(oauthTokenId)
CheckErr(err, "Failed to get oauth2 token for storage sync")
jsonToken, err := json.Marshal(token)
CheckErr(err, "Failed to convert token to json")
config.FileSet(cloudStore.StoreProvider, "client_id", oauthConf.ClientID)
config.FileSet(cloudStore.StoreProvider, "type", cloudStore.StoreProvider)
config.FileSet(cloudStore.StoreProvider, "client_secret", oauthConf.ClientSecret)
config.FileSet(cloudStore.StoreProvider, "token", string(jsonToken))
config.FileSet(cloudStore.StoreProvider, "client_scopes", strings.Join(oauthConf.Scopes, ","))
config.FileSet(cloudStore.StoreProvider, "redirect_url", oauthConf.RedirectURL)
args := []string{
cloudStore.RootPath,
tempDirectoryPath,
}
fsrc, fdst := cmd.NewFsSrcDst(args)
log.Infof("Temp dir for site [%v]/%v ==> %v", cloudStore.Name, cloudStore.RootPath, tempDirectoryPath)
go cmd.Run(true, true, nil, func() error {
if fsrc == nil || fdst == nil {
log.Errorf("Either source or destination is empty")
return nil
}
log.Infof("Starting to copy drive for site base from [%v] to [%v]", fsrc.String(), fdst.String())
if fsrc == nil || fdst == nil {
log.Errorf("Source or destination is null")
return nil
}
dir := sync.CopyDir(fdst, fsrc)
return dir
})
restartAttrs := make(map[string]interface{})
restartAttrs["type"] = "success"
restartAttrs["message"] = "Cloud storage file upload queued"
restartAttrs["title"] = "Success"
actionResponse := NewActionResponse("client.notify", restartAttrs)
responses = append(responses, actionResponse)
return nil, responses, nil
}
func NewSyncSiteStorageActionPerformer(cruds map[string]*DbResource) (ActionPerformerInterface, error) {
handler := SyncSiteStorageActionPerformer{
cruds: cruds,
}
return &handler, nil
}