Documentation
¶
Overview ¶
Package avatar implements avatart proxy for oauth and defines store interface and implements local (fs), gridfs (mongo) and boltdb stores.
Index ¶
- Variables
- func GenerateAvatar(user string) ([]byte, error)
- func GetGravatarURL(email string) (res string, err error)
- func Migrate(dst, src Store) (int, error)
- type BoltDB
- func (b *BoltDB) Close() error
- func (b *BoltDB) Get(avatarID string) (reader io.ReadCloser, size int, err error)
- func (b *BoltDB) ID(avatarID string) (id string)
- func (b *BoltDB) List() (ids []string, err error)
- func (b *BoltDB) Put(userID string, reader io.Reader) (avatar string, err error)
- func (b *BoltDB) Remove(avatarID string) (err error)
- func (b *BoltDB) String() string
- type GridFS
- func (gf *GridFS) Close() error
- func (gf *GridFS) Get(avatar string) (reader io.ReadCloser, size int, err error)
- func (gf *GridFS) ID(avatar string) (id string)
- func (gf *GridFS) List() (ids []string, err error)
- func (gf *GridFS) Put(userID string, reader io.Reader) (avatar string, err error)
- func (gf *GridFS) Remove(avatar string) error
- func (gf *GridFS) String() string
- type LocalFS
- func (fs *LocalFS) Close() error
- func (fs *LocalFS) Get(avatar string) (reader io.ReadCloser, size int, err error)
- func (fs *LocalFS) ID(avatar string) (id string)
- func (fs *LocalFS) List() (ids []string, err error)
- func (fs *LocalFS) Put(userID string, reader io.Reader) (avatar string, err error)
- func (fs *LocalFS) Remove(avatar string) error
- func (fs *LocalFS) String() string
- type NoOp
- func (s *NoOp) Close() error
- func (s *NoOp) Get(string) (reader io.ReadCloser, size int, err error)
- func (s *NoOp) ID(string) (id string)
- func (s *NoOp) List() (ids []string, err error)
- func (s *NoOp) Put(string, io.Reader) (avatarID string, err error)
- func (s *NoOp) Remove(string) error
- func (s *NoOp) String() string
- type Proxy
- type Store
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("avatar not found")
ErrNotFound is returned by Store.Remove when the requested avatar does not exist, so callers can tell a missing avatar from a real failure with errors.Is.
Functions ¶
func GenerateAvatar ¶
GenerateAvatar for give user with identicon
func GetGravatarURL ¶
GetGravatarURL returns url to gravatar picture for given email
func Migrate ¶
Migrate copies avatars from src to dst, returning the number of ids enumerated from src and the first List error if any. Per-avatar Get/Put/Close failures are logged with [WARN] and skipped — they do not abort the migration or surface in the returned error, so the returned count is "ids attempted", not "ids stored".
Types ¶
type BoltDB ¶
type BoltDB struct {
// contains filtered or unexported fields
}
BoltDB implements avatar store with bolt using separate db (file) with "avatars" bucket to keep image bin and "metas" bucket to keep a sha1 fingerprint of the stored bytes. avatarID (base file name) used as a key for both.
func (*BoltDB) Put ¶
Put stores avatar bytes read from reader in the "avatars" bucket and a sha1 of the same bytes in the "metas" bucket, both keyed by the encoded userID with .image suffix. Resizing happens upstream in Proxy.resize; this layer only writes what it is given.
type GridFS ¶
type GridFS struct {
// contains filtered or unexported fields
}
GridFS implements Store for GridFS
func (*GridFS) ID ¶
ID returns the sha1 fingerprint of the avatar content stored in the GridFS file's metadata at Put time, or an encoded fallback id when the file lookup/decode fails.
type LocalFS ¶
type LocalFS struct {
// contains filtered or unexported fields
}
LocalFS implements Store for local file system
func NewLocalFS ¶
NewLocalFS makes file-system avatar store
type NoOp ¶
type NoOp struct{}
NoOp is an empty (no-op) implementation of Store interface
func NewNoOp ¶
func NewNoOp() *NoOp
NewNoOp provides an empty (no-op) implementation of Store interface
type Proxy ¶
Proxy provides http handler for avatars from avatar.Store On user login token will call Put and it will retrieve and save picture locally.
func (*Proxy) Handler ¶
func (p *Proxy) Handler(w http.ResponseWriter, r *http.Request)
Handler serves stored avatar content by avatar id. GET only; rejects invalid ids (403) and stored bytes that fail the safeImgContentType sniff (415). Layered defense headers are set on every response via setAvatarDefenseHeaders.
func (*Proxy) Put ¶
Put fetches u.Picture, validates and optionally resizes the body, stores it via Store, and returns the proxied avatar URL. If u.Picture is empty, the fetch fails, or the upstream bytes are not a recognized image format within the configured dimension and size limits, Put silently falls back to a generated identicon and returns its proxied URL — the caller is not told upstream was rejected.
func (*Proxy) PutContent ¶ added in v2.1.3
PutContent stores already-fetched avatar bytes via the underlying Store and returns the proxied URL. It exists so providers that authenticate with credentials embedded in the upstream URL (e.g. Telegram bot file API: /file/bot{TOKEN}/...) can fetch the content themselves and avoid exposing the credential to Put's URL-fetching path — where it would land in u.Picture, debug logs, and the user JSON returned to clients.
Bytes are read into memory bounded by maxAvatarFetchSize so an unbounded caller (e.g. a streaming HTTP body) cannot exhaust process memory.
type Store ¶
type Store interface {
fmt.Stringer
Put(userID string, reader io.Reader) (avatarID string, err error) // save avatar data from the reader and return base name
Get(avatarID string) (reader io.ReadCloser, size int, err error) // load avatar via reader
ID(avatarID string) (id string) // unique id of stored avatar's data
Remove(avatarID string) error // remove avatar data
List() (ids []string, err error) // list all avatar ids
Close() error // close store
}
Store defines interface to store and load avatars