mirror of
https://github.com/sismics/docs.git
synced 2024-11-22 05:57:57 +01:00
Pass file stream directly to GET /file/:id/data
This commit is contained in:
parent
3a2ffec497
commit
c9fdb6def5
@ -1,11 +1,8 @@
|
||||
package com.sismics.docs.rest.resource;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.text.MessageFormat;
|
||||
@ -23,12 +20,10 @@ import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.StreamingOutput;
|
||||
|
||||
import org.codehaus.jettison.json.JSONException;
|
||||
import org.codehaus.jettison.json.JSONObject;
|
||||
|
||||
import com.google.common.io.ByteStreams;
|
||||
import com.sismics.docs.core.dao.jpa.DocumentDao;
|
||||
import com.sismics.docs.core.dao.jpa.FileDao;
|
||||
import com.sismics.docs.core.model.jpa.Document;
|
||||
@ -57,9 +52,10 @@ public class FileResource extends BaseResource {
|
||||
* @throws JSONException
|
||||
*/
|
||||
@GET
|
||||
@Path("{id: [a-z0-9\\-]+}")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public Response get(
|
||||
@QueryParam("id") String id) throws JSONException {
|
||||
@PathParam("id") String id) throws JSONException {
|
||||
if (!authenticate()) {
|
||||
throw new ForbiddenClientException();
|
||||
}
|
||||
@ -241,15 +237,7 @@ public class FileResource extends BaseResource {
|
||||
});
|
||||
final java.io.File storageFile = matchingFiles[0];
|
||||
|
||||
// Stream the file to the response
|
||||
StreamingOutput stream = new StreamingOutput() {
|
||||
@Override
|
||||
public void write(OutputStream os) throws IOException {
|
||||
ByteStreams.copy(new FileInputStream(storageFile), os);
|
||||
}
|
||||
};
|
||||
return Response.ok(stream)
|
||||
.header("Content-Disposition", MessageFormat.format("attachment; filename=\"{0}\"", storageFile.getName()))
|
||||
return Response.ok(storageFile)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
@ -8,10 +8,10 @@ import javax.ws.rs.core.MediaType;
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.codehaus.jettison.json.JSONArray;
|
||||
import org.codehaus.jettison.json.JSONException;
|
||||
import org.codehaus.jettison.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.io.ByteStreams;
|
||||
import com.sismics.docs.rest.filter.CookieAuthenticationFilter;
|
||||
import com.sismics.util.mime.MimeType;
|
||||
import com.sun.jersey.api.client.ClientResponse;
|
||||
@ -30,10 +30,10 @@ public class TestFileResource extends BaseJerseyTest {
|
||||
/**
|
||||
* Test the document resource.
|
||||
*
|
||||
* @throws JSONException
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
public void testFileResource() throws JSONException {
|
||||
public void testFileResource() throws Exception {
|
||||
// Login admin
|
||||
String adminAuthenticationToken = clientUtil.login("admin", "admin", false);
|
||||
|
||||
@ -63,21 +63,28 @@ public class TestFileResource extends BaseJerseyTest {
|
||||
json = response.getEntity(JSONObject.class);
|
||||
String file1Id = json.getString("id");
|
||||
|
||||
// Get a file
|
||||
fileResource = resource().path("/file");
|
||||
// Get the file
|
||||
fileResource = resource().path("/file/" + file1Id);
|
||||
fileResource.addFilter(new CookieAuthenticationFilter(adminAuthenticationToken));
|
||||
MultivaluedMapImpl getParams = new MultivaluedMapImpl();
|
||||
getParams.putSingle("id", file1Id);
|
||||
response = fileResource.queryParams(getParams).get(ClientResponse.class);
|
||||
response = fileResource.get(ClientResponse.class);
|
||||
json = response.getEntity(JSONObject.class);
|
||||
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
|
||||
Assert.assertEquals(MimeType.IMAGE_JPEG, json.getString("mimetype"));
|
||||
Assert.assertEquals(file1Id, json.getString("id"));
|
||||
|
||||
// Get the file data
|
||||
fileResource = resource().path("/file/" + file1Id + "/data");
|
||||
fileResource.addFilter(new CookieAuthenticationFilter(adminAuthenticationToken));
|
||||
response = fileResource.get(ClientResponse.class);
|
||||
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
|
||||
InputStream is = response.getEntityInputStream();
|
||||
byte[] fileBytes = ByteStreams.toByteArray(is);
|
||||
Assert.assertEquals(163510, fileBytes.length);
|
||||
|
||||
// Get all files from a document
|
||||
fileResource = resource().path("/file/list");
|
||||
fileResource.addFilter(new CookieAuthenticationFilter(adminAuthenticationToken));
|
||||
getParams = new MultivaluedMapImpl();
|
||||
MultivaluedMapImpl getParams = new MultivaluedMapImpl();
|
||||
getParams.putSingle("id", document1Id);
|
||||
response = fileResource.queryParams(getParams).get(ClientResponse.class);
|
||||
json = response.getEntity(JSONObject.class);
|
||||
|
Loading…
Reference in New Issue
Block a user