7 "github.com/iotbzh/xds-server/lib/xdsconfig"
8 "github.com/syncthing/syncthing/lib/config"
9 "github.com/syncthing/syncthing/lib/protocol"
12 // FolderChange is called when configuration has changed
13 func (s *SyncThing) FolderChange(f xdsconfig.FolderConfig) error {
16 stCfg, err := s.ConfigGet()
22 // Add new Device if needed
23 var devID protocol.DeviceID
24 if err := devID.UnmarshalText([]byte(f.SyncThingID)); err != nil {
25 s.log.Errorf("not a valid device id (err %v)\n", err)
29 newDevice := config.DeviceConfiguration{
32 Addresses: []string{"dynamic"},
36 for _, device := range stCfg.Devices {
37 if device.DeviceID == devID {
43 stCfg.Devices = append(stCfg.Devices, newDevice)
46 // Add or update Folder settings
48 if label = f.Label; label == "" {
49 label = strings.Split(id, "/")[0]
51 if id = f.ID; id == "" {
52 id = f.SyncThingID[0:15] + "_" + label
55 folder := config.FolderConfiguration{
58 RawPath: filepath.Join(s.conf.FileConf.ShareRootDir, f.RelativePath),
61 if s.conf.FileConf.SThgConf.RescanIntervalS > 0 {
62 folder.RescanIntervalS = s.conf.FileConf.SThgConf.RescanIntervalS
65 folder.Devices = append(folder.Devices, config.FolderDeviceConfiguration{
66 DeviceID: newDevice.DeviceID,
70 var fld config.FolderConfiguration
71 for _, fld = range stCfg.Folders {
72 if folder.ID == fld.ID {
79 stCfg.Folders = append(stCfg.Folders, folder)
80 fld = stCfg.Folders[0]
83 err = s.ConfigSet(stCfg)
91 // FolderDelete is called to delete a folder config
92 func (s *SyncThing) FolderDelete(id string) error {
94 stCfg, err := s.ConfigGet()
100 for i, fld := range stCfg.Folders {
102 stCfg.Folders = append(stCfg.Folders[:i], stCfg.Folders[i+1:]...)
103 err = s.ConfigSet(stCfg)