|
1 | | -//package dev.coms4156.project.metadetect.controller; |
2 | | -// |
3 | | -//import dev.coms4156.project.metadetect.dto.Dtos; |
4 | | -//import dev.coms4156.project.metadetect.service.AnalyzeService; |
5 | | -//import java.io.IOException; |
6 | | -//import java.io.InputStream; |
7 | | -//import java.time.Instant; |
8 | | -//import java.util.Map; |
9 | | -//import org.springframework.http.HttpStatus; |
10 | | -//import org.springframework.http.ResponseEntity; |
11 | | -//import org.springframework.web.bind.annotation.GetMapping; |
12 | | -//import org.springframework.web.bind.annotation.PathVariable; |
13 | | -//import org.springframework.web.bind.annotation.PostMapping; |
14 | | -//import org.springframework.web.bind.annotation.RequestBody; |
15 | | -//import org.springframework.web.bind.annotation.RequestMapping; |
16 | | -//import org.springframework.web.bind.annotation.RequestParam; |
17 | | -//import org.springframework.web.bind.annotation.RequestPart; |
18 | | -//import org.springframework.web.bind.annotation.RestController; |
19 | | -//import org.springframework.web.multipart.MultipartFile; |
20 | | -//import org.springframework.web.server.ResponseStatusException; |
21 | | -// |
22 | | -///** |
23 | | -// * Endpoints for analysis, metadata, confidence, compare. |
24 | | -// */ |
25 | | -//@RestController |
26 | | -//@RequestMapping("/api") |
27 | | -//public class AnalyzeController { |
28 | | -// |
29 | | -// private final AnalyzeService analyzeService; |
30 | | -// |
31 | | -// public AnalyzeController(AnalyzeService analyzeService) { |
32 | | -// this.analyzeService = analyzeService; |
33 | | -// } |
34 | | -// /** |
35 | | -// * Handles submission of a new image for analysis. |
36 | | -// * This endpoint accepts a multipart/form-data request containing an image file |
37 | | -// * and optional analysis options. It validates and queues the image for |
38 | | -// * processing, returning an {@link Dtos.AnalyzeResponse} with the job ID and |
39 | | -// * current status. |
40 | | -// * |
41 | | -// * @param image the uploaded image file to be analyzed (required) |
42 | | -// * @param options optional parameters controlling which analysis modules to run |
43 | | -// * @return a {@link ResponseEntity} containing the analysis job ID, confidence |
44 | | -// * placeholder, and initial status |
45 | | -// */ |
46 | | -// |
47 | | -// @PostMapping(value = "/analyze", consumes = {"multipart/form-data"}) |
48 | | -// public ResponseEntity<Dtos.AnalyzeResponse> analyze( |
49 | | -// @RequestPart("image") MultipartFile image, |
50 | | -// @RequestPart(value = "options", required = false) Dtos.AnalyzeOptions options) { |
51 | | -// // TODO: validate mime/size; persist; enqueue/compute; return id + status |
52 | | -// Dtos.AnalyzeResponse stub = |
53 | | -// new Dtos.AnalyzeResponse("stub-id", 0.42, "PENDING", Instant.now(), null); |
54 | | -// return ResponseEntity.status(HttpStatus.ACCEPTED).body(stub); |
55 | | -// } |
56 | | -// |
57 | | -// |
58 | | -// // /** |
59 | | -// // * Extracts the C2PA manifest from an uploaded image file. |
60 | | -// // * This endpoint accepts a multipart/form-data request containing an image file. |
61 | | -// // * It processes the file using the C2PA tool to extract the manifest metadata. |
62 | | -// // * The extracted manifest is returned as a JSON string. |
63 | | -// // * |
64 | | -// // * @param file the uploaded image file (required). |
65 | | -// // * @return a {@link ResponseEntity} containing the extracted C2PA manifest as a JSON string. |
66 | | -// // * @throws ResponseStatusException |
67 | | -// // * if the extraction fails due to an I/O error or invalid input. |
68 | | -// // */ |
69 | | -// // @PostMapping(value = "/extract", consumes = "multipart/form-data") |
70 | | -// // public ResponseEntity<String> extract(@RequestParam("file") MultipartFile file) { |
71 | | -// // try { |
72 | | -// // // Extract the C2PA manifest |
73 | | -// // //InputStream in = file.getInputStream(); |
74 | | -// // //String manifestJson = analyzeService.fetchC2pa(in); |
75 | | -// // |
76 | | -// // // String manifestJson = analyzeService.fetchC2pa(new java.io.File("tempfile")); |
77 | | -// // |
78 | | -// // // Return the JSON response |
79 | | -// // return ResponseEntity.ok(manifestJson); |
80 | | -// // } catch (IOException e) { |
81 | | -// // throw new ResponseStatusException(HttpStatus.BAD_REQUEST, |
82 | | -// // "Failed to extract C2PA manifest", e); |
83 | | -// // } |
84 | | -// // } |
85 | | -// |
86 | | -// /** |
87 | | -// * Retrieves metadata for a specific image analysis job. (TODO: IMPLEMENT/DELETE) |
88 | | -// * This endpoint fetches parsed EXIF and other metadata associated with the given job ID. |
89 | | -// * The metadata is returned as a {@link Dtos.MetadataResponse} object. |
90 | | -// * |
91 | | -// * @param id the unique identifier of the image analysis job (required). |
92 | | -// * @return a {@link ResponseEntity} containing the metadata associated with the job ID. |
93 | | -// */ |
94 | | -// @GetMapping("/metadata/{id}") |
95 | | -// public ResponseEntity<Dtos.MetadataResponse> metadata(@PathVariable String id) { |
96 | | -// // TODO: fetch parsed EXIF/metadata from DB |
97 | | -// return ResponseEntity.ok(new Dtos.MetadataResponse(id, Map.of())); |
98 | | -// } |
99 | | -// |
100 | | -// /** |
101 | | -// * Retrieves the confidence score and status for a specific image analysis job. |
102 | | -// * This endpoint fetches the confidence score and current status associated with the given job ID. |
103 | | -// * The confidence score represents the likelihood of the analysis being correct, and the status |
104 | | -// * indicates the current state of the analysis (e.g., PENDING, COMPLETED). |
105 | | -// * |
106 | | -// * @param id the unique identifier of the image analysis job (required). |
107 | | -// * @return a {@link ResponseEntity} containing the confidence score and status for the job ID. |
108 | | -// */ |
109 | | -// @GetMapping("/confidence/{id}") |
110 | | -// public ResponseEntity<Dtos.ConfidenceResponse> confidence(@PathVariable String id) { |
111 | | -// // TODO: fetch score + status from DB |
112 | | -// return ResponseEntity.ok(new Dtos.ConfidenceResponse(id, 0.42, "PENDING")); |
113 | | -// } |
114 | | -// |
115 | | -// /** |
116 | | -// * Compares two images or analysis results to compute their similarity. |
117 | | -// * This endpoint accepts either image files or analysis job IDs to compare the similarity between |
118 | | -// * two images. It supports both file-based comparison and ID-based comparison modes. |
119 | | -// * The similarity score is returned as a percentage value. |
120 | | -// * |
121 | | -// * @param byIds an optional {@link Dtos.CompareRequest} containing job IDs for comparison. |
122 | | -// * @param imageA an optional {@link MultipartFile} representing the first image file to compare. |
123 | | -// * @param imageB an optional {@link MultipartFile} representing the second image file to compare. |
124 | | -// * @return a {@link ResponseEntity} containing the similarity score and details of the comparison. |
125 | | -// */ |
126 | | -// @PostMapping(value = "/compare", consumes = {"application/json", |
127 | | -// "multipart/form-data"}) |
128 | | -// public ResponseEntity<Dtos.CompareResponse> compare( |
129 | | -// @RequestBody(required = false) Dtos.CompareRequest byIds, |
130 | | -// @RequestPart(value = "imageA", required = false) MultipartFile imageA, |
131 | | -// @RequestPart(value = "imageB", required = false) MultipartFile imageB) { |
132 | | -// // TODO: support id mode and file mode; compute similarity |
133 | | -// return ResponseEntity.ok(new Dtos.CompareResponse("a", "b", 0.13)); |
134 | | -// } |
135 | | -//} |
| 1 | +package dev.coms4156.project.metadetect.controller; |
| 2 | + |
| 3 | +import dev.coms4156.project.metadetect.dto.Dtos; |
| 4 | +import dev.coms4156.project.metadetect.service.AnalyzeService; |
| 5 | +import java.util.UUID; |
| 6 | +import org.springframework.http.HttpStatus; |
| 7 | +import org.springframework.http.ResponseEntity; |
| 8 | +import org.springframework.web.bind.annotation.GetMapping; |
| 9 | +import org.springframework.web.bind.annotation.PathVariable; |
| 10 | +import org.springframework.web.bind.annotation.PostMapping; |
| 11 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 12 | +import org.springframework.web.bind.annotation.RequestParam; |
| 13 | +import org.springframework.web.bind.annotation.RestController; |
| 14 | + |
| 15 | + |
| 16 | +/** |
| 17 | + * REST controller for image analysis operations. |
| 18 | + * Endpoints: |
| 19 | + * - POST /api/analyze/{imageId} -> start an analysis (202 Accepted) |
| 20 | + * - GET /api/analyze/{analysisId} -> status/score (polling) |
| 21 | + * - GET /api/analyze/{analysisId}/manifest -> manifest JSON |
| 22 | + * - GET /api/analyze/compare -> stubbed compare (left & right image IDs) |
| 23 | + * Notes: |
| 24 | + * - Ownership and RLS checks are enforced in AnalyzeService/ImageService. |
| 25 | + * - Exceptions (Forbidden/NotFound/etc.) |
| 26 | + * are expected to be mapped by global @RestControllerAdvice. |
| 27 | + */ |
| 28 | +@RestController |
| 29 | +@RequestMapping("/api/analyze") |
| 30 | +public class AnalyzeController { |
| 31 | + |
| 32 | + private final AnalyzeService analyzeService; |
| 33 | + |
| 34 | + public AnalyzeController(AnalyzeService analyzeService) { |
| 35 | + this.analyzeService = analyzeService; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Starts analysis for an existing image that is already uploaded to Supabase Storage. |
| 40 | + * Returns 202 with a body containing the new analysisId. |
| 41 | + */ |
| 42 | + @PostMapping("/{imageId}") |
| 43 | + public ResponseEntity<Dtos.AnalyzeStartResponse> submit(@PathVariable UUID imageId) { |
| 44 | + Dtos.AnalyzeStartResponse resp = analyzeService.submitAnalysis(imageId); |
| 45 | + // As per ticket: 202 Accepted with { analysisId } |
| 46 | + return ResponseEntity.status(HttpStatus.ACCEPTED).body(resp); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Returns current status (PENDING/COMPLETED/FAILED) and an optional score (stubbed). |
| 51 | + * Suitable for client-side polling. |
| 52 | + */ |
| 53 | + @GetMapping("/{analysisId}") |
| 54 | + public ResponseEntity<Dtos.AnalyzeConfidenceResponse> getStatus(@PathVariable UUID analysisId) { |
| 55 | + Dtos.AnalyzeConfidenceResponse resp = analyzeService.getConfidence(analysisId); |
| 56 | + return ResponseEntity.ok(resp); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Returns the stored C2PA manifest JSON for a completed analysis. |
| 61 | + */ |
| 62 | + @GetMapping("/{analysisId}/manifest") |
| 63 | + public ResponseEntity<Dtos.AnalysisManifestResponse> getManifest(@PathVariable UUID analysisId) { |
| 64 | + Dtos.AnalysisManifestResponse resp = analyzeService.getMetadata(analysisId); |
| 65 | + return ResponseEntity.ok(resp); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Stubbed comparison endpoint (Iteration 1). |
| 70 | + * Ownership of both images is validated by the service layer. |
| 71 | + * Example: /api/analyze/compare?left={imageId}&right={imageId} |
| 72 | + */ |
| 73 | + @GetMapping("/compare") |
| 74 | + public ResponseEntity<Dtos.AnalyzeCompareResponse> compare( |
| 75 | + @RequestParam("left") UUID leftImageId, |
| 76 | + @RequestParam("right") UUID rightImageId) { |
| 77 | + |
| 78 | + Dtos.AnalyzeCompareResponse resp = analyzeService.compare(leftImageId, rightImageId); |
| 79 | + return ResponseEntity.ok(resp); |
| 80 | + } |
| 81 | +} |
0 commit comments