Hi everyone,
I'm using this groovy code to validate whether any of attachments added during workflow transition go beyond my allowed size. It works the first time, meaning that it correctly validates that there's an attachment with size that goes over the MAX_ATTACHMENT_SIZE limit, which can be seen on this screen
But when I then removed the file which goes over the limit (merged.pdf) and i try to transition again the validator still fails like this:
What I found out in the log is that the tempWebAttachments variable still holds the merged.pdf even though I removed it from the form. Is there anyway for tempWebAttachments to be up to date, even after I removed/added some attachments?
My validator code:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueFieldConstants
import com.atlassian.jira.issue.attachment.TemporaryWebAttachment
import com.atlassian.jira.issue.attachment.TemporaryWebAttachmentManager
import webwork.action.ActionContext
def temporaryAttachmentUtil = ComponentAccessor.getComponent(TemporaryWebAttachmentManager)
def formToken = ActionContext.getRequest()?.getParameter(IssueFieldConstants.FORM_TOKEN)
def MAX_ATTACHMENT_SIZE = 20971520
if (formToken) {
def tempWebAttachments = temporaryAttachmentUtil.getTemporaryWebAttachmentsByFormToken(formToken)
tempWebAttachments.each { it ->
log.debug "Uploaded attachment name: ${it.filename}"
log.debug "Uploaded attachment filesize: ${it.size}"
}
return tempWebAttachments.every{it -> it.size <= MAX_ATTACHMENT_SIZE}
}
Regards.
Hi @ZubatyNos
There is no API call to 'refresh' the TemporaryWebAttachment.
Try putting in a log message to examine the content type with strip params
it.getContentType(true)
temporaryAttachmentUtil.getTemporaryWebAttachment(it.getTemporaryAttachmentId()
Hi @Tom Lister , thank you for taking your time.
So logging it.getContentType(true)
didn' t show any additional "active" flag sadly, just the MIME type.
Looping through attachments and getting them spefically didn't help
temporaryAttachmentUtil.getTemporaryWebAttachment(
it.getTemporaryAttachmentId().toStringId()
).getOrNull()
The "inactive" tempAttachments were still present the same way as getting them by the form token.
temporaryAttachmentUtil.getTemporaryWebAttachmentsByFormToken(formToken)
The missing puzzle piece was this community post.
Where variable newAttachmentsNames gets all the "active" attachments from the form but in the form of temporaryAttachmentIds
def newAttachmentNames = issue2.modifiedFields.get(IssueFieldConstants.ATTACHMENT)?.newValue as List
the final script where i looped through the newAttachmentNames and paired them via the temporaryAttachmentUtil.getTemporaryWebAttachment function:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueFieldConstants
import com.atlassian.jira.issue.attachment.TemporaryWebAttachment
import com.atlassian.jira.issue.attachment.TemporaryWebAttachmentManager
import webwork.action.ActionContext
import com.atlassian.jira.issue.IssueFieldConstants
import com.atlassian.jira.issue.MutableIssue
def temporaryAttachmentUtil = ComponentAccessor.getComponent(TemporaryWebAttachmentManager)
def formToken = ActionContext.getRequest()?.getParameter(IssueFieldConstants.FORM_TOKEN)
def MAX_ATTACHMENT_SIZE = 20971520
if (formToken) {
def issue2 = issue as MutableIssue
def newAttachmentNames = issue2.modifiedFields.get(IssueFieldConstants.ATTACHMENT)?.newValue as List
return newAttachmentNames.every{it ->
def utilAttach = temporaryAttachmentUtil.getTemporaryWebAttachment(it).getOrNull()
if (utilAttach != null) {
return utilAttach.size <= MAX_ATTACHMENT_SIZE
}
return true
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Glad to see you got to the solution. Could you mark it as solved for other users?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.