move gotd fork into repo. (#111)
- 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
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
// Package source contains remote source interface and implementations for uploader.
|
||||
package source
|
||||
@@ -0,0 +1,82 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package source
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestHTTPSource(t *testing.T) {
|
||||
t.Run("OK", func(t *testing.T) {
|
||||
a := require.New(t)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
||||
defer cancel()
|
||||
|
||||
data := bytes.Repeat([]byte{1}, 10)
|
||||
var h http.HandlerFunc = func(w http.ResponseWriter, req *http.Request) {
|
||||
_, err := w.Write(data)
|
||||
a.NoError(err)
|
||||
}
|
||||
|
||||
s := httptest.NewServer(h)
|
||||
defer s.Close()
|
||||
|
||||
src := new(HTTPSource).WithClient(s.Client())
|
||||
f, err := src.Open(ctx, &url.URL{
|
||||
Scheme: "http",
|
||||
Host: s.Listener.Addr().String(),
|
||||
Path: "img.jpg",
|
||||
})
|
||||
a.NoError(err)
|
||||
a.Len(data, int(f.Size()))
|
||||
a.Equal("img.jpg", f.Name())
|
||||
|
||||
r, err := io.ReadAll(f)
|
||||
a.NoError(err)
|
||||
a.Equal(data, r)
|
||||
|
||||
a.NoError(f.Close())
|
||||
})
|
||||
|
||||
t.Run("NotFound", func(t *testing.T) {
|
||||
a := require.New(t)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
||||
defer cancel()
|
||||
|
||||
var h http.HandlerFunc = func(w http.ResponseWriter, req *http.Request) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
|
||||
s := httptest.NewServer(h)
|
||||
defer s.Close()
|
||||
|
||||
src := new(HTTPSource).WithClient(s.Client())
|
||||
_, err := src.Open(ctx, &url.URL{
|
||||
Scheme: "http",
|
||||
Host: s.Listener.Addr().String(),
|
||||
Path: "img.jpg",
|
||||
})
|
||||
a.Error(err)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package source
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// RemoteFile is abstraction for remote file.
|
||||
type RemoteFile interface {
|
||||
io.ReadCloser
|
||||
// Name returns filename. Should not be empty.
|
||||
Name() string
|
||||
// Size returns size of file. If size is unknown, -1 should be returned.
|
||||
Size() int64
|
||||
}
|
||||
|
||||
// Source is abstraction for remote upload source.
|
||||
type Source interface {
|
||||
Open(ctx context.Context, u *url.URL) (RemoteFile, error)
|
||||
}
|
||||
Reference in New Issue
Block a user