mirror of
https://github.com/drakkan/sftpgo.git
synced 2025-12-07 23:00:55 +03:00
eventmanager: add support for file/directory compression
Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
This commit is contained in:
@@ -48,9 +48,9 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
supportedEventActions = []int{ActionTypeHTTP, ActionTypeCommand, ActionTypeEmail, ActionTypeBackup,
|
||||
ActionTypeUserQuotaReset, ActionTypeFolderQuotaReset, ActionTypeTransferQuotaReset,
|
||||
ActionTypeDataRetentionCheck, ActionTypeMetadataCheck, ActionTypeFilesystem}
|
||||
supportedEventActions = []int{ActionTypeHTTP, ActionTypeCommand, ActionTypeEmail, ActionTypeFilesystem,
|
||||
ActionTypeBackup, ActionTypeUserQuotaReset, ActionTypeFolderQuotaReset, ActionTypeTransferQuotaReset,
|
||||
ActionTypeDataRetentionCheck, ActionTypeMetadataCheck}
|
||||
)
|
||||
|
||||
func isActionTypeValid(action int) bool {
|
||||
@@ -123,6 +123,7 @@ const (
|
||||
FilesystemActionDelete
|
||||
FilesystemActionMkdirs
|
||||
FilesystemActionExist
|
||||
FilesystemActionCompress
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -132,7 +133,7 @@ const (
|
||||
|
||||
var (
|
||||
supportedFsActions = []int{FilesystemActionRename, FilesystemActionDelete, FilesystemActionMkdirs,
|
||||
FilesystemActionExist}
|
||||
FilesystemActionCompress, FilesystemActionExist}
|
||||
)
|
||||
|
||||
func isFilesystemActionValid(value int) bool {
|
||||
@@ -147,6 +148,8 @@ func getFsActionTypeAsString(value int) string {
|
||||
return "Delete"
|
||||
case FilesystemActionExist:
|
||||
return "Paths exist"
|
||||
case FilesystemActionCompress:
|
||||
return "Compress"
|
||||
default:
|
||||
return "Create directories"
|
||||
}
|
||||
@@ -539,6 +542,36 @@ func (c *EventActionDataRetentionConfig) validate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// EventActionFsCompress defines the configuration for the compress filesystem action
|
||||
type EventActionFsCompress struct {
|
||||
// Archive path
|
||||
Name string `json:"name,omitempty"`
|
||||
// Paths to compress
|
||||
Paths []string `json:"paths,omitempty"`
|
||||
}
|
||||
|
||||
func (c *EventActionFsCompress) validate() error {
|
||||
if c.Name == "" {
|
||||
return util.NewValidationError("archive name is mandatory")
|
||||
}
|
||||
c.Name = util.CleanPath(strings.TrimSpace(c.Name))
|
||||
if c.Name == "/" {
|
||||
return util.NewValidationError("invalid archive name")
|
||||
}
|
||||
if len(c.Paths) == 0 {
|
||||
return util.NewValidationError("no path to compress specified")
|
||||
}
|
||||
for idx, val := range c.Paths {
|
||||
val = strings.TrimSpace(val)
|
||||
if val == "" {
|
||||
return util.NewValidationError("invalid path to compress")
|
||||
}
|
||||
c.Paths[idx] = util.CleanPath(val)
|
||||
}
|
||||
c.Paths = util.RemoveDuplicates(c.Paths, false)
|
||||
return nil
|
||||
}
|
||||
|
||||
// EventActionFilesystemConfig defines the configuration for filesystem actions
|
||||
type EventActionFilesystemConfig struct {
|
||||
// Filesystem actions, see the above enum
|
||||
@@ -551,6 +584,8 @@ type EventActionFilesystemConfig struct {
|
||||
Deletes []string `json:"deletes,omitempty"`
|
||||
// file/dirs to check for existence
|
||||
Exist []string `json:"exist,omitempty"`
|
||||
// paths to compress and archive name
|
||||
Compress EventActionFsCompress `json:"compress"`
|
||||
}
|
||||
|
||||
// GetDeletesAsString returns the list of items to delete as comma separated string.
|
||||
@@ -571,6 +606,12 @@ func (c EventActionFilesystemConfig) GetExistAsString() string {
|
||||
return strings.Join(c.Exist, ",")
|
||||
}
|
||||
|
||||
// GetCompressPathsAsString returns the list of items to compress as comma separated string.
|
||||
// Using a pointer receiver will not work in web templates
|
||||
func (c EventActionFilesystemConfig) GetCompressPathsAsString() string {
|
||||
return strings.Join(c.Compress.Paths, ",")
|
||||
}
|
||||
|
||||
func (c *EventActionFilesystemConfig) validateRenames() error {
|
||||
if len(c.Renames) == 0 {
|
||||
return util.NewValidationError("no path to rename specified")
|
||||
@@ -651,6 +692,7 @@ func (c *EventActionFilesystemConfig) validate() error {
|
||||
c.MkDirs = nil
|
||||
c.Deletes = nil
|
||||
c.Exist = nil
|
||||
c.Compress = EventActionFsCompress{}
|
||||
if err := c.validateRenames(); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -658,6 +700,7 @@ func (c *EventActionFilesystemConfig) validate() error {
|
||||
c.Renames = nil
|
||||
c.MkDirs = nil
|
||||
c.Exist = nil
|
||||
c.Compress = EventActionFsCompress{}
|
||||
if err := c.validateDeletes(); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -665,6 +708,7 @@ func (c *EventActionFilesystemConfig) validate() error {
|
||||
c.Renames = nil
|
||||
c.Deletes = nil
|
||||
c.Exist = nil
|
||||
c.Compress = EventActionFsCompress{}
|
||||
if err := c.validateMkdirs(); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -672,9 +716,18 @@ func (c *EventActionFilesystemConfig) validate() error {
|
||||
c.Renames = nil
|
||||
c.Deletes = nil
|
||||
c.MkDirs = nil
|
||||
c.Compress = EventActionFsCompress{}
|
||||
if err := c.validateExist(); err != nil {
|
||||
return err
|
||||
}
|
||||
case FilesystemActionCompress:
|
||||
c.Renames = nil
|
||||
c.MkDirs = nil
|
||||
c.Deletes = nil
|
||||
c.Exist = nil
|
||||
if err := c.Compress.validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -686,6 +739,8 @@ func (c *EventActionFilesystemConfig) getACopy() EventActionFilesystemConfig {
|
||||
copy(deletes, c.Deletes)
|
||||
exist := make([]string, len(c.Exist))
|
||||
copy(exist, c.Exist)
|
||||
compressPaths := make([]string, len(c.Compress.Paths))
|
||||
copy(compressPaths, c.Compress.Paths)
|
||||
|
||||
return EventActionFilesystemConfig{
|
||||
Type: c.Type,
|
||||
@@ -693,6 +748,10 @@ func (c *EventActionFilesystemConfig) getACopy() EventActionFilesystemConfig {
|
||||
MkDirs: mkdirs,
|
||||
Deletes: deletes,
|
||||
Exist: exist,
|
||||
Compress: EventActionFsCompress{
|
||||
Paths: compressPaths,
|
||||
Name: c.Compress.Name,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user