From 14963269dc63bf038163a851e521d6815ab5f514 Mon Sep 17 00:00:00 2001 From: Julio Capote Date: Sat, 7 Jan 2023 17:54:38 -0500 Subject: avatar support --- registry/registry.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) (limited to 'registry/registry.go') 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) { -- cgit v1.2.3