fixed getting image from minio

This commit is contained in:
SlimusMinus
2026-10-13 00:43:06 +03:00
parent e321fa4c60
commit 9dbf2dd043
2 changed files with 64 additions and 1 deletions

View File

@@ -1,6 +1,8 @@
package com.krylov.refound.controller;
import com.krylov.refound.service.FileStorageService;
import java.time.Duration;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
@@ -34,6 +36,32 @@ public class FileStorageController {
return fileStorageService.downloadFile(key);
}
/**
* Редирект на 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));
return ResponseEntity.status(302)
.header("Location", url)
.build();
}
/**
* Возвращает presigned URL в JSON.
* PathPatternParser не допускает ничего после {*...}, поэтому "url" вынесен в префикс:
* GET /api/v1/files/url/avatars/cf80aebe-...jpg?ttlHours=24
*/
@GetMapping("/url/{*objectName}")
public ResponseEntity<Map<String, String>> getPresignedUrl(@PathVariable String objectName, @RequestParam(value = "ttlHours", required = false, defaultValue = "24") long ttlHours) {
String key = objectName.startsWith("/") ? objectName.substring(1) : objectName;
Duration ttl = ttlHours > 0 ? Duration.ofHours(ttlHours) : Duration.ofHours(24);
String url = fileStorageService.generatePresignedDownloadUrl(key, ttl);
return ResponseEntity.ok(Map.of("url", url));
}
@DeleteMapping("/{*objectName}")
public ResponseEntity<Void> deleteFile(@PathVariable String objectName) {
log.info("deleteFile {}", objectName);
@@ -42,4 +70,4 @@ public class FileStorageController {
fileStorageService.deleteFileAsCurrentUser(key);
return ResponseEntity.noContent().build();
}
}
}