Implemented caching of static images

This commit is contained in:
grimsi
2022-08-04 15:57:40 +02:00
parent 7ba2eb2aea
commit 1879648e25
@@ -3,12 +3,16 @@ package de.grimsi.gameyfin.rest;
import de.grimsi.gameyfin.service.DownloadService; import de.grimsi.gameyfin.service.DownloadService;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.http.CacheControl;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.TimeUnit;
/** /**
* This controller handles functionality for images. * This controller handles functionality for images.
*/ */
@@ -20,7 +24,9 @@ public class ImageController {
private final DownloadService downloadService; private final DownloadService downloadService;
@GetMapping(value = "/{imageId}", produces = MediaType.IMAGE_PNG_VALUE) @GetMapping(value = "/{imageId}", produces = MediaType.IMAGE_PNG_VALUE)
public Resource getCoverImageForGame(@PathVariable String imageId) { public ResponseEntity<Resource> getCoverImageForGame(@PathVariable String imageId) {
return downloadService.downloadImage(imageId); return ResponseEntity.ok()
.cacheControl(CacheControl.maxAge(365, TimeUnit.DAYS).cachePublic())
.body(downloadService.downloadImage(imageId));
} }
} }