Skip to content

Commit 31f8a63

Browse files
mp911dechristophstrobl
authored andcommitted
DATAMONGO-2240 - Polishing.
Consistently use this for field access. Access GridFSFile through getter. Original Pull Request: #741
1 parent 888054b commit 31f8a63

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

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

+9-8
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public InputStream getInputStream() throws IOException, IllegalStateException {
116116
public long contentLength() throws IOException {
117117

118118
verifyExists();
119-
return file.getLength();
119+
return getGridFSFile().getLength();
120120
}
121121

122122
/*
@@ -125,7 +125,7 @@ public long contentLength() throws IOException {
125125
*/
126126
@Override
127127
public String getFilename() throws IllegalStateException {
128-
return filename;
128+
return this.filename;
129129
}
130130

131131
/*
@@ -134,7 +134,7 @@ public String getFilename() throws IllegalStateException {
134134
*/
135135
@Override
136136
public boolean exists() {
137-
return file != null;
137+
return this.file != null;
138138
}
139139

140140
/*
@@ -145,7 +145,7 @@ public boolean exists() {
145145
public long lastModified() throws IOException {
146146

147147
verifyExists();
148-
return file.getUploadDate().getTime();
148+
return getGridFSFile().getUploadDate().getTime();
149149
}
150150

151151
/*
@@ -167,7 +167,7 @@ public Object getId() {
167167

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

170-
return file.getId();
170+
return getGridFSFile().getId();
171171
}
172172

173173
/**
@@ -176,7 +176,7 @@ public Object getId() {
176176
*/
177177
@Nullable
178178
public GridFSFile getGridFSFile() {
179-
return file;
179+
return this.file;
180180
}
181181

182182
/**
@@ -194,8 +194,9 @@ public String getContentType() {
194194

195195
return Optionals
196196
.firstNonEmpty(
197-
() -> Optional.ofNullable(file.getMetadata()).map(it -> it.get(CONTENT_TYPE_FIELD, String.class)),
198-
() -> Optional.ofNullable(file.getContentType()))
197+
() -> Optional.ofNullable(getGridFSFile().getMetadata())
198+
.map(it -> it.get(CONTENT_TYPE_FIELD, String.class)),
199+
() -> Optional.ofNullable(getGridFSFile().getContentType()))
199200
.orElseThrow(() -> new MongoGridFSException("No contentType data for this GridFS file"));
200201
}
201202

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

+6-10
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import reactor.core.publisher.Flux;
1919

20-
import java.io.ByteArrayInputStream;
2120
import java.io.FileNotFoundException;
2221
import java.io.IOException;
2322
import java.io.InputStream;
@@ -40,9 +39,6 @@
4039
*/
4140
public class ReactiveGridFsResource extends AbstractResource {
4241

43-
static final String CONTENT_TYPE_FIELD = "_contentType";
44-
private static final ByteArrayInputStream EMPTY_INPUT_STREAM = new ByteArrayInputStream(new byte[0]);
45-
4642
private final @Nullable GridFSFile file;
4743
private final String filename;
4844
private final Flux<DataBuffer> content;
@@ -105,7 +101,7 @@ public InputStream getInputStream() throws IllegalStateException {
105101
public long contentLength() throws IOException {
106102

107103
verifyExists();
108-
return file.getLength();
104+
return getGridFSFile().getLength();
109105
}
110106

111107
/*
@@ -114,7 +110,7 @@ public long contentLength() throws IOException {
114110
*/
115111
@Override
116112
public String getFilename() throws IllegalStateException {
117-
return filename;
113+
return this.filename;
118114
}
119115

120116
/*
@@ -123,7 +119,7 @@ public String getFilename() throws IllegalStateException {
123119
*/
124120
@Override
125121
public boolean exists() {
126-
return file != null;
122+
return this.file != null;
127123
}
128124

129125
/*
@@ -134,7 +130,7 @@ public boolean exists() {
134130
public long lastModified() throws IOException {
135131

136132
verifyExists();
137-
return file.getUploadDate().getTime();
133+
return getGridFSFile().getUploadDate().getTime();
138134
}
139135

140136
/*
@@ -156,7 +152,7 @@ public Object getId() {
156152

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

159-
return file.getId();
155+
return getGridFSFile().getId();
160156
}
161157

162158
/**
@@ -178,7 +174,7 @@ public Flux<DataBuffer> getDownloadStream() {
178174
if (!exists()) {
179175
return Flux.error(new FileNotFoundException(String.format("%s does not exist.", getDescription())));
180176
}
181-
return content;
177+
return this.content;
182178
}
183179

184180
private void verifyExists() throws FileNotFoundException {

0 commit comments

Comments
 (0)