9 "github.com/iotbzh/xds-server/lib/xsapiv1"
10 stconfig "github.com/syncthing/syncthing/lib/config"
11 "github.com/syncthing/syncthing/lib/protocol"
14 // FolderLoadFromStConfig Load/Retrieve folder config from syncthing database
15 func (s *SyncThing) FolderLoadFromStConfig(f *[]xsapiv1.FolderConfig) error {
17 defaultSdk := "" // cannot know which was the default sdk
19 stCfg, err := s.ConfigGet()
23 if len(stCfg.Devices) < 1 {
24 return fmt.Errorf("Cannot load syncthing config: no device defined")
26 devID := stCfg.Devices[0].DeviceID.String()
28 if len(stCfg.Devices) < 2 {
29 return fmt.Errorf("Cannot load syncthing config: no valid device found")
31 devID = stCfg.Devices[1].DeviceID.String()
34 for _, stFld := range stCfg.Folders {
35 cliPath := strings.TrimPrefix(stFld.Path, s.conf.FileConf.ShareRootDir)
39 *f = append(*f, xsapiv1.FolderConfig{
42 ClientPath: strings.TrimRight(cliPath, "/"),
43 Type: xsapiv1.TypeCloudSync,
44 Status: xsapiv1.StatusDisable,
45 DefaultSdk: defaultSdk,
46 RootPath: s.conf.FileConf.ShareRootDir,
47 DataCloudSync: xsapiv1.CloudSyncConfig{SyncThingID: devID},
54 // FolderChange is called when configuration has changed
55 func (s *SyncThing) FolderChange(f xsapiv1.FolderConfig) (string, error) {
58 stCfg, err := s.ConfigGet()
64 stClientID := f.DataCloudSync.SyncThingID
65 // Add new Device if needed
66 var devID protocol.DeviceID
67 if err := devID.UnmarshalText([]byte(stClientID)); err != nil {
68 s.log.Errorf("not a valid device id (err %v)", err)
72 newDevice := stconfig.DeviceConfiguration{
75 Addresses: []string{"dynamic"},
79 for _, device := range stCfg.Devices {
80 if device.DeviceID == devID {
86 stCfg.Devices = append(stCfg.Devices, newDevice)
89 // Add or update Folder settings
91 if label = f.Label; label == "" {
92 label = strings.Split(id, "/")[0]
94 if id = f.ID; id == "" {
95 id = stClientID[0:15] + "_" + label
98 folder := stconfig.FolderConfiguration{
101 Path: filepath.Join(s.conf.FileConf.ShareRootDir, f.ClientPath),
104 if s.conf.FileConf.SThgConf.RescanIntervalS > 0 {
105 folder.RescanIntervalS = s.conf.FileConf.SThgConf.RescanIntervalS
108 folder.Devices = append(folder.Devices, stconfig.FolderDeviceConfiguration{
109 DeviceID: newDevice.DeviceID,
113 var fld stconfig.FolderConfiguration
114 for _, fld = range stCfg.Folders {
115 if folder.ID == fld.ID {
122 stCfg.Folders = append(stCfg.Folders, folder)
123 fld = stCfg.Folders[0]
126 err = s.ConfigSet(stCfg)
134 // FolderDelete is called to delete a folder config
135 func (s *SyncThing) FolderDelete(id string) error {
136 // Get current config
137 stCfg, err := s.ConfigGet()
143 for i, fld := range stCfg.Folders {
145 stCfg.Folders = append(stCfg.Folders[:i], stCfg.Folders[i+1:]...)
146 err = s.ConfigSet(stCfg)
157 // FolderConfigGet Returns the configuration of a specific folder
158 func (s *SyncThing) FolderConfigGet(folderID string) (stconfig.FolderConfiguration, error) {
159 fc := stconfig.FolderConfiguration{}
161 return fc, fmt.Errorf("folderID not set")
163 cfg, err := s.ConfigGet()
167 for _, f := range cfg.Folders {
168 if f.ID == folderID {
173 return fc, fmt.Errorf("id not found")
176 // FolderStatus Returns all information about the current
177 func (s *SyncThing) FolderStatus(folderID string) (*FolderStatus, error) {
181 return nil, fmt.Errorf("folderID not set")
183 if err := s.client.HTTPGet("db/status?folder="+folderID, &data); err != nil {
186 if err := json.Unmarshal(data, &res); err != nil {
192 // IsFolderInSync Returns true when folder is in sync
193 func (s *SyncThing) IsFolderInSync(folderID string) (bool, error) {
194 sts, err := s.FolderStatus(folderID)
198 return sts.NeedBytes == 0 && sts.State == "idle", nil
201 // FolderScan Request immediate folder scan.
202 // Scan all folders if folderID param is empty
203 func (s *SyncThing) FolderScan(folderID string, subpath string) error {
206 url += "?folder=" + folderID
209 url += "&sub=" + subpath
212 return s.client.HTTPPost(url, "")