-
Notifications
You must be signed in to change notification settings - Fork 0
/
transfer_webdav.go
executable file
·85 lines (73 loc) · 2.15 KB
/
transfer_webdav.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
package main
import (
"errors"
"fmt"
"github.com/StarmanMartin/gowebdav"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
)
// TransferManagerWebdav reacts on the channel done_files.
// If folder of file is ready to send it sends it via WebDAV (HTTP) to <CMD arg -dst>.
// It also initializes the zipping if <CMD arg -zip> is set.
type TransferManagerWebdav struct {
args *Args
client *gowebdav.Client
}
// doWork runs in a endless loop. It reacts on the channel done_files.
// If folder of file is ready to send it sends it via HWebDAV (HTTP) to <CMD arg -dst>.
// It also initializes the zipping if <CMD arg -zip> is set
// It terminates as soon as a value is pushed into quit. Run in extra goroutine.
func (m *TransferManagerWebdav) doWork(quit chan int) {
doWorkImplementation(quit, m, m.args)
}
func (m *TransferManagerWebdav) connect_to_server() error {
user := m.args.user
password := m.args.pass
c := gowebdav.NewClient(m.args.dst.String(), user, password, tr)
c.SetTimeout(10 * time.Second)
if err := c.Connect(); err != nil {
return err
}
m.client = c
return nil
}
// send_file sends a file via WebDAV
func (m *TransferManagerWebdav) send_file(path_to_file string, file os.FileInfo) (bool, error) {
var webdavFilePath, urlPathDir string
err := m.connect_to_server()
if err != nil {
return false, err
}
if m.args.sendType == "file" {
urlPathDir = "."
webdavFilePath = file.Name()
} else if relpath, err := filepath.Rel(TempPath, path_to_file); err == nil {
webdavFilePath = strings.Replace(relpath, string(os.PathSeparator), "/", -1)
webdavFilePath = strings.TrimPrefix(webdavFilePath, "./")
urlPathDir = filepath.Dir(webdavFilePath)
} else {
return false, err
}
InfoLogger.Println("Sending...", webdavFilePath)
if urlPathDir != "." {
err := m.client.MkdirAll(urlPathDir, 0644)
if err != nil {
return false, err
}
}
bytes, err := ioutil.ReadFile(path_to_file)
if err != nil {
return false, err
}
defer func() {
if r := recover(); r != nil {
err = errors.New(fmt.Sprintf("%+v", r))
ErrorLogger.Printf("WebDav Panic: %s\n", err)
}
}()
err = m.client.Write(webdavFilePath, bytes, 0644)
return true, err
}