forked from mutschler/mt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.go
62 lines (54 loc) · 1.36 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
package main
import (
"bytes"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
// uploads a file via form submit to the given URL
func uploadFile(filename string) error {
if viper.GetBool("upload") && viper.GetString("upload_url") != "http://example.com/upload" {
log.Infof("uploading file...")
targetUrl := viper.GetString("upload_url")
bodyBuf := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuf)
// this step is very important
fileWriter, err := bodyWriter.CreateFormFile("image", filename)
if err != nil {
log.Errorf("error writing to buffer")
return err
}
// open file handle
fh, err := os.Open(filename)
if err != nil {
log.Errorf("error opening file: %s", err)
return err
}
//iocopy
_, err = io.Copy(fileWriter, fh)
if err != nil {
log.Errorf("error iocopy: %s", err)
return err
}
contentType := bodyWriter.FormDataContentType()
bodyWriter.Close()
resp, err := http.Post(targetUrl, contentType, bodyBuf)
if err != nil {
log.Errorf("error sending request to server: %s", err)
return err
}
defer resp.Body.Close()
resp_body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Errorf("error reading server response: %s", err)
return err
}
log.Infof("Server response:\n%s", string(resp_body))
return nil
}
return nil
}