Missed Team ’24? Catch up on announcements here.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Ways to rename attachments in JIRA

Andris Bērziņš
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 22, 2015

Hi.
I know it's not possible to rename JIRA attachments (https://answers.atlassian.com/questions/103334)
I was wondering how to do it using a plugin. I didn't see any functionality for it in AttachmentManager or Attachment classes. Do I have to go directly to database ([JIRA].[dbo].[fileattachment].[FILENAME])? I know that it has to be possible in some way - These guys are doing it.

4 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

3 votes
Answer accepted
Sascha Becker January 23, 2015

My approach would be extracting the file for the attachment. Delete the old attachment, rename the file and create a new attachment for it.

 

List<Attachment> attachments = attachmentManager.getAttachments(issue);
for (Attachment attachment : attachments) {
    String filePath = PathUtils.joinPaths(attachmentPathManager.getAttachmentPath(), issue.getProjectObject().getKey(), issue.getKey(), attachment.getId().toString());

    File atFile = new File(filePath);
	atFile.rename(....);
	

    if (atFile.exists()) {
        CreateAttachmentParamsBean createAttachmentParamsBean = new CreateAttachmentParamsBean(atFile, attachment.getFilename(), attachment.getMimetype(), attachment.getAuthorObject(), mutableIssueObject, attachment.isZip(), attachment.isThumbnailable(), null, attachment.getCreated(), true);
        if (atFile.canRead()) {
            try {
                attachmentManager.createAttachment(createAttachmentParamsBean);
            } catch (AttachmentException e1) {
                e1.printStackTrace();
            }
        }
    }
	attachmentManager.deleteAttachment(attachment);
}

Issue is the issue you want to change the attachment for. mutableIssueObject is the mutable version of that issue (you might not need a mutable issue here, not sure). 

atFile is the file that should be renamed with atFile.rename(). Delete old attachment afterwards.

 

Hope that helps. If so, please accept my answer.

Andris Bērziņš
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 11, 2015

Thanks for the suggestion. I'm gonna accept this answer since it should probably work. But I don't like the approach of downloading & reuploading - it feels rather messy. My solution (and what I ended up doing) is this: Attachment oAttachment = attachmentManager.getAttachment(icAttachmentId); Issue oIssue = oAttachment.getIssueObject(); if(permissionManager.hasPermission(Permissions.CREATE_ATTACHMENT, oIssue, getLoggedInUser())) { OfBizDelegator delegator = ComponentAccessor.getComponentOfType(OfBizDelegator.class); for (GenericValue attachment : delegator.findByField("FileAttachment", "id", icAttachmentId)) { attachment.setString("filename", scFileName); attachment.store(); } } It's been working for about 2 weeks without any problems.

Like Oleh Ivanichok likes this
Sascha Becker February 12, 2015

Nice, thanks for pointing out another solution.

Sadek Joma'a October 19, 2015

Hi, Thanks for the useful code. But I am new to JIRA administration and I need some more help please. My question is: How do I run this code? Should I run it by Script runner post function? and is the code complete or should I include some header statements?

8 votes
Vadim Rutkevich
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 8, 2015

Hello, Andris.

You can try our add-on - Smart Attachments. It allows you to rename attachments, put them into categories, perform bulk operations, load files to categories via drag-n-drop and other features.

If you have any questions about the add-on, feel free to contact me via vrutkevich@stiltsoft.com.

Thanks.

 

Best Regards,

Vadim Rutkevich

Andris Bērziņš
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 11, 2015

Heh, in case you didn't notice - I already linked to your plugin as a proof that it's possible to do this. The problem is that paying over 1000$ for ability to rename attachments is ridiculous. But it doesn't matter now. I added the renaming functionality myself. Thanks for replying though. :)

Vadim Rutkevich
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 11, 2015

Andris, we can give you a very good discount if you decide to purchase our add-on. Please contact me (vrutkevich@stiltsoft.com) to discuss details if you want. I am sure you will be satified with the discount. Besides renaming, you get other useful features for better performance in JIRA. Thanks. Best Regards, Vadim Rutkevich

Andris Bērziņš
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 12, 2015

Thanks for the offer but we are using JIRA 5.1 and your plugin supports 6.0+. So it wouldn't work anyway.

1 vote
Kristina McInerny April 16, 2019

If you're on a Mac - I can help you...tested on Jira Server.

You cannot "rename" one already uploaded, but you can name it upon upload from a screenshot.

1.  Grab a screenshot

Cmd+Ctrl+Shift+4 (select the part to clip) or

Cmd+Ctrl+Shift+3 (selects entire screen)

2.  View Ticket

3.  Cmd+V (pastes from clipboard)

4.  Name it and select Upload.

see screenshot

screenshot rename attachment.png

0 votes
Oleksandr Chalyi February 27, 2018

Vote for suggestion - Add a REST call for renaming attachments

https://jira.atlassian.com/browse/JRASERVER-66851

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events