bug where a users profile photo would not display if the user had not

logged in
This commit is contained in:
2026-04-06 09:30:15 -04:00
parent 384ef90ea8
commit d0524f901c
2 changed files with 40 additions and 10 deletions

View File

@@ -98,7 +98,11 @@ func UploadPhotoHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
userDN := fmt.Sprintf("uid=%s,cn=users,cn=accounts,%s", sessionData.UserID, BaseDN) userDN := fmt.Sprintf("uid=%s,cn=users,cn=accounts,%s", sessionData.UserID, BaseDN)
LDAPServer.ModifyAttribute(ServiceUserBindDN, ServiceUserPassword, userDN, "jpegphoto", []string{string(data)}) err = LDAPServer.ModifyAttribute(ServiceUserBindDN, ServiceUserPassword, userDN, "jpegphoto", []string{string(data)})
if err != nil {
logging.Error(err.Error())
return
}
CreateUserPhoto(sessionData.UserID, data) CreateUserPhoto(sessionData.UserID, data)
} }
@@ -112,17 +116,25 @@ func AvatarHandler(w http.ResponseWriter, r *http.Request) {
filePath := fmt.Sprintf("./avatars/%s.jpeg", username) filePath := fmt.Sprintf("./avatars/%s.jpeg", username)
cleaned := filepath.Clean(filePath) cleaned := filepath.Clean(filePath)
value, err := helpers.ReadFile(cleaned) fileExist, err := helpers.DoesFileExist(cleaned)
if err != nil {
if err == nil { w.Write(blankPhotoData)
logging.Error(err.Error())
return
}
photoCreatedMutex.Lock() photoCreatedMutex.Lock()
if time.Since(photoCreatedTimestamp[username]) <= 5*time.Minute { if fileExist && time.Since(photoCreatedTimestamp[username]) <= 5*time.Minute {
photoCreatedMutex.Unlock() photoCreatedMutex.Unlock()
w.Write(value) val, err := helpers.ReadFile(cleaned)
if err != nil {
logging.Error(err.Error())
w.Write(blankPhotoData)
return
}
w.Write(val)
return return
} }
photoCreatedMutex.Unlock() photoCreatedMutex.Unlock()
}
userSearch, err := LDAPServer.SerchServer( userSearch, err := LDAPServer.SerchServer(
ServiceUserBindDN, ServiceUserPassword, ServiceUserBindDN, ServiceUserPassword,
@@ -130,10 +142,16 @@ func AvatarHandler(w http.ResponseWriter, r *http.Request) {
fmt.Sprintf("(&(objectClass=inetOrgPerson)(uid=%s))", ldap.LDAPEscapeFilter(username)), fmt.Sprintf("(&(objectClass=inetOrgPerson)(uid=%s))", ldap.LDAPEscapeFilter(username)),
[]string{"jpegphoto"}, []string{"jpegphoto"},
) )
if err == nil || userSearch.EntryCount() == 0 { if err != nil {
logging.Error(err.Error())
w.Write(blankPhotoData) w.Write(blankPhotoData)
return return
} }
if userSearch.EntryCount() == 0 {
w.Write(blankPhotoData)
return
}
entry := userSearch.GetEntry(0) entry := userSearch.GetEntry(0)
bytes := entry.GetRawAttributeValue("jpegphoto") bytes := entry.GetRawAttributeValue("jpegphoto")
if len(bytes) == 0 { if len(bytes) == 0 {
@@ -142,5 +160,6 @@ func AvatarHandler(w http.ResponseWriter, r *http.Request) {
} else { } else {
w.Write(bytes) w.Write(bytes)
CreateUserPhoto(username, bytes) CreateUserPhoto(username, bytes)
return
} }
} }

View File

@@ -52,6 +52,17 @@ func CreateFile(path string) (*os.File, error) {
return file, nil return file, nil
} }
func DoesFileExist(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func HandleFunc(path string, handler func(http.ResponseWriter, *http.Request)) { func HandleFunc(path string, handler func(http.ResponseWriter, *http.Request)) {
logging.Infof("Handling %s", path) logging.Infof("Handling %s", path)
http.HandleFunc(path, handler) http.HandleFunc(path, handler)