-
Notifications
You must be signed in to change notification settings - Fork 114
/
upload.go
72 lines (64 loc) · 1.59 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
package filelink
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
)
const upload = "https://filelink.txthinking.com/"
func (b *fileLink) DoUpload(name string, size int64, file io.Reader) error {
body, err := b.newUpload(uploadConfig{
fileSize: size,
fileName: name,
fileReader: file,
debug: b.Config.DebugMode,
})
if err != nil {
return fmt.Errorf("upload error: %s", err)
}
//fmt.Println("Download Link: " + string(body))
b.result = "http://i.filelink.io/" + string(body)
return nil
}
func (b fileLink) PostUpload(string, int64) (string, error) {
fmt.Printf("Download Link: %s\n", b.result)
return b.result, nil
}
func (b fileLink) newUpload(config uploadConfig) ([]byte, error) {
if config.debug {
log.Printf("start upload")
}
client := http.Client{}
req, err := http.NewRequest("POST", upload, config.fileReader)
if err != nil {
return nil, err
}
req.Header.Set("content-type", "application/octet-stream")
req.Header.Set("referer", "https://filelink.io/")
req.Header.Set("origin", "https://filelink.io/")
req.Header.Set("user-agent", "Mozilla/5.0 (Macintosh;)")
req.Header.Set("x-file-name", config.fileName)
if config.debug {
log.Printf("header: %v", req.Header)
}
resp, err := client.Do(req)
if err != nil {
if config.debug {
log.Printf("do requests returns error: %v", err)
}
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
if config.debug {
log.Printf("read response returns: %v", err)
}
return nil, err
}
_ = resp.Body.Close()
if config.debug {
log.Printf("returns: %v", string(body))
}
return body, nil
}