Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

uploading a user avatar with scriptrunner

I've got image data in base64 strings and want to set user avatars to that image. e.g.

data:image/jpg;base64,/9j/4AA... ...KR//9k=

My current code runs without error and appears to work, except the image isn't loaded correctly. The current avatar is replaced with

useravatar.png

What I'm doing is

Pattern reImage = Pattern.compile('^data\\:(.*?);base64,(.*)$')
def m4 = reImage.matcher(imageraw)
if (m4) {
String imageType = m4[0][1]
String imageB64 = m4[0][2]
byte[] imageBA = imageB64.decodeBase64()
ByteArrayInputStream bais = new ByteArrayInputStream(imageBA)
BufferedImage image = ImageIO.read(bais)
bais.close()
//I've cropped, resized and saved the image and it all works. This tells me the BufferedImage is loaded well
imageFileName = 'somefile.ext' // logic is a bit more complex but this will do for the example
Avatar newAvatar = avatarManager.create(
imageFileName,
imageType,
IconType.USER_ICON_TYPE,
new IconOwningObjectId(user.id.toString()),
new ByteArrayInputStream(imageBA),
null) //I've also tested with a non null Selection
//The returned avatar has a non 0 id, and other values are as expected
avatarService.setCustomUserAvatar(currentUser,user,newAvatar.id)
}

 

1 answer

0 votes
scott_boisvert Community Leader Jan 09, 2020

@Daniel Garcia I'm running into the same issue, were you ever able to figure this out?

scott_boisvert Community Leader Jan 09, 2020

Disregard figured it out. Problem was the user.

Where you have 

new IconOwningObjectId(user.id.toString())

It should be:

new IconOwningObjectId(user.getUsername())

Provided user is of type ApplicationUser 

scott_boisvert Community Leader Jan 09, 2020
import com.atlassian.jira.avatar.Avatar
import com.atlassian.jira.avatar.AvatarManager
import com.atlassian.jira.avatar.AvatarService
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.icon.IconType
import com.atlassian.jira.icon.IconOwningObjectId

ApplicationUser user = ComponentAccessor.getUserManager().getUserByName("bill")

def fileName = "bill.png"
File file = new File("/data/atlassian/application-data/jira/scripts/data/bill.png")
def contentType = AvatarManager.PNG_CONTENT_TYPE
IconType iconType = IconType.USER_ICON_TYPE
IconOwningObjectId owner = new IconOwningObjectId(user.getUsername())
BufferedInputStream iStream = new BufferedInputStream(new FileInputStream(file))
AvatarManager avatarManager = ComponentAccessor.getAvatarManager()
Avatar avatar = avatarManager.create(fileName, contentType, iconType, owner, iStream, null)

AvatarService avatarService = ComponentAccessor.getAvatarService()
avatarService.setCustomUserAvatar(user, user, avatar.getId())

Hi Scott,

good and useful script!

Anyway I had to subsitute

new IconOwningObjectId(user.getUsername())

with

new IconOwningObjectId(user.getKey())

With this expedient your script worked like a charm!

Thanks,

Alberto

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Bitbucket Pipelines

Bitbucket Pipelines Runners is now in open beta

We are excited to announce the open beta program for self-hosted runners. Bitbucket Pipelines Runners is available to everyone. Please try it and let us know your feedback. If you have any issue...

376 views 9 8
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you