summaryrefslogtreecommitdiffstats
path: root/lib/syncthing/folder-st.go
blob: ffcd2840ff8c91b8d243a3ec9f2a1f79e850cfa4 (plain)
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
package st

import (
	"fmt"
	"path/filepath"

	"github.com/iotbzh/xds-server/lib/folder"
	"github.com/iotbzh/xds-server/lib/xdsconfig"
	"github.com/syncthing/syncthing/lib/config"
)

// IFOLDER interface implementation for syncthing

// STFolder .
type STFolder struct {
	globalConfig *xdsconfig.Config
	st           *SyncThing
	fConfig      folder.FolderConfig
	stfConfig    config.FolderConfiguration
}

// NewFolderST Create a new instance of STFolder
func (s *SyncThing) NewFolderST(gc *xdsconfig.Config) *STFolder {
	return &STFolder{
		globalConfig: gc,
		st:           s,
	}
}

// Add a new folder
func (f *STFolder) Add(cfg folder.FolderConfig) (*folder.FolderConfig, error) {

	// Sanity check
	if cfg.DataCloudSync.SyncThingID == "" {
		return nil, fmt.Errorf("device id not set (SyncThingID field)")
	}

	// rootPath should not be empty
	if cfg.RootPath == "" {
		cfg.RootPath = f.globalConfig.FileConf.ShareRootDir
	}

	f.fConfig = cfg

	f.fConfig.DataCloudSync.BuilderSThgID = f.st.MyID // FIXME - should be removed after local ST config rework

	// Update Syncthing folder
	// (expect if status is ErrorConfig)
	// TODO: add cache to avoid multiple requests on startup
	if f.fConfig.Status != folder.StatusErrorConfig {
		id, err := f.st.FolderChange(f.fConfig)
		if err != nil {
			return nil, err
		}

		f.stfConfig, err = f.st.FolderConfigGet(id)
		if err != nil {
			f.fConfig.Status = folder.StatusErrorConfig
			return nil, err
		}

		f.fConfig.Status = folder.StatusEnable
	}

	return &f.fConfig, nil
}

// GetConfig Get public part of folder config
func (f *STFolder) GetConfig() folder.FolderConfig {
	return f.fConfig
}

// GetFullPath returns the full path
func (f *STFolder) GetFullPath(dir string) string {
	if &dir == nil {
		dir = ""
	}
	if filepath.IsAbs(dir) {
		return filepath.Join(f.fConfig.RootPath, dir)
	}
	return filepath.Join(f.fConfig.RootPath, f.fConfig.ClientPath, dir)
}

// Remove a folder
func (f *STFolder) Remove() error {
	return f.st.FolderDelete(f.stfConfig.ID)
}

// Sync Force folder files synchronization
func (f *STFolder) Sync() error {
	return f.st.FolderScan(f.stfConfig.ID, "")
}

// IsInSync Check if folder files are in-sync
func (f *STFolder) IsInSync() (bool, error) {
	return f.st.IsFolderInSync(f.stfConfig.ID)
}