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 return uuid.NewV1().String()[:14] + f.st.MyID[:i] + "_" + suffix
46 func (f *STFolder) Add(cfg folder.FolderConfig) (*folder.FolderConfig, error) {
49 if cfg.DataCloudSync.SyncThingID == "" {
50 return nil, fmt.Errorf("device id not set (SyncThingID field)")
53 // rootPath should not be empty
54 if cfg.RootPath == "" {
55 cfg.RootPath = f.globalConfig.FileConf.ShareRootDir
60 // Update Syncthing folder
61 // (except 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 of a directory (from server POV)
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)
107 // ConvPathCli2Svr Convert path from Client to Server
108 func (f *STFolder) ConvPathCli2Svr(s string) string {
109 if f.fConfig.ClientPath != "" && f.fConfig.RootPath != "" {
110 return strings.Replace(s,
111 f.fConfig.ClientPath,
112 f.fConfig.RootPath+"/"+f.fConfig.ClientPath,
118 // ConvPathSvr2Cli Convert path from Server to Client
119 func (f *STFolder) ConvPathSvr2Cli(s string) string {
120 if f.fConfig.ClientPath != "" && f.fConfig.RootPath != "" {
121 return strings.Replace(s,
122 f.fConfig.RootPath+"/"+f.fConfig.ClientPath,
123 f.fConfig.ClientPath,
130 func (f *STFolder) Remove() error {
131 err := f.st.FolderDelete(f.stfConfig.ID)
133 // Delete folder on server side
134 err2 := os.RemoveAll(f.GetFullPath(""))
142 // RegisterEventChange requests registration for folder event change
143 func (f *STFolder) RegisterEventChange(cb *folder.EventCB, data *folder.EventCBData) error {
145 f.eventChangeCBData = data
149 // UnRegisterEventChange remove registered callback
150 func (f *STFolder) UnRegisterEventChange() error {
151 f.eventChangeCB = nil
152 f.eventChangeCBData = nil
156 // Sync Force folder files synchronization
157 func (f *STFolder) Sync() error {
158 return f.st.FolderScan(f.stfConfig.ID, "")
161 // IsInSync Check if folder files are in-sync
162 func (f *STFolder) IsInSync() (bool, error) {
163 sts, err := f.st.IsFolderInSync(f.stfConfig.ID)
167 f.fConfig.IsInSync = sts
171 // callback use to update IsInSync status
172 func (f *STFolder) cbEventState(ev Event, data *EventsCBData) {
173 prevSync := f.fConfig.IsInSync
174 prevStatus := f.fConfig.Status
178 case EventStateChanged:
181 case "scanning", "syncing":
182 f.fConfig.Status = folder.StatusSyncing
184 f.fConfig.Status = folder.StatusEnable
186 f.fConfig.IsInSync = (to == "idle")
188 case EventFolderPaused:
189 if f.fConfig.Status == folder.StatusEnable {
190 f.fConfig.Status = folder.StatusPause
192 f.fConfig.IsInSync = false
195 if f.eventChangeCB != nil &&
196 (prevSync != f.fConfig.IsInSync || prevStatus != f.fConfig.Status) {
198 (*f.eventChangeCB)(&cpConf, f.eventChangeCBData)