|
46 | 46 | import org.zalando.problem.Status; |
47 | 47 |
|
48 | 48 | import javax.validation.ValidationException; |
49 | | -import java.io.FileInputStream; |
50 | 49 | import java.io.IOException; |
51 | | -import java.nio.file.Path; |
| 50 | +import java.io.InputStream; |
52 | 51 | import java.text.MessageFormat; |
53 | 52 | import java.time.Duration; |
54 | 53 | import java.time.OffsetDateTime; |
@@ -223,21 +222,25 @@ public ResponseEntity<List<PictureVO>> getAllPictureVOsByCow(Long earTagId, Inte |
223 | 222 | ResponseEntity<List<PhotoEntity>> response = fetchPhotosByEarTagId(earTagId, page, size, sort); |
224 | 223 | List<PictureVO> list = new ArrayList<>(); |
225 | 224 | 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); |
235 | 226 | list.add(vo); |
236 | 227 | } |
237 | 228 | long totalCount = BFFUtil.extractTotalCount(response); |
238 | 229 | return BFFUtil.createResponse(list, page, size, sort, totalCount); |
239 | 230 | } |
240 | 231 |
|
| 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 | + |
241 | 244 | private String getCowImageBaseUrl(long earTagId, long pictureId) { |
242 | 245 | return MessageFormat.format("/api/public/cows/{0}/pictures/{1}", |
243 | 246 | String.valueOf(earTagId), String.valueOf(pictureId)); |
@@ -265,14 +268,14 @@ public ResponseEntity<Resource> getImageForCow(Long earTagId, Long pictureId, St |
265 | 268 | if (!availableImageNames.contains(name)) { |
266 | 269 | throw new ResponseStatusException(HttpStatus.NOT_FOUND); |
267 | 270 | } |
268 | | - String baseUrl = getCowImageBaseUrl(earTagId, pictureId); |
269 | | - Path imagePath = cowPictureSourceService.getOrCreateImagePath(pictureId, name, baseUrl); |
| 271 | + |
| 272 | + String baseUrl = getCowImageBaseUrl(earTagId, photoEntity.getId()); |
270 | 273 | CacheControl cacheControl = getImageCacheControl(cattleEntity, photoEntity); |
271 | 274 | return ResponseEntity |
272 | 275 | .ok() |
273 | 276 | .contentType(MediaType.parseMediaType(photoEntity.getImageContentType())) |
274 | 277 | .cacheControl(cacheControl) |
275 | | - .body(new InputStreamResource(new FileInputStream(imagePath.toFile()))); |
| 278 | + .body(new InputStreamResource(cowPictureSourceService.createImageInputStream(photoEntity, baseUrl, name))); |
276 | 279 | } catch (IOException | MimeTypeException e) { |
277 | 280 | log.warn("Image with name {} for cow {} and id {} not found", name, earTagId, pictureId, e); |
278 | 281 | throw new ResponseStatusException(HttpStatus.NOT_FOUND); |
|
0 commit comments