Skip to content

DATAMONGO-2240 - Expose GridFSFile through GridFsResource and ReactiveGridFsResource #741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2240-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2240-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2240-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2240-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class GridFsResource extends InputStreamResource {

/**
* Creates a new, absent {@link GridFsResource}.
*
*
* @param filename filename of the absent resource.
* @since 2.1
*/
Expand Down Expand Up @@ -116,7 +116,7 @@ public InputStream getInputStream() throws IOException, IllegalStateException {
public long contentLength() throws IOException {

verifyExists();
return file.getLength();
return getGridFSFile().getLength();
}

/*
Expand All @@ -125,7 +125,7 @@ public long contentLength() throws IOException {
*/
@Override
public String getFilename() throws IllegalStateException {
return filename;
return this.filename;
}

/*
Expand All @@ -134,7 +134,7 @@ public String getFilename() throws IllegalStateException {
*/
@Override
public boolean exists() {
return file != null;
return this.file != null;
}

/*
Expand All @@ -145,7 +145,7 @@ public boolean exists() {
public long lastModified() throws IOException {

verifyExists();
return file.getUploadDate().getTime();
return getGridFSFile().getUploadDate().getTime();
}

/*
Expand All @@ -167,7 +167,16 @@ public Object getId() {

Assert.state(exists(), () -> String.format("%s does not exist.", getDescription()));

return file.getId();
return getGridFSFile().getId();
}

/**
* @return the underlying {@link GridFSFile}. Can be {@literal null} if absent.
* @since 2.2
*/
@Nullable
public GridFSFile getGridFSFile() {
return this.file;
}

/**
Expand All @@ -185,8 +194,9 @@ public String getContentType() {

return Optionals
.firstNonEmpty(
() -> Optional.ofNullable(file.getMetadata()).map(it -> it.get(CONTENT_TYPE_FIELD, String.class)),
() -> Optional.ofNullable(file.getContentType()))
() -> Optional.ofNullable(getGridFSFile().getMetadata())
.map(it -> it.get(CONTENT_TYPE_FIELD, String.class)),
() -> Optional.ofNullable(getGridFSFile().getContentType()))
.orElseThrow(() -> new MongoGridFSException("No contentType data for this GridFS file"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

import reactor.core.publisher.Flux;

import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.reactivestreams.Publisher;

import org.springframework.core.io.AbstractResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.buffer.DataBuffer;
Expand All @@ -39,9 +39,6 @@
*/
public class ReactiveGridFsResource extends AbstractResource {

static final String CONTENT_TYPE_FIELD = "_contentType";
private static final ByteArrayInputStream EMPTY_INPUT_STREAM = new ByteArrayInputStream(new byte[0]);

private final @Nullable GridFSFile file;
private final String filename;
private final Flux<DataBuffer> content;
Expand Down Expand Up @@ -104,7 +101,7 @@ public InputStream getInputStream() throws IllegalStateException {
public long contentLength() throws IOException {

verifyExists();
return file.getLength();
return getGridFSFile().getLength();
}

/*
Expand All @@ -113,7 +110,7 @@ public long contentLength() throws IOException {
*/
@Override
public String getFilename() throws IllegalStateException {
return filename;
return this.filename;
}

/*
Expand All @@ -122,7 +119,7 @@ public String getFilename() throws IllegalStateException {
*/
@Override
public boolean exists() {
return file != null;
return this.file != null;
}

/*
Expand All @@ -133,7 +130,7 @@ public boolean exists() {
public long lastModified() throws IOException {

verifyExists();
return file.getUploadDate().getTime();
return getGridFSFile().getUploadDate().getTime();
}

/*
Expand All @@ -155,7 +152,16 @@ public Object getId() {

Assert.state(exists(), () -> String.format("%s does not exist.", getDescription()));

return file.getId();
return getGridFSFile().getId();
}

/**
* @return the underlying {@link GridFSFile}. Can be {@literal null} if absent.
* @since 2.2
*/
@Nullable
public GridFSFile getGridFSFile() {
return file;
}

/**
Expand All @@ -168,7 +174,7 @@ public Flux<DataBuffer> getDownloadStream() {
if (!exists()) {
return Flux.error(new FileNotFoundException(String.format("%s does not exist.", getDescription())));
}
return content;
return this.content;
}

private void verifyExists() throws FileNotFoundException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ public void shouldReadContentTypeCorrectly() {
assertThat(resource.getContentType()).isEqualTo("text/plain");
}

@Test // DATAMONGO-2240
public void shouldReturnGridFSFile() {

GridFSFile file = new GridFSFile(new BsonObjectId(), "foo", 0, 0, new Date(), "foo", new Document());
GridFsResource resource = new GridFsResource(file);

assertThat(resource.getGridFSFile()).isSameAs(file);
}

@Test // DATAMONGO-1850
public void shouldThrowExceptionOnEmptyContentType() {

Expand Down Expand Up @@ -89,6 +98,5 @@ public void shouldReturnFilenameForAbsentResource() {
assertThat(absent.exists()).isFalse();
assertThat(absent.getDescription()).contains("GridFs resource [foo]");
assertThat(absent.getFilename()).isEqualTo("foo");

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
package org.springframework.data.mongodb.gridfs;

import static org.assertj.core.api.Assertions.*;
import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Criteria.*;
import static org.springframework.data.mongodb.core.query.Query.*;
import static org.springframework.data.mongodb.gridfs.GridFsCriteria.*;

import org.springframework.dao.IncorrectResultSizeDataAccessException;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;

Expand All @@ -32,12 +31,14 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.core.io.buffer.DefaultDataBuffer;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
Expand Down Expand Up @@ -143,7 +144,7 @@ public void getResourceShouldRetrieveContentByIdentity() throws IOException {
.verifyComplete();
}

@Test // DATAMONGO-1855
@Test // DATAMONGO-1855, DATAMONGO-2240
public void shouldEmitFirstEntryWhenFindFirstRetrievesMoreThanOneResult() throws IOException {

AsyncInputStream upload1 = AsyncStreamHelper.toAsyncInputStream(resource.getInputStream());
Expand All @@ -155,10 +156,25 @@ public void shouldEmitFirstEntryWhenFindFirstRetrievesMoreThanOneResult() throws
operations.findFirst(query(where("filename").regex("foo*"))) //
.flatMap(operations::getResource) //
.as(StepVerifier::create) //
.expectNextCount(1) //
.assertNext(actual -> {

assertThat(actual.getGridFSFile()).isNotNull();
})
.verifyComplete();
}

@Test // DATAMONGO-2240
public void shouldReturnNoGridFsFileWhenAbsent() {

operations.getResource("absent") //
.as(StepVerifier::create) //
.assertNext(actual -> {

assertThat(actual.exists()).isFalse();
assertThat(actual.getGridFSFile()).isNull();
}).verifyComplete();
}

@Test // DATAMONGO-1855
public void shouldEmitErrorWhenFindOneRetrievesMoreThanOneResult() throws IOException {

Expand Down