DATAMONGO-2240 - Polishing.

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

Original Pull Request: #741
This commit is contained in:
Mark Paluch
2019-04-16 13:24:57 +02:00
committed by Christoph Strobl
parent 888054bb2a
commit 31f8a63a17
2 changed files with 15 additions and 18 deletions

View File

@@ -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 {
*/
@Override
public String getFilename() throws IllegalStateException {
return filename;
return this.filename;
}
/*
@@ -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 {
public long lastModified() throws IOException {
verifyExists();
return file.getUploadDate().getTime();
return getGridFSFile().getUploadDate().getTime();
}
/*
@@ -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 {
*/
@Nullable
public GridFSFile getGridFSFile() {
return file;
return this.file;
}
/**
@@ -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"));
}

View File

@@ -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;
*/
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 {
public long contentLength() throws IOException {
verifyExists();
return file.getLength();
return getGridFSFile().getLength();
}
/*
@@ -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 {
*/
@Override
public boolean exists() {
return file != null;
return this.file != null;
}
/*
@@ -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 {
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 {
if (!exists()) {
return Flux.error(new FileNotFoundException(String.format("%s does not exist.", getDescription())));
}
return content;
return this.content;
}
private void verifyExists() throws FileNotFoundException {