-package st
+package xdsserver
import (
"fmt"
"path/filepath"
"strings"
- "github.com/iotbzh/xds-server/lib/folder"
- "github.com/iotbzh/xds-server/lib/xdsconfig"
+ "github.com/iotbzh/xds-server/lib/xsapiv1"
+ st "github.com/iotbzh/xds-server/lib/syncthing"
uuid "github.com/satori/go.uuid"
"github.com/syncthing/syncthing/lib/config"
)
// STFolder .
type STFolder struct {
- globalConfig *xdsconfig.Config
- st *SyncThing
- fConfig folder.FolderConfig
+ *Context
+ st *st.SyncThing
+ fConfig xsapiv1.FolderConfig
stfConfig config.FolderConfiguration
eventIDs []int
- eventChangeCB *folder.EventCB
- eventChangeCBData *folder.EventCBData
+ eventChangeCB *FolderEventCB
+ eventChangeCBData *FolderEventCBData
}
// NewFolderST Create a new instance of STFolder
-func (s *SyncThing) NewFolderST(gc *xdsconfig.Config) *STFolder {
+func NewFolderST(ctx *Context, sthg *st.SyncThing) *STFolder {
return &STFolder{
- globalConfig: gc,
- st: s,
+ Context: ctx,
+ st: sthg,
}
}
}
// Add a new folder
-func (f *STFolder) Add(cfg folder.FolderConfig) (*folder.FolderConfig, error) {
+func (f *STFolder) Add(cfg xsapiv1.FolderConfig) (*xsapiv1.FolderConfig, error) {
// Sanity check
if cfg.DataCloudSync.SyncThingID == "" {
// rootPath should not be empty
if cfg.RootPath == "" {
- cfg.RootPath = f.globalConfig.FileConf.ShareRootDir
+ cfg.RootPath = f.Config.FileConf.ShareRootDir
}
f.fConfig = cfg
// Update Syncthing folder
// (except if status is ErrorConfig)
// TODO: add cache to avoid multiple requests on startup
- if f.fConfig.Status != folder.StatusErrorConfig {
+ if f.fConfig.Status != xsapiv1.StatusErrorConfig {
id, err := f.st.FolderChange(f.fConfig)
if err != nil {
return nil, err
f.stfConfig, err = f.st.FolderConfigGet(id)
if err != nil {
- f.fConfig.Status = folder.StatusErrorConfig
+ f.fConfig.Status = xsapiv1.StatusErrorConfig
return nil, err
}
// Register to events to update folder status
- for _, evName := range []string{EventStateChanged, EventFolderPaused} {
+ for _, evName := range []string{st.EventStateChanged, st.EventFolderPaused} {
evID, err := f.st.Events.Register(evName, f.cbEventState, id, nil)
if err != nil {
return nil, err
}
f.fConfig.IsInSync = false // will be updated later by events
- f.fConfig.Status = folder.StatusEnable
+ f.fConfig.Status = xsapiv1.StatusEnable
}
return &f.fConfig, nil
}
// GetConfig Get public part of folder config
-func (f *STFolder) GetConfig() folder.FolderConfig {
+func (f *STFolder) GetConfig() xsapiv1.FolderConfig {
return f.fConfig
}
}
// Update update some fields of a folder
-func (f *STFolder) Update(cfg folder.FolderConfig) (*folder.FolderConfig, error) {
+func (f *STFolder) Update(cfg xsapiv1.FolderConfig) (*xsapiv1.FolderConfig, error) {
if f.fConfig.ID != cfg.ID {
return nil, fmt.Errorf("Invalid id")
}
}
// RegisterEventChange requests registration for folder event change
-func (f *STFolder) RegisterEventChange(cb *folder.EventCB, data *folder.EventCBData) error {
+func (f *STFolder) RegisterEventChange(cb *FolderEventCB, data *FolderEventCBData) error {
f.eventChangeCB = cb
f.eventChangeCBData = data
return nil
}
// callback use to update IsInSync status
-func (f *STFolder) cbEventState(ev Event, data *EventsCBData) {
+func (f *STFolder) cbEventState(ev st.Event, data *st.EventsCBData) {
prevSync := f.fConfig.IsInSync
prevStatus := f.fConfig.Status
switch ev.Type {
- case EventStateChanged:
+ case st.EventStateChanged:
to := ev.Data["to"]
switch to {
case "scanning", "syncing":
- f.fConfig.Status = folder.StatusSyncing
+ f.fConfig.Status = xsapiv1.StatusSyncing
case "idle":
- f.fConfig.Status = folder.StatusEnable
+ f.fConfig.Status = xsapiv1.StatusEnable
}
f.fConfig.IsInSync = (to == "idle")
- case EventFolderPaused:
- if f.fConfig.Status == folder.StatusEnable {
- f.fConfig.Status = folder.StatusPause
+ case st.EventFolderPaused:
+ if f.fConfig.Status == xsapiv1.StatusEnable {
+ f.fConfig.Status = xsapiv1.StatusPause
}
f.fConfig.IsInSync = false
}