package com.aiteacher.config; import com.aiteacher.figure.FigureStorageService; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; import org.springframework.web.server.ResponseStatusException; import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; /** * Serves figure images by redirecting to a presigned S3 URL. * The key stored in DB is the full S3 object key, e.g. "figures/{bookId}/{figureId}.png". */ @RestController @RequestMapping("/api/v1/figures") public class FigureStorageConfig { private final FigureStorageService figureStorageService; public FigureStorageConfig(FigureStorageService figureStorageService) { this.figureStorageService = figureStorageService; } @GetMapping("/{bookId}/{filename}") public void serve(@PathVariable String bookId, @PathVariable String filename, HttpServletResponse response) throws IOException { String key = "figures/" + bookId + "/" + filename; try { String url = figureStorageService.presignedUrl(key); response.sendRedirect(url); } catch (Exception ex) { throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Figure not found: " + key); } } }