|
1 | 1 | package com.outfitlab.project.infrastructure; |
2 | 2 |
|
| 3 | +import org.springframework.http.*; |
3 | 4 | import com.fasterxml.jackson.core.JsonProcessingException; |
4 | 5 | import com.fasterxml.jackson.databind.JsonNode; |
5 | 6 | import com.fasterxml.jackson.databind.ObjectMapper; |
|
21 | 22 | import org.springframework.web.client.RestTemplate; |
22 | 23 | import org.springframework.web.multipart.MultipartFile; |
23 | 24 | import java.io.IOException; |
| 25 | +import java.io.InputStream; |
| 26 | +import java.net.URL; |
| 27 | +import java.nio.file.Files; |
| 28 | +import java.nio.file.Path; |
| 29 | +import java.nio.file.Paths; |
| 30 | +import java.nio.file.StandardCopyOption; |
24 | 31 | import java.util.HashMap; |
25 | 32 | import java.util.Map; |
26 | 33 |
|
@@ -174,4 +181,73 @@ public String getFileExtension(String nombreArchivo) { |
174 | 181 | } |
175 | 182 | return ""; |
176 | 183 | } |
| 184 | + |
| 185 | + @Override |
| 186 | + public Map<String, String> checkTaskStatus(String taskId) throws JsonProcessingException, InterruptedException { |
| 187 | + Map<String, String> taskStatus = new HashMap<>(); |
| 188 | + String glbUrl = null; |
| 189 | + String webpUrl = null; |
| 190 | + |
| 191 | + for (int i = 0; i < 30; i++) { // acá espero 1 min para ver si ya me generó el glb |
| 192 | + Thread.sleep(2000); |
| 193 | + |
| 194 | + HttpHeaders taskHeaders = new HttpHeaders(); |
| 195 | + taskHeaders.setContentType(MediaType.APPLICATION_JSON); |
| 196 | + taskHeaders.setBearerAuth(tripoApiKey); |
| 197 | + |
| 198 | + HttpEntity<Void> entityWithTaskHeaders = new HttpEntity<>(taskHeaders); |
| 199 | + ResponseEntity<String> statusResponse = restTemplate.exchange( |
| 200 | + taskUrl + "/" + taskId, |
| 201 | + HttpMethod.GET, |
| 202 | + entityWithTaskHeaders, |
| 203 | + String.class); |
| 204 | + |
| 205 | + JsonNode statusJson = mapper.readTree(statusResponse.getBody()); |
| 206 | + String status = statusJson.path("data").get("status").asText(); |
| 207 | + |
| 208 | + if (status.equalsIgnoreCase("success")) { |
| 209 | + glbUrl = statusJson.path("data").get("result").get("pbr_model").get("url").asText(); |
| 210 | + webpUrl = statusJson.path("data").get("result").get("rendered_image").get("url").asText(); |
| 211 | + taskStatus.put("glbUrl", glbUrl); |
| 212 | + taskStatus.put("webpUrl", webpUrl); |
| 213 | + taskStatus.put("taskId", taskId); |
| 214 | + break; |
| 215 | + } else if (status.equalsIgnoreCase("failed")) { |
| 216 | + throw new RuntimeException("La tarea falló: " + statusResponse.getBody()); |
| 217 | + } |
| 218 | + } |
| 219 | + return taskStatus; |
| 220 | + } |
| 221 | + |
| 222 | + @Override |
| 223 | + public Map<String, String> saveFilesFromTask(Map<String, String> taskResponse) throws IOException { |
| 224 | + // cuando tengamos el AWS, la idea es que estas url las bajemos y enviemos esos archivos a nuestro AWS y luego obtener |
| 225 | + // la URL de ESOS archivos recién alojados en nuestro AWS y enviarlas al front así pueden usarlas para mostrarlos. |
| 226 | + // (ya que las url de trippo no nos deja usarla en el front) |
| 227 | + |
| 228 | + String imageUrl = taskResponse.get("webpUrl"); |
| 229 | + String modelUrl = taskResponse.get("glbUrl"); |
| 230 | + String taskId = taskResponse.get("taskId"); |
| 231 | + |
| 232 | + if (imageUrl == null || modelUrl == null || taskId == null) { |
| 233 | + throw new IllegalArgumentException("Faltan datos en el mapa"); |
| 234 | + } |
| 235 | + |
| 236 | + Path resourcesPath = Paths.get("src/main/resources"); |
| 237 | + Map<String, String> localPaths = new HashMap<>(); |
| 238 | + |
| 239 | + Path imagePath = resourcesPath.resolve(taskId + "_image.webp"); |
| 240 | + try (InputStream in = new URL(imageUrl).openStream()) { |
| 241 | + Files.copy(in, imagePath, StandardCopyOption.REPLACE_EXISTING); |
| 242 | + } |
| 243 | + localPaths.put("image", "resources/" + imagePath.getFileName()); |
| 244 | + |
| 245 | + Path modelPath = resourcesPath.resolve(taskId + "_3d.glb"); |
| 246 | + try (InputStream in = new URL(modelUrl).openStream()) { |
| 247 | + Files.copy(in, modelPath, StandardCopyOption.REPLACE_EXISTING); |
| 248 | + } |
| 249 | + localPaths.put("glb", "resources/" + modelPath.getFileName()); |
| 250 | + |
| 251 | + return localPaths; |
| 252 | + } |
177 | 253 | } |
0 commit comments