-
-
Notifications
You must be signed in to change notification settings - Fork 210
/
move.go
68 lines (60 loc) · 1.66 KB
/
move.go
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
package move
import (
"github.com/pufferpanel/pufferpanel/v3"
"github.com/pufferpanel/pufferpanel/v3/logging"
"os"
"path/filepath"
)
type Move struct {
SourceFile string
TargetFile string
}
func (m Move) Run(args pufferpanel.RunOperatorArgs) pufferpanel.OperationResult {
env := args.Environment
fs := args.Server.GetFileServer()
result, valid := validateMove(fs, m.SourceFile, m.TargetFile)
if !valid {
return pufferpanel.OperationResult{Error: nil}
}
for k, v := range result {
logging.Info.Printf("Moving file from %s to %s", k, v)
env.DisplayToConsole(true, "Moving file from %s to %s\n", k, v)
err := args.Server.GetFileServer().Rename(k, v)
if err != nil {
return pufferpanel.OperationResult{Error: err}
}
}
return pufferpanel.OperationResult{Error: nil}
}
func validateMove(fs pufferpanel.FileServer, source string, target string) (result map[string]string, valid bool) {
result = make(map[string]string)
sourceFiles, _ := filepath.Glob(source)
info, err := fs.Stat(target)
if err != nil {
if os.IsNotExist(err) && len(sourceFiles) > 1 {
logging.Error.Printf("Target folder does not exist")
valid = false
return
} else if !os.IsNotExist(err) {
valid = false
logging.Error.Printf("Error reading target file: %s", err)
return
}
} else if info.IsDir() && len(sourceFiles) > 1 {
logging.Error.Printf("Cannot move multiple files to single file target")
valid = false
return
}
if info != nil && info.IsDir() {
for _, v := range sourceFiles {
_, fileName := filepath.Split(v)
result[v] = filepath.Join(target, fileName)
}
} else {
for _, v := range sourceFiles {
result[v] = target
}
}
valid = true
return
}