🎨 Format of the upload request comments
This commit is contained in:
parent
df26b0bddc
commit
d5e2a04158
54
upload.go
54
upload.go
|
@ -5,9 +5,10 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
// "io/ioutil"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
// "os"
|
||||||
|
|
||||||
log "github.com/kirillDanshin/dlog"
|
log "github.com/kirillDanshin/dlog"
|
||||||
json "github.com/pquerna/ffjson/ffjson"
|
json "github.com/pquerna/ffjson/ffjson"
|
||||||
|
@ -16,39 +17,38 @@ import (
|
||||||
|
|
||||||
var ErrBadFileType = errors.New("bad file type")
|
var ErrBadFileType = errors.New("bad file type")
|
||||||
|
|
||||||
// upload is a helper method which provide are three ways to send files
|
// upload is a helper method which provide are three ways to send files (photos, stickers, audio,
|
||||||
// (photos, stickers, audio, media, etc.):
|
// media, etc.):
|
||||||
//
|
//
|
||||||
// 1. If the file is already stored somewhere on the Telegram servers, you don't
|
// 1. If the file is already stored somewhere on the Telegram servers, you don't need to reupload it:
|
||||||
// need to reupload it: each file object has a file_id field, simply pass this
|
// each file object has a file_id field, simply pass this file_id as a parameter instead of uploading.
|
||||||
// file_id as a parameter instead of uploading. There are no limits for files
|
// There are no limits for files sent this way.
|
||||||
// sent this way.
|
// 2. Provide Telegram with an *url.URL for the file to be sent. Telegram will download and send the
|
||||||
// 2. Provide Telegram with an *url.URL for the file to be sent. Telegram will
|
// file. 5 MB max size for photos and 20 MB max for other types of content.
|
||||||
// download and send the file. 5 MB max size for photos and 20 MB max for other
|
// 3. Post the file using multipart/form-data in the usual way that files are uploaded via the
|
||||||
// types of content.
|
// browser. Use []byte or io.Reader for this. 10 MB max size for photos, 50 MB for other files.
|
||||||
// 3. Post the file using multipart/form-data in the usual way that files are
|
|
||||||
// uploaded via the browser. Use path string, []byte or io.Reader for this. 10 MB
|
|
||||||
// max size for photos, 50 MB for other files.
|
|
||||||
//
|
//
|
||||||
// Sending by file_id
|
// Sending by file_id
|
||||||
//
|
//
|
||||||
// - It is not possible to change the file type when resending by file_id. I.e.
|
// - It is not possible to change the file type when resending by file_id. I.e. a video can't be sent
|
||||||
// a video can't be sent as a photo, a photo can't be sent as a document, etc.
|
// as a photo, a photo can't be sent as a document, etc.
|
||||||
// - It is not possible to resend thumbnails.
|
// - It is not possible to resend thumbnails.
|
||||||
// - Resending a photo by file_id will send all of its sizes.
|
// - Resending a photo by file_id will send all of its sizes.
|
||||||
// - file_id is unique for each individual bot and can't be transferred from one
|
// - file_id is unique for each individual bot and can't be transferred from one bot to another.
|
||||||
// bot to another.
|
|
||||||
//
|
//
|
||||||
// Sending by URL
|
// Sending by URL
|
||||||
//
|
//
|
||||||
// - When sending by *url.URL the target file must have the correct MIME type
|
// - When sending by *url.URL the target file must have the correct MIME type (e.g., audio/mpeg for
|
||||||
// (e.g., audio/mpeg for sendAudio, etc.).
|
// sendAudio, etc.).
|
||||||
// - In sendDocument, sending by URL will currently only work for gif, pdf and
|
// - In sendDocument, sending by URL will currently only work for gif, pdf and zip files.
|
||||||
// zip files.
|
// - To use SendVoice, the file must have the type audio/ogg and be no more than 1MB in size. 1–20MB
|
||||||
// - To use SendVoice, the file must have the type audio/ogg and be no more than
|
// voice notes will be sent as files.
|
||||||
// 1MB in size. 1–20MB voice notes will be sent as files.
|
|
||||||
// - Other configurations may work but we can't guarantee that they will.
|
// - Other configurations may work but we can't guarantee that they will.
|
||||||
func (bot *Bot) upload(file InputFile, fieldName, fileName, method string, args *http.Args) (*Response, error) {
|
func (bot *Bot) upload(
|
||||||
|
file InputFile,
|
||||||
|
fieldName, fileName, method string,
|
||||||
|
args *http.Args,
|
||||||
|
) (*Response, error) {
|
||||||
buffer := bytes.NewBuffer(nil)
|
buffer := bytes.NewBuffer(nil)
|
||||||
multi := multipart.NewWriter(buffer)
|
multi := multipart.NewWriter(buffer)
|
||||||
|
|
||||||
|
@ -96,20 +96,22 @@ func (bot *Bot) upload(file InputFile, fieldName, fileName, method string, args
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err = io.Copy(formFile, bytes.NewReader(f)); err != nil {
|
if _, err = io.Copy(formFile, bytes.NewReader(f)); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
case *url.URL:
|
case *url.URL: // Send by URL
|
||||||
if err := multi.WriteField(fieldName, f.String()); err != nil {
|
if err := multi.WriteField(fieldName, f.String()); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
case io.Reader:
|
case io.Reader: // Upload new
|
||||||
if _, err := multi.CreateFormFile(fieldName, fileName); err != nil {
|
if _, err := multi.CreateFormFile(fieldName, fileName); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return nil, ErrBadFileType
|
return nil, ErrBadFileType
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := multi.Close(); err != nil {
|
if err := multi.Close(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user