DATAMONGO-809 - Filename is now optional when storing files to GridFS.
Added method overloads to GridFsOperations and GridFsTemplate to store files without a filename given. Original pull request: #119.
This commit is contained in:
committed by
Oliver Gierke
parent
d27bec8ed5
commit
753e794194
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2013 the original author or authors.
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -32,6 +32,7 @@ import com.mongodb.gridfs.GridFSFile;
|
||||
* @author Oliver Gierke
|
||||
* @author Philipp Schneider
|
||||
* @author Thomas Darimont
|
||||
* @author Martin Baumgartner
|
||||
*/
|
||||
public interface GridFsOperations extends ResourcePatternResolver {
|
||||
|
||||
@@ -44,6 +45,24 @@ public interface GridFsOperations extends ResourcePatternResolver {
|
||||
*/
|
||||
GridFSFile store(InputStream content, String filename);
|
||||
|
||||
/**
|
||||
* Stores the given content into a file with the given name.
|
||||
*
|
||||
* @param content must not be {@literal null}.
|
||||
* @param metadata can be {@literal null}.
|
||||
* @return the {@link GridFSFile} just created
|
||||
*/
|
||||
GridFSFile store(InputStream content, Object metadata);
|
||||
|
||||
/**
|
||||
* Stores the given content into a file with the given name.
|
||||
*
|
||||
* @param content must not be {@literal null}.
|
||||
* @param metadata can be {@literal null}.
|
||||
* @return the {@link GridFSFile} just created
|
||||
*/
|
||||
GridFSFile store(InputStream content, DBObject metadata);
|
||||
|
||||
/**
|
||||
* Stores the given content into a file with the given name and content type.
|
||||
*
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2013 the original author or authors.
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -44,6 +44,7 @@ import com.mongodb.gridfs.GridFSInputFile;
|
||||
* @author Oliver Gierke
|
||||
* @author Philipp Schneider
|
||||
* @author Thomas Darimont
|
||||
* @author Martin Baumgartner
|
||||
*/
|
||||
public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver {
|
||||
|
||||
@@ -89,6 +90,25 @@ public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver
|
||||
return store(content, filename, (Object) null);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.gridfs.GridFsOperations#store(java.io.InputStream, java.lang.Object)
|
||||
*/
|
||||
|
||||
@Override
|
||||
public GridFSFile store(InputStream content, Object metadata) {
|
||||
return store(content, null, metadata);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.gridfs.GridFsOperations#store(java.io.InputStream, com.mongodb.DBObject)
|
||||
*/
|
||||
@Override
|
||||
public GridFSFile store(InputStream content, DBObject metadata) {
|
||||
return store(content, null, metadata);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.gridfs.GridFsOperations#store(java.io.InputStream, java.lang.String, java.lang.String)
|
||||
@@ -102,7 +122,6 @@ public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver
|
||||
* @see org.springframework.data.mongodb.gridfs.GridFsOperations#store(java.io.InputStream, java.lang.String, java.lang.Object)
|
||||
*/
|
||||
public GridFSFile store(InputStream content, String filename, Object metadata) {
|
||||
|
||||
return store(content, filename, null, metadata);
|
||||
}
|
||||
|
||||
@@ -137,10 +156,12 @@ public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver
|
||||
public GridFSFile store(InputStream content, String filename, String contentType, DBObject metadata) {
|
||||
|
||||
Assert.notNull(content);
|
||||
Assert.hasText(filename);
|
||||
|
||||
GridFSInputFile file = getGridFs().createFile(content);
|
||||
file.setFilename(filename);
|
||||
|
||||
if (filename != null) {
|
||||
file.setFilename(filename);
|
||||
}
|
||||
|
||||
if (metadata != null) {
|
||||
file.setMetaData(metadata);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2013 the original author or authors.
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -46,6 +46,7 @@ import com.mongodb.gridfs.GridFSFile;
|
||||
* @author Oliver Gierke
|
||||
* @author Philipp Schneider
|
||||
* @author Thomas Darimont
|
||||
* @author Martin Baumgartner
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration("classpath:gridfs/gridfs.xml")
|
||||
@@ -171,6 +172,37 @@ public class GridFsTemplateIntegrationTests {
|
||||
assertThat(operations.getResource("doesnotexist"), is(nullValue()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-809
|
||||
*/
|
||||
@Test
|
||||
public void storesAndFindsSimpleDocumentWithMetadataDBObject() throws IOException {
|
||||
|
||||
DBObject metadata = new BasicDBObject("key", "value");
|
||||
GridFSFile reference = operations.store(resource.getInputStream(), metadata);
|
||||
|
||||
List<GridFSDBFile> result = operations.find(query(whereMetaData("key").is("value")));
|
||||
|
||||
assertThat(result.size(), is(1));
|
||||
assertSame(result.get(0), reference);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-809
|
||||
*/
|
||||
@Test
|
||||
public void storesAndFindsSimpleDocumentWithMetadataObject() throws IOException {
|
||||
|
||||
Metadata metadata = new Metadata();
|
||||
metadata.version = "1.0";
|
||||
GridFSFile reference = operations.store(resource.getInputStream(), metadata);
|
||||
|
||||
List<GridFSDBFile> result = operations.find(query(whereMetaData("version").is("1.0")));
|
||||
|
||||
assertThat(result.size(), is(1));
|
||||
assertSame(result.get(0), reference);
|
||||
}
|
||||
|
||||
private static void assertSame(GridFSFile left, GridFSFile right) {
|
||||
|
||||
assertThat(left.getId(), is(right.getId()));
|
||||
|
||||
Reference in New Issue
Block a user