telebot/file.go

88 lines
2.3 KiB
Go
Raw Normal View History

2015-07-03 18:54:40 +00:00
package telebot
import (
"io"
2015-07-03 18:54:40 +00:00
"os"
)
// File object represents any sort of file.
type File struct {
FileID string `json:"file_id"`
2020-04-26 16:40:11 +00:00
UniqueID string `json:"file_unique_id"`
2015-07-03 18:54:40 +00:00
FileSize int `json:"file_size"`
2017-11-18 18:16:16 +00:00
// FilePath is used for files on Telegram server.
FilePath string `json:"file_path"`
2015-07-03 18:54:40 +00:00
2022-05-30 07:51:42 +00:00
// FileLocal is used for files on local file system.
FileLocal string `json:"file_local"`
2017-11-18 18:16:16 +00:00
// FileURL is used for file on the internet.
FileURL string `json:"file_url"`
// FileReader is used for file backed with io.Reader.
FileReader io.Reader `json:"-"`
fileName string
2017-11-18 18:16:16 +00:00
}
// FromDisk constructs a new local (on-disk) file object.
//
// Note, it returns File, not *File for a very good reason:
// in telebot, File is pretty much an embeddable struct,
// so upon uploading media you'll need to set embedded File
// with something. NewFile() returning File makes it a one-liner.
//
2020-07-25 21:01:39 +00:00
// photo := &tele.Photo{File: tele.FromDisk("chicken.jpg")}
2017-11-18 18:16:16 +00:00
//
func FromDisk(filename string) File {
return File{FileLocal: filename}
2015-07-03 18:54:40 +00:00
}
// FromURL constructs a new file on provided HTTP URL.
2015-07-03 18:54:40 +00:00
//
2017-11-18 18:16:16 +00:00
// Note, it returns File, not *File for a very good reason:
// in telebot, File is pretty much an embeddable struct,
// so upon uploading media you'll need to set embedded File
// with something. NewFile() returning File makes it a one-liner.
//
2020-07-25 21:01:39 +00:00
// photo := &tele.Photo{File: tele.FromURL("https://site.com/picture.jpg")}
2017-11-18 18:16:16 +00:00
//
func FromURL(url string) File {
return File{FileURL: url}
2017-11-18 18:16:16 +00:00
}
// FromReader constructs a new file from io.Reader.
//
// Note, it returns File, not *File for a very good reason:
// in telebot, File is pretty much an embeddable struct,
// so upon uploading media you'll need to set embedded File
// with something. NewFile() returning File makes it a one-liner.
//
2020-07-25 21:01:39 +00:00
// photo := &tele.Photo{File: tele.FromReader(bytes.NewReader(...))}
//
func FromReader(reader io.Reader) File {
return File{FileReader: reader}
}
2017-11-18 18:33:20 +00:00
func (f *File) stealRef(g *File) {
if g.OnDisk() {
f.FileLocal = g.FileLocal
2015-07-03 18:54:40 +00:00
}
if g.FileURL != "" {
2017-11-18 18:33:20 +00:00
f.FileURL = g.FileURL
}
2015-07-03 18:54:40 +00:00
}
2017-11-18 18:16:16 +00:00
// InCloud tells whether the file is present on Telegram servers.
func (f *File) InCloud() bool {
2015-10-13 14:38:15 +00:00
return f.FileID != ""
2015-07-03 18:54:40 +00:00
}
2015-07-06 16:58:54 +00:00
2017-11-18 18:16:16 +00:00
// OnDisk will return true if file is present on disk.
func (f *File) OnDisk() bool {
2020-06-09 20:28:28 +00:00
_, err := os.Stat(f.FileLocal)
return err == nil
2015-07-06 16:58:54 +00:00
}