1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-19 05:09:42 +02:00

Refactor custom & default logo logic

This commit is contained in:
McMatts 2019-01-07 12:41:55 +00:00
parent 90aa9710a6
commit dd355958a2
2 changed files with 24 additions and 11 deletions

View file

@ -310,29 +310,41 @@ func (h *Handler) Logo(w http.ResponseWriter, r *http.Request) {
ctx := domain.GetRequestContext(r)
d := organization.GetSubdomainFromHost(r)
// If organization has logo, send it back.
logo, err := h.Store.Organization.Logo(ctx, d)
if err != nil {
h.Runtime.Log.Infof("unable to fetch logo for domain %s", d)
response.WriteNotFound(w)
if err == nil && len(logo) > 0 {
h.writeLogo(w, r, logo)
return
}
if err != nil {
h.Runtime.Log.Infof("unable to fetch logo for domain %s", d)
}
if len(string(logo)) == 0 {
logo, err = base64.StdEncoding.DecodeString(defaultLogo)
// Otherwise, we send back default logo.
h.DefaultLogo(w, r)
}
// DefaultLogo write the default Documize logo to the HTTP response.
func (h *Handler) DefaultLogo(w http.ResponseWriter, r *http.Request) {
logo, err := base64.StdEncoding.DecodeString(defaultLogo)
if err != nil {
h.Runtime.Log.Error("unable to decode default logo", err)
response.WriteEmpty(w)
return
}
}
h.writeLogo(w, r, logo)
}
// writeLogo writes byte array as logo to HTTP response stream.
func (h *Handler) writeLogo(w http.ResponseWriter, r *http.Request, logo []byte) {
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(logo)))
w.WriteHeader(http.StatusOK)
_, err = w.Write(logo)
_, err := w.Write(logo)
if err != nil {
h.Runtime.Log.Error("failed to write org logo", err)
h.Runtime.Log.Error("failed to write logo", err)
return
}
}

View file

@ -95,6 +95,7 @@ func RegisterEndpoints(rt *env.Runtime, s *store.Store) {
AddPublic(rt, "reset/{token}", []string{"POST", "OPTIONS"}, nil, user.ResetPassword)
AddPublic(rt, "share/{spaceID}", []string{"POST", "OPTIONS"}, nil, space.AcceptInvitation)
AddPublic(rt, "attachment/{orgID}/{attachmentID}", []string{"GET", "OPTIONS"}, nil, attachment.Download)
AddPublic(rt, "logo", []string{"GET", "OPTIONS"}, []string{"default", "true"}, meta.DefaultLogo)
AddPublic(rt, "logo", []string{"GET", "OPTIONS"}, nil, meta.Logo)
//**************************************************