/
sftp.go
164 lines (141 loc) · 2.84 KB
/
sftp.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package sftpfilesystem
import (
"fmt"
"io"
"log"
"os"
"path"
"strings"
"github.com/pkg/sftp"
"github.com/wtran29/fenix/fenix/cmd/filesystems"
"golang.org/x/crypto/ssh"
)
type SFTP struct {
Host string
User string
Pass string
Port string
}
func (s *SFTP) getCredentials() (*sftp.Client, error) {
addr := fmt.Sprintf("%s:%s", s.Host, s.Port)
config := &ssh.ClientConfig{
User: s.User,
Auth: []ssh.AuthMethod{
ssh.Password(s.Pass),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
conn, err := ssh.Dial("tcp", addr, config)
if err != nil {
return nil, err
}
client, err := sftp.NewClient(conn)
if err != nil {
return nil, err
}
cwd, err := client.Getwd()
if err != nil {
return nil, err
}
log.Println("Current working directory:", cwd)
return client, nil
}
func (s *SFTP) Put(filename, folder string) error {
client, err := s.getCredentials()
if err != nil {
return err
}
defer client.Close()
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
f2, err := client.Create(fmt.Sprintf("%s/%s", folder, path.Base(filename)))
if err != nil {
return err
}
defer f2.Close()
if _, err := io.Copy(f2, f); err != nil {
return err
}
return nil
}
func (s *SFTP) List(prefix string) ([]filesystems.Listing, error) {
var listing []filesystems.Listing
client, err := s.getCredentials()
if err != nil {
return listing, err
}
defer client.Close()
files, err := client.ReadDir(prefix)
if err != nil {
return listing, err
}
for _, file := range files {
var item filesystems.Listing
if !strings.HasPrefix(file.Name(), ".") {
b := float64(file.Size())
kb := b / 1024
mb := kb / 1024
item.Key = file.Name()
item.Size = mb
item.LastModified = file.ModTime()
item.IsDir = file.IsDir()
listing = append(listing, item)
}
}
return listing, nil
}
func (s *SFTP) Delete(itemsToDel []string) bool {
client, err := s.getCredentials()
if err != nil {
return false
}
defer client.Close()
for _, item := range itemsToDel {
err := client.Remove(item)
if err != nil {
return false
}
}
return true
}
func (s *SFTP) Get(destination string, items ...string) error {
client, err := s.getCredentials()
if err != nil {
return err
}
defer client.Close()
for _, item := range items {
err := func() error {
// create destination file
dstFile, err := os.Create(fmt.Sprintf("%s/%s", destination, path.Base(item)))
if err != nil {
return err
}
defer dstFile.Close()
// open source file
srcFile, err := client.Open(item)
if err != nil {
return err
}
defer srcFile.Close()
// copy src to dst
_, err = io.Copy(dstFile, srcFile)
if err != nil {
return err
}
// flush the in-memory copy
err = dstFile.Sync()
if err != nil {
return err
}
return nil
}()
if err != nil {
return err
}
}
return nil
}