Skip to content

Commit

Permalink
Fix for fieldname generation in FileReader
Browse files Browse the repository at this point in the history
  • Loading branch information
John Doe committed Jul 6, 2022
1 parent 59c0834 commit 6f77eec
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions availableTypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package telegrambot
import (
"encoding/hex"
"io"
"math/rand"
)

// This object represents a Telegram user or bot.
Expand Down Expand Up @@ -1509,12 +1510,29 @@ type FileReader struct {
// Name of the file
Name string
Reader io.Reader

fieldname string
}

func (fr *FileReader) multipartFormFile() (fieldname string, filename string, reader io.Reader) {
fr.checkFieldname()

return fr.fieldname, fr.Name, fr.Reader
}

func (fr *FileReader) MarshalJSON() ([]byte, error) {
return []byte(`"` + "attach://" + hex.EncodeToString([]byte(fr.Name)) + `"`), nil
fr.checkFieldname()

return []byte(`"` + "attach://" + fr.fieldname + `"`), nil
}

func (fr *FileReader) multipartFormFile() (fieldname string, filename string, reader io.Reader) {
return hex.EncodeToString([]byte(fr.Name)), fr.Name, fr.Reader
func (fr *FileReader) checkFieldname() {
if fr.fieldname != "" {
return
}

b := make([]byte, 6)
rand.Read(b)

fr.fieldname = hex.EncodeToString(b)
}

0 comments on commit 6f77eec

Please sign in to comment.