Skip to content

Commit

Permalink
Detect and set proper Content-Type for avatars
Browse files Browse the repository at this point in the history
Detect proper avatar type to return instead of returning `image/*`,
which is not valid.
  • Loading branch information
paskal committed Sep 21, 2024
1 parent 458e57e commit 825b117
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
17 changes: 15 additions & 2 deletions avatar/avatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import (
"github.com/go-pkgz/auth/token"
)

// http.sniffLen is 512 bytes which is how much we need to read to detect content type
const sniffLen = 512

// Proxy provides http handler for avatars from avatar.Store
// On user login token will call Put and it will retrieve and save picture locally.
type Proxy struct {
Expand Down Expand Up @@ -100,7 +103,6 @@ func (p *Proxy) load(url string, client *http.Client) (rc io.ReadCloser, err err

// Handler returns token routes for given provider
func (p *Proxy) Handler(w http.ResponseWriter, r *http.Request) {

if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
}
Expand Down Expand Up @@ -136,9 +138,20 @@ func (p *Proxy) Handler(w http.ResponseWriter, r *http.Request) {
}
}()

w.Header().Set("Content-Type", "image/*")
buf := make([]byte, sniffLen)
n, err := avReader.Read(buf)
if err != nil {
p.Logf("[WARN] can't read from avatar reader for %s, %s", avatarID, err)
return
}
w.Header().Set("Content-Length", strconv.Itoa(size))
w.Header().Set("Content-Type", http.DetectContentType(buf))
w.WriteHeader(http.StatusOK)
if _, err = w.Write(buf[:n]); err != nil {
p.Logf("[WARN] can't write response to %s, %s", r.RemoteAddr, err)
return
}
// write the rest of response size if it's bigger than 512 bytes, or nothing as EOF would be sent right away then
if _, err = io.Copy(w, avReader); err != nil {
p.Logf("[WARN] can't send response to %s, %s", r.RemoteAddr, err)
}
Expand Down
17 changes: 15 additions & 2 deletions v2/avatar/avatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import (
"github.com/go-pkgz/auth/v2/token"
)

// http.sniffLen is 512 bytes which is how much we need to read to detect content type
const sniffLen = 512

// Proxy provides http handler for avatars from avatar.Store
// On user login token will call Put and it will retrieve and save picture locally.
type Proxy struct {
Expand Down Expand Up @@ -100,7 +103,6 @@ func (p *Proxy) load(url string, client *http.Client) (rc io.ReadCloser, err err

// Handler returns token routes for given provider
func (p *Proxy) Handler(w http.ResponseWriter, r *http.Request) {

if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
}
Expand Down Expand Up @@ -136,9 +138,20 @@ func (p *Proxy) Handler(w http.ResponseWriter, r *http.Request) {
}
}()

w.Header().Set("Content-Type", "image/*")
buf := make([]byte, sniffLen)
n, err := avReader.Read(buf)
if err != nil {
p.Logf("[WARN] can't read from avatar reader for %s, %s", avatarID, err)
return
}
w.Header().Set("Content-Length", strconv.Itoa(size))
w.Header().Set("Content-Type", http.DetectContentType(buf))
w.WriteHeader(http.StatusOK)
if _, err = w.Write(buf[:n]); err != nil {
p.Logf("[WARN] can't write response to %s, %s", r.RemoteAddr, err)
return
}
// write the rest of response size if it's bigger than 512 bytes, or nothing as EOF would be sent right away then
if _, err = io.Copy(w, avReader); err != nil {
p.Logf("[WARN] can't send response to %s, %s", r.RemoteAddr, err)
}
Expand Down

0 comments on commit 825b117

Please sign in to comment.