forked from 26000/irchuu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
komf.go
162 lines (141 loc) · 3.72 KB
/
komf.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
package upload
import (
"bytes"
"github.com/26000/irchuu/config"
"github.com/26000/irchuu/paths"
"gopkg.in/telegram-bot-api.v4"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
"path"
"strings"
)
// Komf uploads a Telegram media file to a komf hosting. It doesn't check
// if the file was already uploaded, that is handeled by pomfs.
func Komf(bot *tgbotapi.BotAPI, id string, c *config.Telegram) (url string, err error) {
file, err := bot.GetFileDirectURL(id)
if err != nil {
return
}
fileStrings := strings.Split(file, "/")
fileName := strings.Split(fileStrings[len(fileStrings)-1], ".")
var ext string
if len(fileName) > 1 {
ext = "." + fileName[len(fileName)-1]
}
localUrl := path.Join(c.DataDir, id+ext)
// if it is already downloaded, just upload the local copy
if paths.Exists(localUrl) {
return uploadLocalFileKomf(localUrl, c)
}
return uploadRemoteFileKomf(file, localUrl, id, fileStrings[len(fileStrings)-1], c)
}
// uploadLocalFileKomf actually uploads the file to a komf using HTTP POST with
// multipart/form-data mime. It also reads the whole file to memory because of
// the current implementation of Go's multipart.
func uploadLocalFileKomf(file string, c *config.Telegram) (url string, err error) {
f, err := os.Open(file)
if err != nil {
return
}
defer f.Close()
var b bytes.Buffer
w := multipart.NewWriter(&b)
ff, err := w.CreateFormFile("file", f.Name())
if err != nil {
return
}
// i know, copying it to RAM was never a good idea...
_, err = io.Copy(ff, f)
if err != nil {
return
}
err = w.WriteField("date", c.KomfDate)
if err != nil {
return
}
w.Close()
r, err := http.Post(makeKomfUrl(c.Komf), w.FormDataContentType(), &b)
if err != nil {
return
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return
}
url = makeKomfDownloadUrl(c.KomfPublicURL, string(body))
return
}
// uploadRemoteFileKomf downloads a file from Telegram and uploads it to a
// komf using HTTP POST with multipart/form-data mime. It also reads the
// whole file to memory because of the current implementation of Go's multipart.
func uploadRemoteFileKomf(file string, localUrl string, id string, name string, c *config.Telegram) (url string, err error) {
downloadable, err := http.Get(file)
defer downloadable.Body.Close()
var b bytes.Buffer
w := multipart.NewWriter(&b)
ff, err := w.CreateFormFile("file", name) // create the multipart file
if err != nil {
return
}
if c.DownloadMedia {
// if we need to both upload and download it, we use io.MultiWriter
res, err := os.Create(localUrl)
if err != nil {
return "", err
}
writer := io.MultiWriter(res, ff)
// i know, copying it to RAM was never a good idea...
_, err = io.Copy(writer, downloadable.Body)
res.Close()
if err != nil {
return "", err
}
} else {
// just io.Copy otherwise
// i know, copying it to RAM was never a good idea...
_, err = io.Copy(ff, downloadable.Body)
if err != nil {
return
}
}
err = w.WriteField("date", c.KomfDate)
if err != nil {
return
}
w.Close()
r, err := http.Post(makeKomfUrl(c.Komf), w.FormDataContentType(), &b)
if err != nil {
return
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return
}
url = makeKomfDownloadUrl(c.KomfPublicURL, string(body))
return
}
// makePomfUrl just appends "upload" to a komf site link.
func makeKomfUrl(komf string) string {
if len(komf) < 2 {
return ""
}
if komf[len(komf)-1] == '/' {
return komf + "upload"
}
return komf + "/upload"
}
// makeKomfDownloadUrl appends a file path to a komf site link.
func makeKomfDownloadUrl(komf string, file string) string {
if len(komf) < 2 {
return ""
}
if komf[len(komf)-1] == '/' {
return komf + file[1:]
}
return komf + file
}