diff options
author | Julio Capote <jcapote@gmail.com> | 2023-01-07 22:54:38 +0000 |
---|---|---|
committer | Julio Capote <jcapote@gmail.com> | 2023-01-07 22:54:38 +0000 |
commit | 14963269dc63bf038163a851e521d6815ab5f514 (patch) | |
tree | d85f0b5cfebe22924e0068ce1b47b65c79953331 /registry | |
parent | af05fbea27df62c96b411a941cf5bb612f256e9d (diff) | |
download | communique-14963269dc63bf038163a851e521d6815ab5f514.tar.gz |
avatar support
Diffstat (limited to 'registry')
-rw-r--r-- | registry/registry.go | 57 |
1 files changed, 56 insertions, 1 deletions
diff --git a/registry/registry.go b/registry/registry.go index aa61d58..e50c6a5 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -9,6 +9,7 @@ import ( "encoding/pem" "fmt" "io" + "io/ioutil" "net/http" "net/url" "strings" @@ -47,6 +48,10 @@ func NewRegistry(cfg config.Config, persister *models.Persister, log *zap.Sugare if err != nil { return nil, err } + err = persistAvatarIfFound(v, persister) + if err != nil { + return nil, err + } } reg.delivery, err = delivery.NewSigned(persister) if err != nil { @@ -67,6 +72,36 @@ func generateKeypairIfNeeded(v config.Handler, p *models.Persister) error { return nil } +func persistAvatarIfFound(v config.Handler, p *models.Persister) error { + if v.AvatarUrl == "" || v.AvatarContentType == "" { + return nil + } + resp, err := http.Get(v.AvatarUrl) + if resp.StatusCode != 200 { + return fmt.Errorf("request to avatarUrl %s failed", v.AvatarUrl) + } + contentType := resp.Header.Get("content-type") + if contentType != v.AvatarContentType { + return fmt.Errorf("avatarUrl response content-type '%s' does match avatarContentType '%s'", contentType, v.AvatarContentType) + } + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return err + } + if len(body) == 0 { + return fmt.Errorf("avatarUrl response was empty") + } + kp, err := models.CreateAvatar(v, v.AvatarContentType, body) + if err != nil { + return err + } + err = p.Store(kp) + if err != nil { + return err + } + return nil +} + func (r *Registry) Actor(name string) (map[string]interface{}, error) { handler := r.findByName(name) if handler == nil { @@ -91,7 +126,27 @@ func (r *Registry) Actor(name string) (map[string]interface{}, error) { Bytes: x509.MarshalPKCS1PublicKey(&privKey.PublicKey), }, ) - return views.RenderActor(handler.handlerCfg.Name, r.cfg.Domain, string(pemdata)) + return views.RenderActor(handler.handlerCfg.Name, r.cfg.Domain, string(pemdata), handler.handlerCfg.AvatarContentType) +} + +func (r *Registry) ActorAvatar(name string) ([]byte, string, error) { + handler := r.findByName(name) + if handler == nil { + return nil, "", nil + } + aso := models.NewAvatar(handler.handlerCfg) + result, err := r.persister.Find(aso) + if err != nil { + return nil, "", err + } + buf := bytes.NewBuffer(result) + dec := gob.NewDecoder(buf) + var avatar models.Avatar + err = dec.Decode(&avatar) + if err != nil { + return nil, "", err + } + return avatar.Bytes, handler.handlerCfg.AvatarContentType, nil } func (r *Registry) OutboxCollection(name string) (map[string]interface{}, error) { |