Translate

Saturday, May 7, 2022

Golang - Display Active Directory thumbnailPhoto attribute as HTML IMG

I'm working on an application to list all of the active directory attributes that are set for a user. The application uses "encoding/base64" and "github.com/go-ldap/ldap". To display the tumbnai imag, I used this HTML:

"<div style='width:400px;'><img src='data:image/jpg;base64," + b64.StdEncoding.EncodeToString(attrE.ByteValues[0]) + "'/></div>"

This is the code that iterates through all of the returned AD attributes and creates the HTML text:

var sHTML string
var sA []string
for _, entry := range strcAttrs.Entries {
	for _, attr := range entry.Attributes {
		sA = append(sA, attr.Name)
	}
}
sort.Strings(sA)
for _, attE := range sA {
	for _, entry := range strcAttrs.Entries {
		for _, attrE := range entry.Attributes {
			if attrE.Name == attE {
				if ldapAccess.IncSlcStdAttrs(attrE.Name) {
					sHTML = sHTML + "<span style='text-decoration: bold; background-color: green'>"
				} else {
					sHTML = sHTML + "<span style='text-decoration: bold;'>"
				}
				sHTML = sHTML + "attribute: " + attrE.Name + "</span><br />"
				var attr string = ""
				for _, attr = range attrE.Values {
					if ldapAccess.Include(attrE.Name) {
						if ldapAccess.IsDate64(attrE.Name) {
							sHTML = sHTML + "Date: " + ldapAccess.ConvertStringToDate64(attr)
						} else {
							if ldapAccess.IsDateZ(attrE.Name) {
								sHTML = sHTML + "Date: " + ldapAccess.ConvertStringToDateZ(attr)
							} else {
								sHTML = sHTML + attr
							}
						}
					} else {
						if (attrE.Name) == "thumbnailPhoto" {
							sHTML = sHTML + "<div style='width:400px;'><img src='data:image/jpg;base64," + b64.StdEncoding.EncodeToString(attrE.ByteValues[0]) + "'/></div>"
						} else {

							sHTML = sHTML + "Not Displayed"
						}
					}
					sHTML = sHTML + "<br/>"
				}
			}
		}
	}
	sHTML = sHTML + "<br/>"
}

No comments:

Post a Comment

Thank you for commenting!