9 "github.com/iotbzh/xds-server/lib/xsapiv1"
10 st "github.com/iotbzh/xds-server/lib/syncthing"
11 uuid "github.com/satori/go.uuid"
12 "github.com/syncthing/syncthing/lib/config"
15 // IFOLDER interface implementation for syncthing
18 type STFolder struct {
21 fConfig xsapiv1.FolderConfig
22 stfConfig config.FolderConfiguration
24 eventChangeCB *FolderEventCB
25 eventChangeCBData *FolderEventCBData
28 // NewFolderST Create a new instance of STFolder
29 func NewFolderST(ctx *Context, sthg *st.SyncThing) *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 xsapiv1.FolderConfig) (*xsapiv1.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.Config.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 != xsapiv1.StatusErrorConfig {
68 id, err := f.st.FolderChange(f.fConfig)
73 f.stfConfig, err = f.st.FolderConfigGet(id)
75 f.fConfig.Status = xsapiv1.StatusErrorConfig
79 // Register to events to update folder status
80 for _, evName := range []string{st.EventStateChanged, st.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 = xsapiv1.StatusEnable
92 return &f.fConfig, nil
95 // GetConfig Get public part of folder config
96 func (f *STFolder) GetConfig() xsapiv1.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 // Update update some fields of a folder
147 func (f *STFolder) Update(cfg xsapiv1.FolderConfig) (*xsapiv1.FolderConfig, error) {
148 if f.fConfig.ID != cfg.ID {
149 return nil, fmt.Errorf("Invalid id")
152 return &f.fConfig, nil
155 // RegisterEventChange requests registration for folder event change
156 func (f *STFolder) RegisterEventChange(cb *FolderEventCB, data *FolderEventCBData) error {
158 f.eventChangeCBData = data
162 // UnRegisterEventChange remove registered callback
163 func (f *STFolder) UnRegisterEventChange() error {
164 f.eventChangeCB = nil
165 f.eventChangeCBData = nil
169 // Sync Force folder files synchronization
170 func (f *STFolder) Sync() error {
171 return f.st.FolderScan(f.stfConfig.ID, "")
174 // IsInSync Check if folder files are in-sync
175 func (f *STFolder) IsInSync() (bool, error) {
176 sts, err := f.st.IsFolderInSync(f.stfConfig.ID)
180 f.fConfig.IsInSync = sts
184 // callback use to update IsInSync status
185 func (f *STFolder) cbEventState(ev st.Event, data *st.EventsCBData) {
186 prevSync := f.fConfig.IsInSync
187 prevStatus := f.fConfig.Status
191 case st.EventStateChanged:
194 case "scanning", "syncing":
195 f.fConfig.Status = xsapiv1.StatusSyncing
197 f.fConfig.Status = xsapiv1.StatusEnable
199 f.fConfig.IsInSync = (to == "idle")
201 case st.EventFolderPaused:
202 if f.fConfig.Status == xsapiv1.StatusEnable {
203 f.fConfig.Status = xsapiv1.StatusPause
205 f.fConfig.IsInSync = false
208 if f.eventChangeCB != nil &&
209 (prevSync != f.fConfig.IsInSync || prevStatus != f.fConfig.Status) {
211 (*f.eventChangeCB)(&cpConf, f.eventChangeCBData)