Skip to content

Commit b00f2aa

Browse files
committed
opt-in for thumbnail storage on s3 instead of disk
i have missed that cloudflare dont cache the images as long as i originally thought
1 parent 88dda1d commit b00f2aa

File tree

11 files changed

+496
-143
lines changed

11 files changed

+496
-143
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
/target/classes/static/**
66
/src/test/javascript/coverage/
77

8+
# Bon
9+
/src/main/resources/config/application-dev-*.yml
10+
811
######################
912
# Node
1013
######################

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
<spring-cloud-contract.version>2.2.2.RELEASE</spring-cloud-contract.version>
9999
<net.coobird.thumbnailator.version>0.4.11</net.coobird.thumbnailator.version>
100100
<org.apache.tika.version>1.24.1</org.apache.tika.version>
101+
<aws-java-sdk-bom.version>1.12.32</aws-java-sdk-bom.version>
101102
</properties>
102103

103104
<dependencyManagement>
@@ -112,6 +113,13 @@
112113
<!-- jhipster-needle-maven-add-dependency-management -->
113114

114115
<!-- Bon -->
116+
<dependency>
117+
<groupId>com.amazonaws</groupId>
118+
<artifactId>aws-java-sdk-bom</artifactId>
119+
<version>${aws-java-sdk-bom.version}</version>
120+
<type>pom</type>
121+
<scope>import</scope>
122+
</dependency>
115123
<dependency>
116124
<groupId>com.graphql-java-kickstart</groupId>
117125
<artifactId>graphql-spring-boot-starter</artifactId>
@@ -432,6 +440,10 @@
432440
<!-- jhipster-needle-maven-add-dependency -->
433441

434442
<!-- Bon -->
443+
<dependency>
444+
<groupId>com.amazonaws</groupId>
445+
<artifactId>aws-java-sdk-s3</artifactId>
446+
</dependency>
435447
<dependency>
436448
<groupId>com.graphql-java-kickstart</groupId>
437449
<artifactId>graphql-spring-boot-starter</artifactId>

src/main/java/com/bonlimousin/gateway/bff/delegate/CowVOResourceDelegateImpl.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@
4646
import org.zalando.problem.Status;
4747

4848
import javax.validation.ValidationException;
49-
import java.io.FileInputStream;
5049
import java.io.IOException;
51-
import java.nio.file.Path;
50+
import java.io.InputStream;
5251
import java.text.MessageFormat;
5352
import java.time.Duration;
5453
import java.time.OffsetDateTime;
@@ -223,21 +222,25 @@ public ResponseEntity<List<PictureVO>> getAllPictureVOsByCow(Long earTagId, Inte
223222
ResponseEntity<List<PhotoEntity>> response = fetchPhotosByEarTagId(earTagId, page, size, sort);
224223
List<PictureVO> list = new ArrayList<>();
225224
for (PhotoEntity entity : response.getBody()) {
226-
PictureVO vo = PictureVOMapper.INSTANCE.photoEntityToPictureVO(entity);
227-
String baseUrl = getCowImageBaseUrl(earTagId, entity.getId());
228-
try {
229-
Map<PictureSize, PictureSourceVO> map = cowPictureSourceService.createPictureSourceVOs(entity, baseUrl);
230-
vo.setSources(new ArrayList<>(map.values()));
231-
cowPictureSourceService.asyncCreateThumbnailsOnDiskIfNotExists(entity.getId(), map);
232-
} catch (MimeTypeException e) {
233-
log.warn("Failed to extract image extension", e);
234-
}
225+
PictureVO vo = getPictureVO(earTagId, entity);
235226
list.add(vo);
236227
}
237228
long totalCount = BFFUtil.extractTotalCount(response);
238229
return BFFUtil.createResponse(list, page, size, sort, totalCount);
239230
}
240231

232+
private PictureVO getPictureVO(Long earTagId, PhotoEntity entity) {
233+
PictureVO vo = PictureVOMapper.INSTANCE.photoEntityToPictureVO(entity);
234+
String baseUrl = getCowImageBaseUrl(earTagId, entity.getId());
235+
try {
236+
Map<PictureSize, PictureSourceVO> map = cowPictureSourceService.createPictureSourceVOs(entity, baseUrl);
237+
vo.setSources(new ArrayList<>(map.values()));
238+
} catch (MimeTypeException e) {
239+
log.warn("Failed to extract image extension", e);
240+
}
241+
return vo;
242+
}
243+
241244
private String getCowImageBaseUrl(long earTagId, long pictureId) {
242245
return MessageFormat.format("/api/public/cows/{0}/pictures/{1}",
243246
String.valueOf(earTagId), String.valueOf(pictureId));
@@ -265,14 +268,14 @@ public ResponseEntity<Resource> getImageForCow(Long earTagId, Long pictureId, St
265268
if (!availableImageNames.contains(name)) {
266269
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
267270
}
268-
String baseUrl = getCowImageBaseUrl(earTagId, pictureId);
269-
Path imagePath = cowPictureSourceService.getOrCreateImagePath(pictureId, name, baseUrl);
271+
272+
String baseUrl = getCowImageBaseUrl(earTagId, photoEntity.getId());
270273
CacheControl cacheControl = getImageCacheControl(cattleEntity, photoEntity);
271274
return ResponseEntity
272275
.ok()
273276
.contentType(MediaType.parseMediaType(photoEntity.getImageContentType()))
274277
.cacheControl(cacheControl)
275-
.body(new InputStreamResource(new FileInputStream(imagePath.toFile())));
278+
.body(new InputStreamResource(cowPictureSourceService.createImageInputStream(photoEntity, baseUrl, name)));
276279
} catch (IOException | MimeTypeException e) {
277280
log.warn("Image with name {} for cow {} and id {} not found", name, earTagId, pictureId, e);
278281
throw new ResponseStatusException(HttpStatus.NOT_FOUND);

0 commit comments

Comments
 (0)