Skip to content

Commit

Permalink
opt-in for thumbnail storage on s3 instead of disk
Browse files Browse the repository at this point in the history
i have missed that cloudflare dont cache the images as long as i originally thought
  • Loading branch information
frostmarked committed Jul 31, 2021
1 parent 88dda1d commit b00f2aa
Show file tree
Hide file tree
Showing 11 changed files with 496 additions and 143 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
/target/classes/static/**
/src/test/javascript/coverage/

# Bon
/src/main/resources/config/application-dev-*.yml

######################
# Node
######################
Expand Down
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
<spring-cloud-contract.version>2.2.2.RELEASE</spring-cloud-contract.version>
<net.coobird.thumbnailator.version>0.4.11</net.coobird.thumbnailator.version>
<org.apache.tika.version>1.24.1</org.apache.tika.version>
<aws-java-sdk-bom.version>1.12.32</aws-java-sdk-bom.version>
</properties>

<dependencyManagement>
Expand All @@ -112,6 +113,13 @@
<!-- jhipster-needle-maven-add-dependency-management -->

<!-- Bon -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-bom</artifactId>
<version>${aws-java-sdk-bom.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.graphql-java-kickstart</groupId>
<artifactId>graphql-spring-boot-starter</artifactId>
Expand Down Expand Up @@ -432,6 +440,10 @@
<!-- jhipster-needle-maven-add-dependency -->

<!-- Bon -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
</dependency>
<dependency>
<groupId>com.graphql-java-kickstart</groupId>
<artifactId>graphql-spring-boot-starter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@
import org.zalando.problem.Status;

import javax.validation.ValidationException;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.io.InputStream;
import java.text.MessageFormat;
import java.time.Duration;
import java.time.OffsetDateTime;
Expand Down Expand Up @@ -223,21 +222,25 @@ public ResponseEntity<List<PictureVO>> getAllPictureVOsByCow(Long earTagId, Inte
ResponseEntity<List<PhotoEntity>> response = fetchPhotosByEarTagId(earTagId, page, size, sort);
List<PictureVO> list = new ArrayList<>();
for (PhotoEntity entity : response.getBody()) {
PictureVO vo = PictureVOMapper.INSTANCE.photoEntityToPictureVO(entity);
String baseUrl = getCowImageBaseUrl(earTagId, entity.getId());
try {
Map<PictureSize, PictureSourceVO> map = cowPictureSourceService.createPictureSourceVOs(entity, baseUrl);
vo.setSources(new ArrayList<>(map.values()));
cowPictureSourceService.asyncCreateThumbnailsOnDiskIfNotExists(entity.getId(), map);
} catch (MimeTypeException e) {
log.warn("Failed to extract image extension", e);
}
PictureVO vo = getPictureVO(earTagId, entity);
list.add(vo);
}
long totalCount = BFFUtil.extractTotalCount(response);
return BFFUtil.createResponse(list, page, size, sort, totalCount);
}

private PictureVO getPictureVO(Long earTagId, PhotoEntity entity) {
PictureVO vo = PictureVOMapper.INSTANCE.photoEntityToPictureVO(entity);
String baseUrl = getCowImageBaseUrl(earTagId, entity.getId());
try {
Map<PictureSize, PictureSourceVO> map = cowPictureSourceService.createPictureSourceVOs(entity, baseUrl);
vo.setSources(new ArrayList<>(map.values()));
} catch (MimeTypeException e) {
log.warn("Failed to extract image extension", e);
}
return vo;
}

private String getCowImageBaseUrl(long earTagId, long pictureId) {
return MessageFormat.format("/api/public/cows/{0}/pictures/{1}",
String.valueOf(earTagId), String.valueOf(pictureId));
Expand Down Expand Up @@ -265,14 +268,14 @@ public ResponseEntity<Resource> getImageForCow(Long earTagId, Long pictureId, St
if (!availableImageNames.contains(name)) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}
String baseUrl = getCowImageBaseUrl(earTagId, pictureId);
Path imagePath = cowPictureSourceService.getOrCreateImagePath(pictureId, name, baseUrl);

String baseUrl = getCowImageBaseUrl(earTagId, photoEntity.getId());
CacheControl cacheControl = getImageCacheControl(cattleEntity, photoEntity);
return ResponseEntity
.ok()
.contentType(MediaType.parseMediaType(photoEntity.getImageContentType()))
.cacheControl(cacheControl)
.body(new InputStreamResource(new FileInputStream(imagePath.toFile())));
.body(new InputStreamResource(cowPictureSourceService.createImageInputStream(photoEntity, baseUrl, name)));
} catch (IOException | MimeTypeException e) {
log.warn("Image with name {} for cow {} and id {} not found", name, earTagId, pictureId, e);
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
Expand Down
Loading

0 comments on commit b00f2aa

Please sign in to comment.