7 "github.com/iotbzh/xds-server/lib/folder"
8 "github.com/iotbzh/xds-server/lib/xdsconfig"
9 "github.com/syncthing/syncthing/lib/config"
12 // IFOLDER interface implementation for syncthing
15 type STFolder struct {
16 globalConfig *xdsconfig.Config
18 fConfig folder.FolderConfig
19 stfConfig config.FolderConfiguration
22 // NewFolderST Create a new instance of STFolder
23 func (s *SyncThing) NewFolderST(gc *xdsconfig.Config) *STFolder {
31 func (f *STFolder) Add(cfg folder.FolderConfig) (*folder.FolderConfig, error) {
34 if cfg.DataCloudSync.SyncThingID == "" {
35 return nil, fmt.Errorf("device id not set (SyncThingID field)")
38 // rootPath should not be empty
39 if cfg.RootPath == "" {
40 cfg.RootPath = f.globalConfig.FileConf.ShareRootDir
45 f.fConfig.DataCloudSync.BuilderSThgID = f.st.MyID // FIXME - should be removed after local ST config rework
47 // Update Syncthing folder
48 // (expect if status is ErrorConfig)
49 // TODO: add cache to avoid multiple requests on startup
50 if f.fConfig.Status != folder.StatusErrorConfig {
51 id, err := f.st.FolderChange(f.fConfig)
56 f.stfConfig, err = f.st.FolderConfigGet(id)
58 f.fConfig.Status = folder.StatusErrorConfig
62 f.fConfig.Status = folder.StatusEnable
65 return &f.fConfig, nil
68 // GetConfig Get public part of folder config
69 func (f *STFolder) GetConfig() folder.FolderConfig {
73 // GetFullPath returns the full path
74 func (f *STFolder) GetFullPath(dir string) string {
78 if filepath.IsAbs(dir) {
79 return filepath.Join(f.fConfig.RootPath, dir)
81 return filepath.Join(f.fConfig.RootPath, f.fConfig.ClientPath, dir)
85 func (f *STFolder) Remove() error {
86 return f.st.FolderDelete(f.stfConfig.ID)
89 // Sync Force folder files synchronization
90 func (f *STFolder) Sync() error {
91 return f.st.FolderScan(f.stfConfig.ID, "")
94 // IsInSync Check if folder files are in-sync
95 func (f *STFolder) IsInSync() (bool, error) {
96 return f.st.IsFolderInSync(f.stfConfig.ID)