Add folder update support and ClientData field.
[src/xds/xds-server.git] / lib / syncthing / folder-st.go
index da27062..74aa4bb 100644 (file)
@@ -2,7 +2,9 @@ package st
 
 import (
        "fmt"
+       "os"
        "path/filepath"
+       "strings"
 
        "github.com/iotbzh/xds-server/lib/folder"
        "github.com/iotbzh/xds-server/lib/xdsconfig"
@@ -37,7 +39,11 @@ func (f *STFolder) NewUID(suffix string) string {
        if i > 15 {
                i = 15
        }
-       return uuid.NewV1().String()[:14] + f.st.MyID[:i] + "_" + suffix
+       uuid := uuid.NewV1().String()[:14] + f.st.MyID[:i]
+       if len(suffix) > 0 {
+               uuid += "_" + suffix
+       }
+       return uuid
 }
 
 // Add a new folder
@@ -55,10 +61,8 @@ func (f *STFolder) Add(cfg folder.FolderConfig) (*folder.FolderConfig, error) {
 
        f.fConfig = cfg
 
-       f.fConfig.DataCloudSync.BuilderSThgID = f.st.MyID // FIXME - should be removed after local ST config rework
-
        // Update Syncthing folder
-       // (expect if status is ErrorConfig)
+       // (except if status is ErrorConfig)
        // TODO: add cache to avoid multiple requests on startup
        if f.fConfig.Status != folder.StatusErrorConfig {
                id, err := f.st.FolderChange(f.fConfig)
@@ -93,7 +97,7 @@ func (f *STFolder) GetConfig() folder.FolderConfig {
        return f.fConfig
 }
 
-// GetFullPath returns the full path
+// GetFullPath returns the full path of a directory (from server POV)
 func (f *STFolder) GetFullPath(dir string) string {
        if &dir == nil {
                dir = ""
@@ -104,9 +108,48 @@ func (f *STFolder) GetFullPath(dir string) string {
        return filepath.Join(f.fConfig.RootPath, f.fConfig.ClientPath, dir)
 }
 
+// ConvPathCli2Svr Convert path from Client to Server
+func (f *STFolder) ConvPathCli2Svr(s string) string {
+       if f.fConfig.ClientPath != "" && f.fConfig.RootPath != "" {
+               return strings.Replace(s,
+                       f.fConfig.ClientPath,
+                       f.fConfig.RootPath+"/"+f.fConfig.ClientPath,
+                       -1)
+       }
+       return s
+}
+
+// ConvPathSvr2Cli Convert path from Server to Client
+func (f *STFolder) ConvPathSvr2Cli(s string) string {
+       if f.fConfig.ClientPath != "" && f.fConfig.RootPath != "" {
+               return strings.Replace(s,
+                       f.fConfig.RootPath+"/"+f.fConfig.ClientPath,
+                       f.fConfig.ClientPath,
+                       -1)
+       }
+       return s
+}
+
 // Remove a folder
 func (f *STFolder) Remove() error {
-       return f.st.FolderDelete(f.stfConfig.ID)
+       err := f.st.FolderDelete(f.stfConfig.ID)
+
+       // Delete folder on server side
+       err2 := os.RemoveAll(f.GetFullPath(""))
+
+       if err != nil {
+               return err
+       }
+       return err2
+}
+
+// Update update some fields of a folder
+func (f *STFolder) Update(cfg folder.FolderConfig) (*folder.FolderConfig, error) {
+       if f.fConfig.ID != cfg.ID {
+               return nil, fmt.Errorf("Invalid id")
+       }
+       f.fConfig = cfg
+       return &f.fConfig, nil
 }
 
 // RegisterEventChange requests registration for folder event change