7a04f298d2
- update to latest telegram layer - remove some references to fields in tg.Entities that don't exist in the schema - originally added here: https://github.com/beeper/td/commit/820929062a2ba0104397bc01235ab58a9cff780e - referenced here - https://github.com/mautrix/telegramgo/commit/124f0967ed195b5a380c9bd02e170ada9710dde3 - https://github.com/mautrix/telegramgo/commit/4205047aab2e0639217148b5d125bfaab668bd8e
83 lines
1.5 KiB
Go
83 lines
1.5 KiB
Go
package source
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"path"
|
|
|
|
"github.com/go-faster/errors"
|
|
"go.uber.org/multierr"
|
|
)
|
|
|
|
// HTTPSource is HTTP source.
|
|
type HTTPSource struct {
|
|
client *http.Client
|
|
}
|
|
|
|
// NewHTTPSource creates new HTTPSource.
|
|
func NewHTTPSource() *HTTPSource {
|
|
return &HTTPSource{client: http.DefaultClient}
|
|
}
|
|
|
|
// WithClient sets HTTP client to use.
|
|
func (s *HTTPSource) WithClient(client *http.Client) *HTTPSource {
|
|
s.client = client
|
|
return s
|
|
}
|
|
|
|
type httpFile struct {
|
|
body io.ReadCloser
|
|
name string
|
|
size int64
|
|
}
|
|
|
|
func (h httpFile) Read(p []byte) (n int, err error) {
|
|
return h.body.Read(p)
|
|
}
|
|
|
|
func (h httpFile) Close() error {
|
|
return h.body.Close()
|
|
}
|
|
|
|
func (h httpFile) Name() string {
|
|
return h.name
|
|
}
|
|
|
|
func (h httpFile) Size() int64 {
|
|
return h.size
|
|
}
|
|
|
|
// Open implements Source.
|
|
func (s *HTTPSource) Open(ctx context.Context, u *url.URL) (_ RemoteFile, rerr error) {
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "create request")
|
|
}
|
|
|
|
resp, err := s.client.Do(req)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "get")
|
|
}
|
|
defer func() {
|
|
if rerr != nil {
|
|
multierr.AppendInto(&rerr, resp.Body.Close())
|
|
}
|
|
}()
|
|
if resp.StatusCode >= 400 {
|
|
return nil, errors.Errorf("bad code %d", resp.StatusCode)
|
|
}
|
|
|
|
lastURL := u
|
|
if resp.Request.URL != nil {
|
|
lastURL = resp.Request.URL
|
|
}
|
|
|
|
return httpFile{
|
|
body: resp.Body,
|
|
name: path.Base(lastURL.Path),
|
|
size: resp.ContentLength,
|
|
}, nil
|
|
}
|