Browse Source

DATAMONGO-2240 - Polishing.

Consistently use this for field access. Access GridFSFile through getter.

Original Pull Request: #741
pull/768/head
Mark Paluch 7 years ago committed by Christoph Strobl
parent
commit
31f8a63a17
  1. 17
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsResource.java
  2. 16
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsResource.java

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

@ -116,7 +116,7 @@ public class GridFsResource extends InputStreamResource { @@ -116,7 +116,7 @@ public class GridFsResource extends InputStreamResource {
public long contentLength() throws IOException {
verifyExists();
return file.getLength();
return getGridFSFile().getLength();
}
/*
@ -125,7 +125,7 @@ public class GridFsResource extends InputStreamResource { @@ -125,7 +125,7 @@ public class GridFsResource extends InputStreamResource {
*/
@Override
public String getFilename() throws IllegalStateException {
return filename;
return this.filename;
}
/*
@ -134,7 +134,7 @@ public class GridFsResource extends InputStreamResource { @@ -134,7 +134,7 @@ public class GridFsResource extends InputStreamResource {
*/
@Override
public boolean exists() {
return file != null;
return this.file != null;
}
/*
@ -145,7 +145,7 @@ public class GridFsResource extends InputStreamResource { @@ -145,7 +145,7 @@ public class GridFsResource extends InputStreamResource {
public long lastModified() throws IOException {
verifyExists();
return file.getUploadDate().getTime();
return getGridFSFile().getUploadDate().getTime();
}
/*
@ -167,7 +167,7 @@ public class GridFsResource extends InputStreamResource { @@ -167,7 +167,7 @@ public class GridFsResource extends InputStreamResource {
Assert.state(exists(), () -> String.format("%s does not exist.", getDescription()));
return file.getId();
return getGridFSFile().getId();
}
/**
@ -176,7 +176,7 @@ public class GridFsResource extends InputStreamResource { @@ -176,7 +176,7 @@ public class GridFsResource extends InputStreamResource {
*/
@Nullable
public GridFSFile getGridFSFile() {
return file;
return this.file;
}
/**
@ -194,8 +194,9 @@ public class GridFsResource extends InputStreamResource { @@ -194,8 +194,9 @@ public class GridFsResource extends InputStreamResource {
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"));
}

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

@ -17,7 +17,6 @@ package org.springframework.data.mongodb.gridfs; @@ -17,7 +17,6 @@ package org.springframework.data.mongodb.gridfs;
import reactor.core.publisher.Flux;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
@ -40,9 +39,6 @@ import com.mongodb.client.gridfs.model.GridFSFile; @@ -40,9 +39,6 @@ import com.mongodb.client.gridfs.model.GridFSFile;
*/
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;
@ -105,7 +101,7 @@ public class ReactiveGridFsResource extends AbstractResource { @@ -105,7 +101,7 @@ public class ReactiveGridFsResource extends AbstractResource {
public long contentLength() throws IOException {
verifyExists();
return file.getLength();
return getGridFSFile().getLength();
}
/*
@ -114,7 +110,7 @@ public class ReactiveGridFsResource extends AbstractResource { @@ -114,7 +110,7 @@ public class ReactiveGridFsResource extends AbstractResource {
*/
@Override
public String getFilename() throws IllegalStateException {
return filename;
return this.filename;
}
/*
@ -123,7 +119,7 @@ public class ReactiveGridFsResource extends AbstractResource { @@ -123,7 +119,7 @@ public class ReactiveGridFsResource extends AbstractResource {
*/
@Override
public boolean exists() {
return file != null;
return this.file != null;
}
/*
@ -134,7 +130,7 @@ public class ReactiveGridFsResource extends AbstractResource { @@ -134,7 +130,7 @@ public class ReactiveGridFsResource extends AbstractResource {
public long lastModified() throws IOException {
verifyExists();
return file.getUploadDate().getTime();
return getGridFSFile().getUploadDate().getTime();
}
/*
@ -156,7 +152,7 @@ public class ReactiveGridFsResource extends AbstractResource { @@ -156,7 +152,7 @@ public class ReactiveGridFsResource extends AbstractResource {
Assert.state(exists(), () -> String.format("%s does not exist.", getDescription()));
return file.getId();
return getGridFSFile().getId();
}
/**
@ -178,7 +174,7 @@ public class ReactiveGridFsResource extends AbstractResource { @@ -178,7 +174,7 @@ public class ReactiveGridFsResource extends AbstractResource {
if (!exists()) {
return Flux.error(new FileNotFoundException(String.format("%s does not exist.", getDescription())));
}
return content;
return this.content;
}
private void verifyExists() throws FileNotFoundException {

Loading…
Cancel
Save