Skip to content
Snippets Groups Projects
Commit 417aff44 authored by srosse's avatar srosse
Browse files

OMA-54: return a real mime type with the download of forum's attachments

parent e37fb05b
No related branches found
No related tags found
No related merge requests found
...@@ -64,6 +64,7 @@ import org.olat.core.logging.OLog; ...@@ -64,6 +64,7 @@ import org.olat.core.logging.OLog;
import org.olat.core.logging.Tracing; import org.olat.core.logging.Tracing;
import org.olat.core.util.FileUtils; import org.olat.core.util.FileUtils;
import org.olat.core.util.StringHelper; import org.olat.core.util.StringHelper;
import org.olat.core.util.WebappHelper;
import org.olat.core.util.vfs.LocalFileImpl; import org.olat.core.util.vfs.LocalFileImpl;
import org.olat.core.util.vfs.VFSContainer; import org.olat.core.util.vfs.VFSContainer;
import org.olat.core.util.vfs.VFSItem; import org.olat.core.util.vfs.VFSItem;
...@@ -455,7 +456,9 @@ public class ForumWebService { ...@@ -455,7 +456,9 @@ public class ForumWebService {
Response.ResponseBuilder response = request.evaluatePreconditions(lastModified); Response.ResponseBuilder response = request.evaluatePreconditions(lastModified);
if(response == null) { if(response == null) {
File attachment = ((LocalFileImpl)item).getBasefile(); File attachment = ((LocalFileImpl)item).getBasefile();
response = Response.ok(attachment).lastModified(lastModified).cacheControl(cc); String mimeType = WebappHelper.getMimeType(attachment.getName());
if (mimeType == null) mimeType = "application/octet-stream";
response = Response.ok(attachment).lastModified(lastModified).type(mimeType).cacheControl(cc);
} }
return response.build(); return response.build();
} else if (item instanceof VFSLeaf) { } else if (item instanceof VFSLeaf) {
...@@ -464,7 +467,9 @@ public class ForumWebService { ...@@ -464,7 +467,9 @@ public class ForumWebService {
Response.ResponseBuilder response = request.evaluatePreconditions(lastModified); Response.ResponseBuilder response = request.evaluatePreconditions(lastModified);
if(response == null) { if(response == null) {
StreamingOutput attachment = new VFSStreamingOutput((VFSLeaf)item); StreamingOutput attachment = new VFSStreamingOutput((VFSLeaf)item);
response = Response.ok(attachment).lastModified(lastModified).cacheControl(cc); String mimeType = WebappHelper.getMimeType(item.getName());
if (mimeType == null) mimeType = "application/octet-stream";
response = Response.ok(attachment).lastModified(lastModified).type(mimeType).cacheControl(cc);
} }
return response.build(); return response.build();
} }
......
...@@ -39,6 +39,7 @@ import java.net.URI; ...@@ -39,6 +39,7 @@ import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.util.List; import java.util.List;
import java.util.UUID;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
...@@ -56,6 +57,8 @@ import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; ...@@ -56,6 +57,8 @@ import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part; import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart; import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference; import org.codehaus.jackson.type.TypeReference;
import org.junit.Before; import org.junit.Before;
...@@ -76,6 +79,7 @@ import org.olat.restapi.support.vo.File64VO; ...@@ -76,6 +79,7 @@ import org.olat.restapi.support.vo.File64VO;
import org.olat.restapi.support.vo.FileVO; import org.olat.restapi.support.vo.FileVO;
import org.olat.test.JunitTestHelper; import org.olat.test.JunitTestHelper;
import org.olat.test.OlatJerseyTestCase; import org.olat.test.OlatJerseyTestCase;
import org.springframework.beans.factory.annotation.Autowired;
public class ForumTest extends OlatJerseyTestCase { public class ForumTest extends OlatJerseyTestCase {
...@@ -83,6 +87,9 @@ public class ForumTest extends OlatJerseyTestCase { ...@@ -83,6 +87,9 @@ public class ForumTest extends OlatJerseyTestCase {
private static Message m1, m2, m3, m4 ,m5; private static Message m1, m2, m3, m4 ,m5;
private static Identity id1; private static Identity id1;
@Autowired
private ForumManager forumManager;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
...@@ -248,15 +255,39 @@ public class ForumTest extends OlatJerseyTestCase { ...@@ -248,15 +255,39 @@ public class ForumTest extends OlatJerseyTestCase {
@Test @Test
public void testGetAttachment() throws IOException, URISyntaxException { public void testGetAttachment() throws IOException, URISyntaxException {
HttpClient c = loginWithCookie("administrator", "openolat"); //set a attachment
VFSContainer container = forumManager.getMessageContainer(m1.getForum().getKey(), m1.getKey());
InputStream portraitIn = CoursesElementsTest.class.getResourceAsStream("portrait.jpg");
assertNotNull(portraitIn);
VFSLeaf attachment = container.createChildLeaf(UUID.randomUUID().toString().replace("-", "") + ".jpg");
FileUtils.bcopy(portraitIn, attachment.getOutputStream(false), "");
RestConnection conn = new RestConnection();
assertTrue(conn.login("administrator", "openolat"));
URI uri = getForumUriBuilder().path("posts").path(m1.getKey().toString()).path("attachments").build(); URI uri = getForumUriBuilder().path("posts").path(m1.getKey().toString()).path("attachments").build();
GetMethod method = createGet(uri, MediaType.APPLICATION_JSON, true); HttpGet method = conn.createGet(uri, MediaType.APPLICATION_JSON, true);
int code = c.executeMethod(method); HttpResponse response = conn.execute(method);
assertEquals(200, code); assertEquals(200, response.getStatusLine().getStatusCode());
InputStream body = method.getResponseBodyAsStream(); List<FileVO> files = parseFileArray(response.getEntity().getContent());
List<FileVO> files = parseFileArray(body);
assertNotNull(files); assertNotNull(files);
FileVO attachmentVO = null;
for(FileVO file:files) {
if(attachment.getName().equals(file.getTitle())) {
attachmentVO = file;
}
}
assertNotNull(attachmentVO);
URI downloadURI = new URI(attachmentVO.getHref());
HttpGet download = conn.createGet(downloadURI, MediaType.APPLICATION_JSON, true);
HttpResponse downloadResponse = conn.execute(download);
assertEquals(200, downloadResponse.getStatusLine().getStatusCode());
String contentType = downloadResponse.getEntity().getContentType().getValue();
assertEquals("image/jpeg", contentType);
} }
@Test @Test
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment