DATAMONGO-1813 - Add GridFsOperations.getResource(GridFSFile).
We now provide GridFsOperations.getResource(GridFSFile) to create GridFsResource without a database lookup. This allows direct creation of GridFsResource for GridFSFile. Original pull request: #543.
This commit is contained in:
committed by
Mark Paluch
parent
1d7cc2eb97
commit
aab86d23c9
@@ -34,6 +34,7 @@ import com.mongodb.client.gridfs.GridFSFindIterable;
|
||||
* @author Thomas Darimont
|
||||
* @author Martin Baumgartner
|
||||
* @author Christoph Strobl
|
||||
* @author Hartmut Lang
|
||||
*/
|
||||
public interface GridFsOperations extends ResourcePatternResolver {
|
||||
|
||||
@@ -157,6 +158,13 @@ public interface GridFsOperations extends ResourcePatternResolver {
|
||||
*/
|
||||
GridFsResource getResource(String filename);
|
||||
|
||||
/**
|
||||
* Returns the {@link GridFsResource} for a {@link com.mongodb.client.gridfs.model.GridFSFile}.
|
||||
* @param file must not be {@literal null}.
|
||||
* @return the resource for the file.
|
||||
*/
|
||||
GridFsResource getResource(com.mongodb.client.gridfs.model.GridFSFile file);
|
||||
|
||||
/**
|
||||
* Returns all {@link GridFsResource}s matching the given file name pattern.
|
||||
*
|
||||
|
||||
@@ -51,6 +51,7 @@ import com.mongodb.client.gridfs.model.GridFSUploadOptions;
|
||||
* @author Martin Baumgartner
|
||||
* @author Christoph Strobl
|
||||
* @author Mark Paluch
|
||||
* @author Hartmut Lang
|
||||
*/
|
||||
public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver {
|
||||
|
||||
@@ -228,13 +229,22 @@ public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver
|
||||
public GridFsResource getResource(String location) {
|
||||
|
||||
GridFSFile file = findOne(query(whereFilename().is(location)));
|
||||
return file != null ? new GridFsResource(file, getGridFs().openDownloadStream(location)) : null;
|
||||
return file != null ? getResource(file) : null;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.core.io.support.ResourcePatternResolver#getResources(java.lang.String)
|
||||
* @see org.springframework.data.mongodb.gridfs.GridFsOperations#getResource(com.mongodb.client.gridfs.model.GridFSFile)
|
||||
*/
|
||||
public GridFsResource getResource(GridFSFile file) {
|
||||
|
||||
return new GridFsResource(file, getGridFs().openDownloadStream(file.getFilename()));
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.core.io.support.ResourcePatternResolver#getResources(java.lang.String)
|
||||
*/
|
||||
public GridFsResource[] getResources(String locationPattern) {
|
||||
|
||||
if (!StringUtils.hasText(locationPattern)) {
|
||||
|
||||
@@ -42,6 +42,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.mongodb.MongoGridFSException;
|
||||
import com.mongodb.client.gridfs.GridFSFindIterable;
|
||||
import com.mongodb.client.gridfs.model.GridFSFile;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link GridFsTemplate}.
|
||||
@@ -50,6 +51,7 @@ import com.mongodb.client.gridfs.GridFSFindIterable;
|
||||
* @author Philipp Schneider
|
||||
* @author Thomas Darimont
|
||||
* @author Martin Baumgartner
|
||||
* @author Hartmut Lang
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration("classpath:gridfs/gridfs.xml")
|
||||
@@ -225,6 +227,20 @@ public class GridFsTemplateIntegrationTests {
|
||||
assertThatThrownBy(() -> result.getContentType()).isInstanceOf(MongoGridFSException.class);
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-1813
|
||||
public void convertFileToResource() throws IOException {
|
||||
|
||||
Document metadata = new Document("key", "value");
|
||||
ObjectId reference = operations.store(resource.getInputStream(), "foobar", metadata);
|
||||
|
||||
List<com.mongodb.client.gridfs.model.GridFSFile> files = new ArrayList<com.mongodb.client.gridfs.model.GridFSFile>();
|
||||
GridFSFile file = operations.findOne(query(whereMetaData("key").is("value")));
|
||||
GridFsResource result = operations.getResource(file);
|
||||
|
||||
assertThat(result.contentLength()).isEqualTo(resource.contentLength());
|
||||
assertThat(((BsonObjectId) result.getId()).getValue()).isEqualTo(reference);
|
||||
}
|
||||
|
||||
class Metadata {
|
||||
String version;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user