8 common "github.com/iotbzh/xds-common/golib"
9 "github.com/iotbzh/xds-server/lib/folder"
10 stconfig "github.com/syncthing/syncthing/lib/config"
11 "github.com/syncthing/syncthing/lib/protocol"
14 // FIXME remove and use an interface on xdsconfig.FolderConfig
15 type FolderChangeArg struct {
23 // FolderLoadFromStConfig Load/Retrieve folder config from syncthing database
24 func (s *SyncThing) FolderLoadFromStConfig(f *[]folder.FolderConfig) error {
26 defaultSdk := "" // cannot know which was the default sdk
28 stCfg, err := s.ConfigGet()
32 if len(stCfg.Devices) < 1 {
33 return fmt.Errorf("Cannot load syncthing config: no device defined")
35 devID := stCfg.Devices[0].DeviceID.String()
37 if len(stCfg.Devices) < 2 {
38 return fmt.Errorf("Cannot load syncthing config: no valid device found")
40 devID = stCfg.Devices[1].DeviceID.String()
43 for _, stFld := range stCfg.Folders {
45 cliPath := strings.TrimPrefix(stFld.Path, s.conf.FileConf.ShareRootDir)
50 *f = append(*f, folder.FolderConfig{
53 ClientPath: strings.TrimRight(cliPath, "/"),
54 Type: folder.TypeCloudSync,
55 Status: folder.StatusDisable,
56 DefaultSdk: defaultSdk,
57 RootPath: "", //s.conf.FileConf.ShareRootDir,
58 DataCloudSync: folder.CloudSyncConfig{SyncThingID: devID},
65 // FolderChange is called when configuration has changed
66 func (s *SyncThing) FolderChange(f FolderChangeArg) (string, error) {
69 stCfg, err := s.ConfigGet()
75 // Add new Device if needed
76 var devID protocol.DeviceID
77 if err := devID.UnmarshalText([]byte(f.SyncThingID)); err != nil {
78 s.log.Errorf("not a valid device id (err %v)", err)
82 newDevice := stconfig.DeviceConfiguration{
85 Addresses: []string{"dynamic"},
89 for _, device := range stCfg.Devices {
90 if device.DeviceID == devID {
96 stCfg.Devices = append(stCfg.Devices, newDevice)
99 // Add or update Folder settings
101 if label = f.Label; label == "" {
102 label = strings.Split(id, "/")[0]
104 if id = f.ID; id == "" {
105 id = f.SyncThingID[0:15] + "_" + label
108 // Resolve local path
109 pathCli, err := common.ResolveEnvVar(f.RelativePath)
111 pathCli = f.RelativePath
113 // SEB still need ShareRootDir ? a sup
114 // pathCli := filepath.Join(f.ShareRootDir, f.RelativePath)
116 folder := stconfig.FolderConfiguration{
124 if s.conf.FileConf.SThgConf.RescanIntervalS > 0 {
125 folder.RescanIntervalS = s.conf.FileConf.SThgConf.RescanIntervalS
129 folder.Devices = append(folder.Devices, stconfig.FolderDeviceConfiguration{
130 DeviceID: newDevice.DeviceID,
134 var fld stconfig.FolderConfiguration
135 for _, fld = range stCfg.Folders {
136 if folder.ID == fld.ID {
143 stCfg.Folders = append(stCfg.Folders, folder)
144 fld = stCfg.Folders[0]
147 err = s.ConfigSet(stCfg)
155 // FolderDelete is called to delete a folder config
156 func (s *SyncThing) FolderDelete(id string) error {
157 // Get current config
158 stCfg, err := s.ConfigGet()
164 for i, fld := range stCfg.Folders {
166 stCfg.Folders = append(stCfg.Folders[:i], stCfg.Folders[i+1:]...)
167 err = s.ConfigSet(stCfg)
178 // FolderConfigGet Returns the configuration of a specific folder
179 func (s *SyncThing) FolderConfigGet(folderID string) (stconfig.FolderConfiguration, error) {
180 fc := stconfig.FolderConfiguration{}
182 return fc, fmt.Errorf("folderID not set")
184 cfg, err := s.ConfigGet()
188 for _, f := range cfg.Folders {
189 if f.ID == folderID {
194 return fc, fmt.Errorf("id not found")
197 // FolderStatus Returns all information about the current
198 func (s *SyncThing) FolderStatus(folderID string) (*FolderStatus, error) {
202 return nil, fmt.Errorf("folderID not set")
204 if err := s.client.HTTPGet("db/status?folder="+folderID, &data); err != nil {
207 if err := json.Unmarshal(data, &res); err != nil {
213 // IsFolderInSync Returns true when folder is in sync
214 func (s *SyncThing) IsFolderInSync(folderID string) (bool, error) {
215 sts, err := s.FolderStatus(folderID)
219 return sts.NeedBytes == 0 && sts.State == "idle", nil
222 // FolderScan Request immediate folder scan.
223 // Scan all folders if folderID param is empty
224 func (s *SyncThing) FolderScan(folderID string, subpath string) error {
227 url += "?folder=" + folderID
230 url += "&sub=" + subpath
233 return s.client.HTTPPost(url, "")