3

Using the ldapsj-client module, I'm trying to save the thumbnailPhoto into a file

const auth = async () => {

    const client = new LdapClient({ url: 'myaddomain' })
    await client.bind('someemail@domain.com.br', 'passwaord')

    const opts = {
        filter: `(sAMAccountName=credential)`,
        scope: "sub"
    }

    const s = await client.search(myBaseDN, opts)

    console.log('thumbnailPhoto', s[0].thumbnailPhoto)

}

The console.log() outputs something like '����JFIF��C...'

I cannot figure out how to save this binary into a file. When I try several approaches, as explained here, does not work. It seems the data from AD is not in the same "format".

I tried to convert it into a Buffer and then, to base64

const buffer = Buffer.from(s[0].thumbnailPhoto, 'binary')
var src = "data:image/png;base64," + Buffer.from(s[0].thumbnailPhoto).toString('base64')

But the output is not a valid base64.

Christian Benseler
  • 7,907
  • 8
  • 40
  • 71

1 Answers1

0

I'm really late to the game here, but if you are now using the ldapjs package (which seems to be a currently maintained version of what you used), you can access the buffers with the .raw prop.

With your code:

const s = await client.search(myBaseDN, opts)

console.log('thumbnailPhoto', s[0].raw.thumbnailPhoto)

And that can now be converted to base64.

 Buffer.from(s[0].raw.thumbnailPhoto, 'binary').toString('base64')

// <img src="data:image/*;base64,<base64 here>">

I suppose you are probably not working on this project anymore, but hopefully this will help another searcher like me :)

Note: check out this pr https://github.com/ldapjs/node-ldapjs/pull/107

sur.la.route
  • 440
  • 3
  • 11