DATAMONGO-1695 - Make sure we read GridFs content type from the same field we write it to.
We now consistently store the content type of a file in _contentType in the metadata document. On the lookup side we still fall back to the deprecated file.getContentType().
This commit is contained in:
@@ -69,6 +69,6 @@ public class GridFsCriteria extends Criteria {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static GridFsCriteria whereContentType() {
|
public static GridFsCriteria whereContentType() {
|
||||||
return new GridFsCriteria("metadata.type");
|
return new GridFsCriteria("metadata.".concat(GridFsResource.CONTENT_TYPE_FIELD));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,6 +33,8 @@ import com.mongodb.gridfs.GridFSDBFile;
|
|||||||
*/
|
*/
|
||||||
public class GridFsResource extends InputStreamResource {
|
public class GridFsResource extends InputStreamResource {
|
||||||
|
|
||||||
|
static final String CONTENT_TYPE_FIELD = "_contentType";
|
||||||
|
|
||||||
private final GridFSFile file;
|
private final GridFSFile file;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -91,7 +93,11 @@ public class GridFsResource extends InputStreamResource {
|
|||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
public String getContentType() {
|
public String getContentType() {
|
||||||
return file.getContentType();
|
|
||||||
|
String contentType = file.getMetadata().get(CONTENT_TYPE_FIELD, String.class);
|
||||||
|
|
||||||
|
return contentType != null ? contentType : file.getContentType();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -166,7 +166,7 @@ public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver
|
|||||||
|
|
||||||
Document mData = new Document();
|
Document mData = new Document();
|
||||||
if (StringUtils.hasText(contentType)) {
|
if (StringUtils.hasText(contentType)) {
|
||||||
mData.put("type", contentType);
|
mData.put(GridFsResource.CONTENT_TYPE_FIELD, contentType);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (metadata != null) {
|
if (metadata != null) {
|
||||||
|
|||||||
@@ -15,8 +15,10 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.data.mongodb.gridfs;
|
package org.springframework.data.mongodb.gridfs;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.hamcrest.Matchers.*;
|
import static org.hamcrest.Matchers.*;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
import static org.springframework.data.mongodb.core.query.Criteria.*;
|
import static org.springframework.data.mongodb.core.query.Criteria.*;
|
||||||
import static org.springframework.data.mongodb.core.query.Query.*;
|
import static org.springframework.data.mongodb.core.query.Query.*;
|
||||||
import static org.springframework.data.mongodb.gridfs.GridFsCriteria.*;
|
import static org.springframework.data.mongodb.gridfs.GridFsCriteria.*;
|
||||||
@@ -41,7 +43,6 @@ import org.springframework.test.context.ContextConfiguration;
|
|||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
import com.mongodb.client.gridfs.GridFSFindIterable;
|
import com.mongodb.client.gridfs.GridFSFindIterable;
|
||||||
import com.mongodb.gridfs.GridFSFile;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Integration tests for {@link GridFsTemplate}.
|
* Integration tests for {@link GridFsTemplate}.
|
||||||
@@ -210,15 +211,15 @@ public class GridFsTemplateIntegrationTests {
|
|||||||
assertEquals(((BsonObjectId) files.get(0).getId()).getValue(), reference);
|
assertEquals(((BsonObjectId) files.get(0).getId()).getValue(), reference);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void assertSame(GridFSFile left, GridFSFile right) {
|
@Test // DATAMONGO-1695
|
||||||
|
public void readsContentTypeCorrectly() throws IOException {
|
||||||
|
|
||||||
assertThat(left.getId(), is(right.getId()));
|
operations.store(resource.getInputStream(), "someName", "contentType");
|
||||||
assertThat(left.getMD5(), is(right.getMD5()));
|
|
||||||
assertThat(left.getMetaData(), is(right.getMetaData()));
|
assertThat(operations.getResource("someName").getContentType()).isEqualTo("contentType");
|
||||||
}
|
}
|
||||||
|
|
||||||
class Metadata {
|
class Metadata {
|
||||||
|
|
||||||
String version;
|
String version;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user