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

Extract content type from uploaded file

Romain Pouclet April 30, 2014

Hi,

I'm using atlassian-rest-common to upload a file from my confluence plugin, so this is what I have:

public Response uploadAttachment(@MultipartFormParam("file") FilePart filePart) {

// ...

}
The goal of this service is to temporary store a file in Confluence's temp folder and attach it later to a content. For now, these attachments are only images. I'd like to retrieve the mime type of this file, ideally later, from a File or an InputStream. Is there something in Confluence I could use to do that? I'm this close form adding Apache Tika in my plugin but that's a 20Mb dependency I could easily live without...
Thanks!
Romain

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted
Joe Clark
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 30, 2014

There is a getContentType() method on the FilePart parameter that will return the MIME type of the uploaded file. Here's an example REST resource to demonstrate:

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.atlassian.plugins.rest.common.multipart.FilePart;
import com.atlassian.plugins.rest.common.multipart.MultipartFormParam;

@Path ("/test")
public class TestResource
{
    @POST
    @Path("/file")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.TEXT_PLAIN)
    public Response doStuffWithFile(@MultipartFormParam ("file") FilePart file)
    {
        return Response.ok("You uploaded a " + file.getContentType() + " file").build();
    }
}

And the HTML form that submits to it:

<form method="POST" action="$req.contextPath/rest/module/1.0/test/file" enctype="multipart/form-data">
	<div class="field-group">
		<label for="file">File</label>
		<input class="file" type="file" name="file" id="file"/>
	</div>
	<input type="submit" class="submit" value="submit"/>
</form>

Romain Pouclet April 30, 2014

Damn, the documentation that pops up in google when you look for com.atlassian.plugins.rest.common.multipart.FilePart is https://docs.atlassian.com/atlassian-rest/1.0.0/atlassian-rest-common/apidocs/com/atlassian/plugins/rest/common/multipart/FilePart.htmlso I didn't bother, sorry :<

Thanks for the quick answer thought!

Joe Clark
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 1, 2014

Ah, that's an easy mistake to make! Some of the docs are on docs.atlassian.com and other docs are on developer.atlassian.com/static. It looks like the newer versions of atlassian-rest-common docs are on the latter.

It looks like the getContentType method was added in version 2.x - https://developer.atlassian.com/static/javadoc/rest/2.5/reference/com/atlassian/plugins/rest/common/multipart/FilePart.html

TAGS
AUG Leaders

Atlassian Community Events