Skip to content

Commit 4fa09d8

Browse files
committed
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().
1 parent bb84b92 commit 4fa09d8

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsCriteria.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,6 @@ public static GridFsCriteria whereFilename() {
6969
* @return
7070
*/
7171
public static GridFsCriteria whereContentType() {
72-
return new GridFsCriteria("metadata.type");
72+
return new GridFsCriteria("metadata.".concat(GridFsResource.CONTENT_TYPE_FIELD));
7373
}
7474
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsResource.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
*/
3434
public class GridFsResource extends InputStreamResource {
3535

36+
static final String CONTENT_TYPE_FIELD = "_contentType";
37+
3638
private final GridFSFile file;
3739

3840
/**
@@ -91,7 +93,11 @@ public Object getId() {
9193
*
9294
* @return
9395
*/
96+
@SuppressWarnings("deprecation")
9497
public String getContentType() {
95-
return file.getContentType();
98+
99+
String contentType = file.getMetadata().get(CONTENT_TYPE_FIELD, String.class);
100+
101+
return contentType != null ? contentType : file.getContentType();
96102
}
97103
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsTemplate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public ObjectId store(InputStream content, String filename, String contentType,
166166

167167
Document mData = new Document();
168168
if (StringUtils.hasText(contentType)) {
169-
mData.put("type", contentType);
169+
mData.put(GridFsResource.CONTENT_TYPE_FIELD, contentType);
170170
}
171171

172172
if (metadata != null) {

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/GridFsTemplateIntegrationTests.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
*/
1616
package org.springframework.data.mongodb.gridfs;
1717

18+
import static org.assertj.core.api.Assertions.assertThat;
1819
import static org.hamcrest.Matchers.*;
1920
import static org.junit.Assert.*;
21+
import static org.junit.Assert.assertThat;
2022
import static org.springframework.data.mongodb.core.query.Criteria.*;
2123
import static org.springframework.data.mongodb.core.query.Query.*;
2224
import static org.springframework.data.mongodb.gridfs.GridFsCriteria.*;
@@ -41,7 +43,6 @@
4143
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
4244

4345
import com.mongodb.client.gridfs.GridFSFindIterable;
44-
import com.mongodb.gridfs.GridFSFile;
4546

4647
/**
4748
* Integration tests for {@link GridFsTemplate}.
@@ -210,15 +211,15 @@ public void storesAndFindsSimpleDocumentWithMetadataObject() throws IOException
210211
assertEquals(((BsonObjectId) files.get(0).getId()).getValue(), reference);
211212
}
212213

213-
private static void assertSame(GridFSFile left, GridFSFile right) {
214+
@Test // DATAMONGO-1695
215+
public void readsContentTypeCorrectly() throws IOException {
214216

215-
assertThat(left.getId(), is(right.getId()));
216-
assertThat(left.getMD5(), is(right.getMD5()));
217-
assertThat(left.getMetaData(), is(right.getMetaData()));
217+
operations.store(resource.getInputStream(), "someName", "contentType");
218+
219+
assertThat(operations.getResource("someName").getContentType()).isEqualTo("contentType");
218220
}
219221

220222
class Metadata {
221-
222223
String version;
223224
}
224225
}

0 commit comments

Comments
 (0)