9 "github.com/iotbzh/xds-server/lib/folder"
10 "github.com/iotbzh/xds-server/lib/xdsconfig"
11 uuid "github.com/satori/go.uuid"
12 "github.com/syncthing/syncthing/lib/config"
15 // IFOLDER interface implementation for syncthing
18 type STFolder struct {
19 globalConfig *xdsconfig.Config
21 fConfig folder.FolderConfig
22 stfConfig config.FolderConfiguration
24 eventChangeCB *folder.EventCB
25 eventChangeCBData *folder.EventCBData
28 // NewFolderST Create a new instance of STFolder
29 func (s *SyncThing) NewFolderST(gc *xdsconfig.Config) *STFolder {
37 func (f *STFolder) NewUID(suffix string) string {
42 uuid := uuid.NewV1().String()[:14] + f.st.MyID[:i]
50 func (f *STFolder) Add(cfg folder.FolderConfig) (*folder.FolderConfig, error) {
53 if cfg.DataCloudSync.SyncThingID == "" {
54 return nil, fmt.Errorf("device id not set (SyncThingID field)")
57 // rootPath should not be empty
58 if cfg.RootPath == "" {
59 cfg.RootPath = f.globalConfig.FileConf.ShareRootDir
64 // Update Syncthing folder
65 // (except if status is ErrorConfig)
66 // TODO: add cache to avoid multiple requests on startup
67 if f.fConfig.Status != folder.StatusErrorConfig {
68 id, err := f.st.FolderChange(f.fConfig)
73 f.stfConfig, err = f.st.FolderConfigGet(id)
75 f.fConfig.Status = folder.StatusErrorConfig
79 // Register to events to update folder status
80 for _, evName := range []string{EventStateChanged, EventFolderPaused} {
81 evID, err := f.st.Events.Register(evName, f.cbEventState, id, nil)
85 f.eventIDs = append(f.eventIDs, evID)
88 f.fConfig.IsInSync = false // will be updated later by events
89 f.fConfig.Status = folder.StatusEnable
92 return &f.fConfig, nil
95 // GetConfig Get public part of folder config
96 func (f *STFolder) GetConfig() folder.FolderConfig {
100 // GetFullPath returns the full path of a directory (from server POV)
101 func (f *STFolder) GetFullPath(dir string) string {
105 if filepath.IsAbs(dir) {
106 return filepath.Join(f.fConfig.RootPath, dir)
108 return filepath.Join(f.fConfig.RootPath, f.fConfig.ClientPath, dir)
111 // ConvPathCli2Svr Convert path from Client to Server
112 func (f *STFolder) ConvPathCli2Svr(s string) string {
113 if f.fConfig.ClientPath != "" && f.fConfig.RootPath != "" {
114 return strings.Replace(s,
115 f.fConfig.ClientPath,
116 f.fConfig.RootPath+"/"+f.fConfig.ClientPath,
122 // ConvPathSvr2Cli Convert path from Server to Client
123 func (f *STFolder) ConvPathSvr2Cli(s string) string {
124 if f.fConfig.ClientPath != "" && f.fConfig.RootPath != "" {
125 return strings.Replace(s,
126 f.fConfig.RootPath+"/"+f.fConfig.ClientPath,
127 f.fConfig.ClientPath,
134 func (f *STFolder) Remove() error {
135 err := f.st.FolderDelete(f.stfConfig.ID)
137 // Delete folder on server side
138 err2 := os.RemoveAll(f.GetFullPath(""))
146 // RegisterEventChange requests registration for folder event change
147 func (f *STFolder) RegisterEventChange(cb *folder.EventCB, data *folder.EventCBData) error {
149 f.eventChangeCBData = data
153 // UnRegisterEventChange remove registered callback
154 func (f *STFolder) UnRegisterEventChange() error {
155 f.eventChangeCB = nil
156 f.eventChangeCBData = nil
160 // Sync Force folder files synchronization
161 func (f *STFolder) Sync() error {
162 return f.st.FolderScan(f.stfConfig.ID, "")
165 // IsInSync Check if folder files are in-sync
166 func (f *STFolder) IsInSync() (bool, error) {
167 sts, err := f.st.IsFolderInSync(f.stfConfig.ID)
171 f.fConfig.IsInSync = sts
175 // callback use to update IsInSync status
176 func (f *STFolder) cbEventState(ev Event, data *EventsCBData) {
177 prevSync := f.fConfig.IsInSync
178 prevStatus := f.fConfig.Status
182 case EventStateChanged:
185 case "scanning", "syncing":
186 f.fConfig.Status = folder.StatusSyncing
188 f.fConfig.Status = folder.StatusEnable
190 f.fConfig.IsInSync = (to == "idle")
192 case EventFolderPaused:
193 if f.fConfig.Status == folder.StatusEnable {
194 f.fConfig.Status = folder.StatusPause
196 f.fConfig.IsInSync = false
199 if f.eventChangeCB != nil &&
200 (prevSync != f.fConfig.IsInSync || prevStatus != f.fConfig.Status) {
202 (*f.eventChangeCB)(&cpConf, f.eventChangeCBData)