From 6f011b0fa1d01cd7d3e8424e582c8e5a17a085f1 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Fri, 6 Jul 2018 13:01:09 +0200 Subject: [PATCH] DATAMONGO-2021 - Polishing. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adapt getResources(…) to use the file id and no longer the file name when opening a download stream. Add author tag. Original pull request: #581. --- .../data/mongodb/gridfs/GridFsTemplate.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsTemplate.java index 8d4beeaf3..84b9c4306 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsTemplate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsTemplate.java @@ -51,6 +51,7 @@ import com.mongodb.client.gridfs.model.GridFSUploadOptions; * @author Martin Baumgartner * @author Christoph Strobl * @author Mark Paluch + * @author Niklas Helge Hanft */ public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver { @@ -228,7 +229,7 @@ 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 ? new GridFsResource(file, getGridFs().openDownloadStream(file.getObjectId())) : null; } /* @@ -246,13 +247,13 @@ public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver if (path.isPattern()) { GridFSFindIterable files = find(query(whereFilename().regex(path.toRegex()))); - List resources = new ArrayList(); + List resources = new ArrayList<>(); for (GridFSFile file : files) { - resources.add(new GridFsResource(file, getGridFs().openDownloadStream(file.getFilename()))); + resources.add(new GridFsResource(file, getGridFs().openDownloadStream(file.getObjectId()))); } - return resources.toArray(new GridFsResource[resources.size()]); + return resources.toArray(new GridFsResource[0]); } return new GridFsResource[] { getResource(locationPattern) };