2 * Copyright (C) 2017 "IoT.bzh"
3 * Author Sebastien Douheret <sebastien@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
26 "github.com/iotbzh/xds-server/lib/xsapiv1"
27 stconfig "github.com/syncthing/syncthing/lib/config"
28 "github.com/syncthing/syncthing/lib/protocol"
31 // FolderLoadFromStConfig Load/Retrieve folder config from syncthing database
32 func (s *SyncThing) FolderLoadFromStConfig(f *[]xsapiv1.FolderConfig) error {
34 defaultSdk := "" // cannot know which was the default sdk
36 stCfg, err := s.ConfigGet()
40 if len(stCfg.Devices) < 1 {
41 return fmt.Errorf("Cannot load syncthing config: no device defined")
43 devID := stCfg.Devices[0].DeviceID.String()
45 if len(stCfg.Devices) < 2 {
46 return fmt.Errorf("Cannot load syncthing config: no valid device found")
48 devID = stCfg.Devices[1].DeviceID.String()
51 for _, stFld := range stCfg.Folders {
52 cliPath := strings.TrimPrefix(stFld.Path, s.conf.FileConf.ShareRootDir)
56 *f = append(*f, xsapiv1.FolderConfig{
59 ClientPath: strings.TrimRight(cliPath, "/"),
60 Type: xsapiv1.TypeCloudSync,
61 Status: xsapiv1.StatusDisable,
62 DefaultSdk: defaultSdk,
63 RootPath: s.conf.FileConf.ShareRootDir,
64 DataCloudSync: xsapiv1.CloudSyncConfig{SyncThingID: devID},
71 // FolderChange is called when configuration has changed
72 func (s *SyncThing) FolderChange(f xsapiv1.FolderConfig) (string, error) {
75 stCfg, err := s.ConfigGet()
81 stClientID := f.DataCloudSync.SyncThingID
82 // Add new Device if needed
83 var devID protocol.DeviceID
84 if err := devID.UnmarshalText([]byte(stClientID)); err != nil {
85 s.log.Errorf("not a valid device id (err %v)", err)
89 newDevice := stconfig.DeviceConfiguration{
92 Addresses: []string{"dynamic"},
96 for _, device := range stCfg.Devices {
97 if device.DeviceID == devID {
103 stCfg.Devices = append(stCfg.Devices, newDevice)
106 // Add or update Folder settings
108 if label = f.Label; label == "" {
109 label = strings.Split(id, "/")[0]
111 if id = f.ID; id == "" {
112 id = stClientID[0:15] + "_" + label
115 folder := stconfig.FolderConfiguration{
118 Path: filepath.Join(s.conf.FileConf.ShareRootDir, f.ClientPath),
121 if s.conf.FileConf.SThgConf.RescanIntervalS > 0 {
122 folder.RescanIntervalS = s.conf.FileConf.SThgConf.RescanIntervalS
125 folder.Devices = append(folder.Devices, stconfig.FolderDeviceConfiguration{
126 DeviceID: newDevice.DeviceID,
130 var fld stconfig.FolderConfiguration
131 for _, fld = range stCfg.Folders {
132 if folder.ID == fld.ID {
139 stCfg.Folders = append(stCfg.Folders, folder)
140 fld = stCfg.Folders[0]
143 err = s.ConfigSet(stCfg)
151 // FolderDelete is called to delete a folder config
152 func (s *SyncThing) FolderDelete(id string) error {
153 // Get current config
154 stCfg, err := s.ConfigGet()
160 for i, fld := range stCfg.Folders {
162 stCfg.Folders = append(stCfg.Folders[:i], stCfg.Folders[i+1:]...)
163 err = s.ConfigSet(stCfg)
174 // FolderConfigGet Returns the configuration of a specific folder
175 func (s *SyncThing) FolderConfigGet(folderID string) (stconfig.FolderConfiguration, error) {
176 fc := stconfig.FolderConfiguration{}
178 return fc, fmt.Errorf("folderID not set")
180 cfg, err := s.ConfigGet()
184 for _, f := range cfg.Folders {
185 if f.ID == folderID {
190 return fc, fmt.Errorf("id not found")
193 // FolderStatus Returns all information about the current
194 func (s *SyncThing) FolderStatus(folderID string) (*FolderStatus, error) {
198 return nil, fmt.Errorf("folderID not set")
200 if err := s.client.HTTPGet("db/status?folder="+folderID, &data); err != nil {
203 if err := json.Unmarshal(data, &res); err != nil {
209 // IsFolderInSync Returns true when folder is in sync
210 func (s *SyncThing) IsFolderInSync(folderID string) (bool, error) {
211 sts, err := s.FolderStatus(folderID)
215 return sts.NeedBytes == 0 && sts.State == "idle", nil
218 // FolderScan Request immediate folder scan.
219 // Scan all folders if folderID param is empty
220 func (s *SyncThing) FolderScan(folderID string, subpath string) error {
223 url += "?folder=" + folderID
226 url += "&sub=" + subpath
229 return s.client.HTTPPost(url, "")