Sane GetFile API
This commit is contained in:
parent
9abe50bebd
commit
b27494aa52
2 changed files with 29 additions and 23 deletions
|
@ -13,7 +13,7 @@ func executeClientCommand(action tg.ClientCommand, client net.Conn) {
|
|||
api.SendTextMessage(data)
|
||||
case tg.CmdGetFile:
|
||||
data := *(action.FileRequestData)
|
||||
api.GetFile(data, client, *action.Callback)
|
||||
api.GetFileNet(data, client, *action.Callback)
|
||||
case tg.CmdSendPhoto:
|
||||
data := *(action.PhotoData)
|
||||
api.SendPhoto(data)
|
||||
|
|
50
telegram.go
50
telegram.go
|
@ -377,23 +377,13 @@ func (t Telegram) AnswerInlineQuery(data InlineQueryResponse) error {
|
|||
// GetFile sends a "getFile" API call to Telegram's servers and fetches the file
|
||||
// specified afterward. The file will be then send back to the client that requested it
|
||||
// with the specified callback id.
|
||||
func (t Telegram) GetFile(data FileRequestData, client net.Conn, callback int) {
|
||||
fail := func(msg string) {
|
||||
errmsg, _ := json.Marshal(BrokerUpdate{
|
||||
Type: BError,
|
||||
Error: &msg,
|
||||
Callback: &callback,
|
||||
})
|
||||
fmt.Fprintln(client, string(errmsg))
|
||||
}
|
||||
|
||||
func (t Telegram) GetFile(data FileRequestData) ([]byte, error) {
|
||||
postdata := url.Values{
|
||||
"file_id": {data.FileID},
|
||||
}
|
||||
resp, err := http.PostForm(t.apiURL("getFile"), postdata)
|
||||
if checkerr("GetFile/post", err) {
|
||||
fail("Server didn't like my request")
|
||||
return
|
||||
return nil, fmt.Errorf("Server didn't like my request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
|
@ -403,27 +393,23 @@ func (t Telegram) GetFile(data FileRequestData, client net.Conn, callback int) {
|
|||
}{}
|
||||
err = json.NewDecoder(resp.Body).Decode(&filespecs)
|
||||
if checkerr("GetFile/json.Decode", err) {
|
||||
fail("Server sent garbage (or error)")
|
||||
return
|
||||
return nil, fmt.Errorf("Server sent garbage (or error): %w", err)
|
||||
}
|
||||
if filespecs.Result == nil {
|
||||
fail("Server didn't send a file info, does the file exist?")
|
||||
return
|
||||
return nil, fmt.Errorf("Server didn't send a file info, does the file exist?")
|
||||
}
|
||||
result := *filespecs.Result
|
||||
|
||||
path := APIEndpoint + "file/bot" + t.Token + "/" + *result.Path
|
||||
fileresp, err := http.Get(path)
|
||||
if checkerr("GetFile/get", err) {
|
||||
fail("Could not retrieve file from Telegram's servers")
|
||||
return
|
||||
return nil, fmt.Errorf("Could not retrieve file from Telegram's servers: %w", err)
|
||||
}
|
||||
defer fileresp.Body.Close()
|
||||
|
||||
rawdata, err := ioutil.ReadAll(fileresp.Body)
|
||||
if checkerr("GetFile/ioutil.ReadAll", err) {
|
||||
fail("Could not read file data")
|
||||
return
|
||||
return nil, fmt.Errorf("Could not read file data: %w", err)
|
||||
}
|
||||
|
||||
rawlen := len(rawdata)
|
||||
|
@ -431,15 +417,35 @@ func (t Telegram) GetFile(data FileRequestData, client net.Conn, callback int) {
|
|||
// ???
|
||||
log.Printf("[GetFile] WARN ?? Downloaded file does not match provided filesize: %d != %d\n", rawlen, *result.Size)
|
||||
}
|
||||
b64data := base64.StdEncoding.EncodeToString(rawdata)
|
||||
return rawdata, nil
|
||||
}
|
||||
|
||||
func (t Telegram) GetFileNet(data FileRequestData, client net.Conn, callback int) {
|
||||
byt, err := t.GetFile(data)
|
||||
if err != nil {
|
||||
errstr := err.Error()
|
||||
errmsg, _ := json.Marshal(BrokerUpdate{
|
||||
Type: BError,
|
||||
Error: &errstr,
|
||||
Callback: &callback,
|
||||
})
|
||||
fmt.Fprintln(client, string(errmsg))
|
||||
}
|
||||
|
||||
b64data := base64.StdEncoding.EncodeToString(byt)
|
||||
clientmsg, err := json.Marshal(BrokerUpdate{
|
||||
Type: BFile,
|
||||
Bytes: &b64data,
|
||||
Callback: &callback,
|
||||
})
|
||||
if checkerr("GetFile/json.Marshal", err) {
|
||||
fail("Could not serialize reply JSON")
|
||||
errstr := "Could not serialize reply JSON"
|
||||
errmsg, _ := json.Marshal(BrokerUpdate{
|
||||
Type: BError,
|
||||
Error: &errstr,
|
||||
Callback: &callback,
|
||||
})
|
||||
fmt.Fprintln(client, string(errmsg))
|
||||
return
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue