fixed image caching
This commit is contained in:
@@ -5,6 +5,8 @@ import java.time.Duration;
|
||||
import java.util.Map;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.CacheControl;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
@@ -20,6 +22,12 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
@RequestMapping("/api/v1/files")
|
||||
@RequiredArgsConstructor
|
||||
public class FileStorageController {
|
||||
/**
|
||||
* Сколько браузер помнит редирект на presigned URL и не ходит за ним на бэк.
|
||||
* Вместе с кэшем ссылки (12ч) укладывается в её срок жизни (24ч).
|
||||
*/
|
||||
private static final Duration REDIRECT_CACHE_MAX_AGE = Duration.ofHours(6);
|
||||
|
||||
private final FileStorageService fileStorageService;
|
||||
|
||||
@PostMapping
|
||||
@@ -39,13 +47,16 @@ public class FileStorageController {
|
||||
/**
|
||||
* Редирект на presigned URL для прямой загрузки файла с MinIO (Вариант B).
|
||||
* Браузер перейдёт по 302 на временную ссылку и скачает картинку напрямую с MinIO.
|
||||
* Ссылка стабильна (кэшируется) и сам редирект кэшируется браузером — повторный
|
||||
* показ картинки не требует ни запроса на бэк, ни повторной загрузки.
|
||||
*/
|
||||
@GetMapping(value = "/{*objectName}", params = "presigned=true")
|
||||
public ResponseEntity<Void> redirectToPresigned(@PathVariable String objectName) {
|
||||
String key = objectName.startsWith("/") ? objectName.substring(1) : objectName;
|
||||
String url = fileStorageService.generatePresignedDownloadUrl(key, Duration.ofHours(24));
|
||||
String url = fileStorageService.getCachedPresignedDownloadUrl(key);
|
||||
return ResponseEntity.status(302)
|
||||
.header("Location", url)
|
||||
.header(HttpHeaders.LOCATION, url)
|
||||
.cacheControl(CacheControl.maxAge(REDIRECT_CACHE_MAX_AGE).cachePrivate())
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user