Sane GetFile API

This commit is contained in:
Hamcha 2022-03-27 21:58:52 +02:00
parent 9abe50bebd
commit b27494aa52
Signed by: hamcha
GPG key ID: 1669C533B8CF6D89
2 changed files with 29 additions and 23 deletions

View file

@ -13,7 +13,7 @@ func executeClientCommand(action tg.ClientCommand, client net.Conn) {
api.SendTextMessage(data) api.SendTextMessage(data)
case tg.CmdGetFile: case tg.CmdGetFile:
data := *(action.FileRequestData) data := *(action.FileRequestData)
api.GetFile(data, client, *action.Callback) api.GetFileNet(data, client, *action.Callback)
case tg.CmdSendPhoto: case tg.CmdSendPhoto:
data := *(action.PhotoData) data := *(action.PhotoData)
api.SendPhoto(data) api.SendPhoto(data)

View file

@ -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 // 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 // specified afterward. The file will be then send back to the client that requested it
// with the specified callback id. // with the specified callback id.
func (t Telegram) GetFile(data FileRequestData, client net.Conn, callback int) { func (t Telegram) GetFile(data FileRequestData) ([]byte, error) {
fail := func(msg string) {
errmsg, _ := json.Marshal(BrokerUpdate{
Type: BError,
Error: &msg,
Callback: &callback,
})
fmt.Fprintln(client, string(errmsg))
}
postdata := url.Values{ postdata := url.Values{
"file_id": {data.FileID}, "file_id": {data.FileID},
} }
resp, err := http.PostForm(t.apiURL("getFile"), postdata) resp, err := http.PostForm(t.apiURL("getFile"), postdata)
if checkerr("GetFile/post", err) { if checkerr("GetFile/post", err) {
fail("Server didn't like my request") return nil, fmt.Errorf("Server didn't like my request: %w", err)
return
} }
defer resp.Body.Close() 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) err = json.NewDecoder(resp.Body).Decode(&filespecs)
if checkerr("GetFile/json.Decode", err) { if checkerr("GetFile/json.Decode", err) {
fail("Server sent garbage (or error)") return nil, fmt.Errorf("Server sent garbage (or error): %w", err)
return
} }
if filespecs.Result == nil { if filespecs.Result == nil {
fail("Server didn't send a file info, does the file exist?") return nil, fmt.Errorf("Server didn't send a file info, does the file exist?")
return
} }
result := *filespecs.Result result := *filespecs.Result
path := APIEndpoint + "file/bot" + t.Token + "/" + *result.Path path := APIEndpoint + "file/bot" + t.Token + "/" + *result.Path
fileresp, err := http.Get(path) fileresp, err := http.Get(path)
if checkerr("GetFile/get", err) { if checkerr("GetFile/get", err) {
fail("Could not retrieve file from Telegram's servers") return nil, fmt.Errorf("Could not retrieve file from Telegram's servers: %w", err)
return
} }
defer fileresp.Body.Close() defer fileresp.Body.Close()
rawdata, err := ioutil.ReadAll(fileresp.Body) rawdata, err := ioutil.ReadAll(fileresp.Body)
if checkerr("GetFile/ioutil.ReadAll", err) { if checkerr("GetFile/ioutil.ReadAll", err) {
fail("Could not read file data") return nil, fmt.Errorf("Could not read file data: %w", err)
return
} }
rawlen := len(rawdata) 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) 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{ clientmsg, err := json.Marshal(BrokerUpdate{
Type: BFile, Type: BFile,
Bytes: &b64data, Bytes: &b64data,
Callback: &callback, Callback: &callback,
}) })
if checkerr("GetFile/json.Marshal", err) { 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 return
} }