9 "github.com/iotbzh/xds-server/lib/folder"
10 "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 *[]folder.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.RawPath, s.conf.FileConf.ShareRootDir)
37 cliPath = stFld.RawPath
39 *f = append(*f, folder.FolderConfig{
42 ClientPath: strings.TrimRight(cliPath, "/"),
43 Type: folder.TypeCloudSync,
44 Status: folder.StatusDisable,
45 DefaultSdk: defaultSdk,
46 RootPath: s.conf.FileConf.ShareRootDir,
47 DataCloudSync: folder.CloudSyncConfig{SyncThingID: devID},
54 // FolderChange is called when configuration has changed
55 func (s *SyncThing) FolderChange(f folder.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 := config.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 := config.FolderConfiguration{
101 RawPath: 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, config.FolderDeviceConfiguration{
109 DeviceID: newDevice.DeviceID,
113 var fld config.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) (config.FolderConfiguration, error) {
159 fc := config.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 // FIXME better to detected FolderCompletion event (/rest/events)
195 // See https://docs.syncthing.net/dev/events.html
196 sts, err := s.FolderStatus(folderID)
200 return sts.NeedBytes == 0, nil
203 // FolderScan Request immediate folder scan.
204 // Scan all folders if folderID param is empty
205 func (s *SyncThing) FolderScan(folderID string, subpath string) error {
208 url += "?folder=" + folderID
211 url += "&sub=" + subpath
214 return s.client.HTTPPost(url, "")