Skip to content

Commit

Permalink
[FEATURE] Cache images to dont overload the server
Browse files Browse the repository at this point in the history
  • Loading branch information
nck974 committed Jan 18, 2024
1 parent 340309a commit 61bddc1
Showing 1 changed file with 8 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package dev.nichoko.diogenes.controller;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.CacheControl;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -25,8 +28,13 @@ public FilesController(FileStorageService fileStorageService) {
@GetMapping("/images/{imageName}")
public ResponseEntity<Resource> serveImage(@PathVariable String imageName) {
Resource resource = fileStorageService.readItemImage(imageName);

// Set caching headers
CacheControl cacheControl = CacheControl.maxAge(30, TimeUnit.DAYS);

return ResponseEntity.ok()
.contentType(FileTypeChecker.guessImageContentType(imageName))
.cacheControl(cacheControl)
.body(resource);

}
Expand Down

0 comments on commit 61bddc1

Please sign in to comment.