7 "github.com/iotbzh/xds-server/lib/folder"
8 "github.com/iotbzh/xds-server/lib/xdsconfig"
9 uuid "github.com/satori/go.uuid"
10 "github.com/syncthing/syncthing/lib/config"
13 // IFOLDER interface implementation for syncthing
16 type STFolder struct {
17 globalConfig *xdsconfig.Config
19 fConfig folder.FolderConfig
20 stfConfig config.FolderConfiguration
22 eventChangeCB *folder.EventCB
23 eventChangeCBData *folder.EventCBData
26 // NewFolderST Create a new instance of STFolder
27 func (s *SyncThing) NewFolderST(gc *xdsconfig.Config) *STFolder {
35 func (f *STFolder) NewUID(suffix string) string {
40 return uuid.NewV1().String()[:14] + f.st.MyID[:i] + "_" + suffix
44 func (f *STFolder) Add(cfg folder.FolderConfig) (*folder.FolderConfig, error) {
47 if cfg.DataCloudSync.SyncThingID == "" {
48 return nil, fmt.Errorf("device id not set (SyncThingID field)")
51 // rootPath should not be empty
52 if cfg.RootPath == "" {
53 cfg.RootPath = f.globalConfig.FileConf.ShareRootDir
58 f.fConfig.DataCloudSync.BuilderSThgID = f.st.MyID // FIXME - should be removed after local ST config rework
60 // Update Syncthing folder
61 // (expect if status is ErrorConfig)
62 // TODO: add cache to avoid multiple requests on startup
63 if f.fConfig.Status != folder.StatusErrorConfig {
64 id, err := f.st.FolderChange(f.fConfig)
69 f.stfConfig, err = f.st.FolderConfigGet(id)
71 f.fConfig.Status = folder.StatusErrorConfig
75 // Register to events to update folder status
76 for _, evName := range []string{EventStateChanged, EventFolderPaused} {
77 evID, err := f.st.Events.Register(evName, f.cbEventState, id, nil)
81 f.eventIDs = append(f.eventIDs, evID)
84 f.fConfig.IsInSync = false // will be updated later by events
85 f.fConfig.Status = folder.StatusEnable
88 return &f.fConfig, nil
91 // GetConfig Get public part of folder config
92 func (f *STFolder) GetConfig() folder.FolderConfig {
96 // GetFullPath returns the full path
97 func (f *STFolder) GetFullPath(dir string) string {
101 if filepath.IsAbs(dir) {
102 return filepath.Join(f.fConfig.RootPath, dir)
104 return filepath.Join(f.fConfig.RootPath, f.fConfig.ClientPath, dir)
108 func (f *STFolder) Remove() error {
109 return f.st.FolderDelete(f.stfConfig.ID)
112 // RegisterEventChange requests registration for folder event change
113 func (f *STFolder) RegisterEventChange(cb *folder.EventCB, data *folder.EventCBData) error {
115 f.eventChangeCBData = data
119 // UnRegisterEventChange remove registered callback
120 func (f *STFolder) UnRegisterEventChange() error {
121 f.eventChangeCB = nil
122 f.eventChangeCBData = nil
126 // Sync Force folder files synchronization
127 func (f *STFolder) Sync() error {
128 return f.st.FolderScan(f.stfConfig.ID, "")
131 // IsInSync Check if folder files are in-sync
132 func (f *STFolder) IsInSync() (bool, error) {
133 sts, err := f.st.IsFolderInSync(f.stfConfig.ID)
137 f.fConfig.IsInSync = sts
141 // callback use to update IsInSync status
142 func (f *STFolder) cbEventState(ev Event, data *EventsCBData) {
143 prevSync := f.fConfig.IsInSync
144 prevStatus := f.fConfig.Status
148 case EventStateChanged:
151 case "scanning", "syncing":
152 f.fConfig.Status = folder.StatusSyncing
154 f.fConfig.Status = folder.StatusEnable
156 f.fConfig.IsInSync = (to == "idle")
158 case EventFolderPaused:
159 if f.fConfig.Status == folder.StatusEnable {
160 f.fConfig.Status = folder.StatusPause
162 f.fConfig.IsInSync = false
165 if f.eventChangeCB != nil &&
166 (prevSync != f.fConfig.IsInSync || prevStatus != f.fConfig.Status) {
168 (*f.eventChangeCB)(&cpConf, f.eventChangeCBData)