8 "github.com/iotbzh/xds-server/lib/folder"
9 "github.com/iotbzh/xds-server/lib/xdsconfig"
10 uuid "github.com/satori/go.uuid"
11 "github.com/syncthing/syncthing/lib/config"
14 // IFOLDER interface implementation for syncthing
17 type STFolder struct {
18 globalConfig *xdsconfig.Config
20 fConfig folder.FolderConfig
21 stfConfig config.FolderConfiguration
23 eventChangeCB *folder.EventCB
24 eventChangeCBData *folder.EventCBData
27 // NewFolderST Create a new instance of STFolder
28 func (s *SyncThing) NewFolderST(gc *xdsconfig.Config) *STFolder {
36 func (f *STFolder) NewUID(suffix string) string {
41 return uuid.NewV1().String()[:14] + f.st.MyID[:i] + "_" + suffix
45 func (f *STFolder) Add(cfg folder.FolderConfig) (*folder.FolderConfig, error) {
48 if cfg.DataCloudSync.SyncThingID == "" {
49 return nil, fmt.Errorf("device id not set (SyncThingID field)")
52 // rootPath should not be empty
53 if cfg.RootPath == "" {
54 cfg.RootPath = f.globalConfig.FileConf.ShareRootDir
59 f.fConfig.DataCloudSync.BuilderSThgID = f.st.MyID // FIXME - should be removed after local ST config rework
61 // Update Syncthing folder
62 // (expect if status is ErrorConfig)
63 // TODO: add cache to avoid multiple requests on startup
64 if f.fConfig.Status != folder.StatusErrorConfig {
65 id, err := f.st.FolderChange(f.fConfig)
70 f.stfConfig, err = f.st.FolderConfigGet(id)
72 f.fConfig.Status = folder.StatusErrorConfig
76 // Register to events to update folder status
77 for _, evName := range []string{EventStateChanged, EventFolderPaused} {
78 evID, err := f.st.Events.Register(evName, f.cbEventState, id, nil)
82 f.eventIDs = append(f.eventIDs, evID)
85 f.fConfig.IsInSync = false // will be updated later by events
86 f.fConfig.Status = folder.StatusEnable
89 return &f.fConfig, nil
92 // GetConfig Get public part of folder config
93 func (f *STFolder) GetConfig() folder.FolderConfig {
97 // GetFullPath returns the full path of a directory (from server POV)
98 func (f *STFolder) GetFullPath(dir string) string {
102 if filepath.IsAbs(dir) {
103 return filepath.Join(f.fConfig.RootPath, dir)
105 return filepath.Join(f.fConfig.RootPath, f.fConfig.ClientPath, dir)
108 // ConvPathCli2Svr Convert path from Client to Server
109 func (f *STFolder) ConvPathCli2Svr(s string) string {
110 if f.fConfig.ClientPath != "" && f.fConfig.RootPath != "" {
111 return strings.Replace(s,
112 f.fConfig.ClientPath,
113 f.fConfig.RootPath+"/"+f.fConfig.ClientPath,
119 // ConvPathSvr2Cli Convert path from Server to Client
120 func (f *STFolder) ConvPathSvr2Cli(s string) string {
121 if f.fConfig.ClientPath != "" && f.fConfig.RootPath != "" {
122 return strings.Replace(s,
123 f.fConfig.RootPath+"/"+f.fConfig.ClientPath,
124 f.fConfig.ClientPath,
131 func (f *STFolder) Remove() error {
132 return f.st.FolderDelete(f.stfConfig.ID)
135 // RegisterEventChange requests registration for folder event change
136 func (f *STFolder) RegisterEventChange(cb *folder.EventCB, data *folder.EventCBData) error {
138 f.eventChangeCBData = data
142 // UnRegisterEventChange remove registered callback
143 func (f *STFolder) UnRegisterEventChange() error {
144 f.eventChangeCB = nil
145 f.eventChangeCBData = nil
149 // Sync Force folder files synchronization
150 func (f *STFolder) Sync() error {
151 return f.st.FolderScan(f.stfConfig.ID, "")
154 // IsInSync Check if folder files are in-sync
155 func (f *STFolder) IsInSync() (bool, error) {
156 sts, err := f.st.IsFolderInSync(f.stfConfig.ID)
160 f.fConfig.IsInSync = sts
164 // callback use to update IsInSync status
165 func (f *STFolder) cbEventState(ev Event, data *EventsCBData) {
166 prevSync := f.fConfig.IsInSync
167 prevStatus := f.fConfig.Status
171 case EventStateChanged:
174 case "scanning", "syncing":
175 f.fConfig.Status = folder.StatusSyncing
177 f.fConfig.Status = folder.StatusEnable
179 f.fConfig.IsInSync = (to == "idle")
181 case EventFolderPaused:
182 if f.fConfig.Status == folder.StatusEnable {
183 f.fConfig.Status = folder.StatusPause
185 f.fConfig.IsInSync = false
188 if f.eventChangeCB != nil &&
189 (prevSync != f.fConfig.IsInSync || prevStatus != f.fConfig.Status) {
191 (*f.eventChangeCB)(&cpConf, f.eventChangeCBData)