aboutsummaryrefslogtreecommitdiffstats
path: root/lib/model/folder.go
blob: fa9440999b68ec04ce0b2ab1906b2ccab32edd0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package model

import (
	"fmt"

	"github.com/iotbzh/xds-server/lib/common"
	"github.com/iotbzh/xds-server/lib/syncthing"
	"github.com/iotbzh/xds-server/lib/xdsconfig"
)

// Folder Represent a an XDS folder
type Folder struct {
	Conf *xdsconfig.Config
	SThg *st.SyncThing
}

// NewFolder Create a new instance of Model Folder
func NewFolder(cfg *xdsconfig.Config, st *st.SyncThing) *Folder {
	return &Folder{
		Conf: cfg,
		SThg: st,
	}
}

// GetFolderFromID retrieves the Folder config from id
func (c *Folder) GetFolderFromID(id string) *xdsconfig.FolderConfig {
	if idx := c.Conf.Folders.GetIdx(id); idx != -1 {
		return &c.Conf.Folders[idx]
	}
	return nil
}

// UpdateAll updates all the current configuration
func (c *Folder) UpdateAll(newCfg xdsconfig.Config) error {
	return fmt.Errorf("Not Supported")
	/*
		if err := VerifyConfig(newCfg); err != nil {
			return err
		}

		// TODO: c.Builder = c.Builder.Update(newCfg.Builder)
		c.Folders = c.Folders.Update(newCfg.Folders)

		// FIXME To be tested & improved error handling
		for _, f := range c.Folders {
			if err := c.SThg.FolderChange(st.FolderChangeArg{
				ID:           f.ID,
				Label:        f.Label,
				RelativePath: f.RelativePath,
				SyncThingID:  f.SyncThingID,
				ShareRootDir: c.ShareRootDir,
			}); err != nil {
				return err
			}
		}

		return nil
	*/
}

// UpdateFolder updates a specific folder into the current configuration
func (c *Folder) UpdateFolder(newFolder xdsconfig.FolderConfig) (xdsconfig.FolderConfig, error) {
	// rootPath should not be empty
	if newFolder.RootPath == "" {
		newFolder.RootPath = c.Conf.ShareRootDir
	}

	// Sanity check of folder settings
	if err := newFolder.Verify(); err != nil {
		return xdsconfig.FolderConfig{}, err
	}

	// Normalize path (needed for Windows path including bashlashes)
	newFolder.RelativePath = common.PathNormalize(newFolder.RelativePath)

	// Update config folder
	c.Conf.Folders = c.Conf.Folders.Update(xdsconfig.FoldersConfig{newFolder})

	// Update Syncthing folder
	err := c.SThg.FolderChange(newFolder)

	newFolder.BuilderSThgID = c.Conf.Builder.SyncThingID // FIXME - should be removed after local ST config rework
	newFolder.Status = xdsconfig.FolderStatusEnable

	return newFolder, err
}

// DeleteFolder deletes a specific folder
func (c *Folder) DeleteFolder(id string) (xdsconfig.FolderConfig, error) {
	var fld xdsconfig.FolderConfig
	var err error

	if err = c.SThg.FolderDelete(id); err != nil {
		return fld, err
	}

	c.Conf.Folders, fld, err = c.Conf.Folders.Delete(id)

	return fld, err
}

// ForceSync Force the synchronization of a folder
func (c *Folder) ForceSync(id string) error {
	return c.SThg.FolderScan(id, "")
}

// IsFolderInSync Returns true when folder is in sync
func (c *Folder) IsFolderInSync(id string) (bool, error) {
	return c.SThg.IsFolderInSync(id)
}