-
Notifications
You must be signed in to change notification settings - Fork 99
/
upload.go
99 lines (88 loc) · 1.98 KB
/
upload.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
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
// Package goftp upload helper
package goftp
import (
"os"
"path/filepath"
)
func (ftp *FTP) copyDir(localPath string) error {
fullPath, err := filepath.Abs(localPath)
if err != nil {
return err
}
pwd, err := ftp.Pwd()
if err != nil {
return err
}
walkFunc := func(path string, fi os.FileInfo, err error) error {
// Stop upon error
if err != nil {
return err
}
relPath, err := filepath.Rel(fullPath, path)
if err != nil {
return err
}
switch {
case fi.IsDir():
// Walk calls walkFn on root as well
if path == fullPath {
return nil
}
if err = ftp.Mkd(relPath); err != nil {
if _, err = ftp.List(relPath + "/"); err != nil {
return err
}
}
case fi.Mode()&os.ModeSymlink == os.ModeSymlink:
fInfo, err := os.Stat(path)
if err != nil {
return err
}
if fInfo.IsDir() {
err = ftp.Mkd(relPath)
return err
} else if fInfo.Mode()&os.ModeType != 0 {
// ignore other special files
return nil
}
fallthrough
case fi.Mode()&os.ModeType == 0:
if err = ftp.copyFile(path, pwd+"/"+relPath); err != nil {
return err
}
default:
// Ignore other special files
}
return nil
}
return filepath.Walk(fullPath, walkFunc)
}
func (ftp *FTP) copyFile(localPath, serverPath string) (err error) {
var file *os.File
if file, err = os.Open(localPath); err != nil {
return err
}
defer file.Close()
if err := ftp.Stor(serverPath, file); err != nil {
return err
}
return nil
}
// Upload a file, or recursively upload a directory.
// Only normal files and directories are uploaded.
// Symlinks are not kept but treated as normal files/directories if targets are so.
func (ftp *FTP) Upload(localPath string) (err error) {
fInfo, err := os.Stat(localPath)
if err != nil {
return err
}
switch {
case fInfo.IsDir():
return ftp.copyDir(localPath)
case fInfo.Mode()&os.ModeType == 0:
return ftp.copyFile(localPath, filepath.Base(localPath))
default:
// Ignore other special files
}
return nil
}