Skip to content

Commit 3231a49

Browse files
committed
move into javaai dir, add k8s and dockerfile and basicauth
1 parent 64f79a0 commit 3231a49

File tree

4 files changed

+103
-18
lines changed

4 files changed

+103
-18
lines changed

java-ai/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
<artifactId>json</artifactId>
3333
<version>20231013</version>
3434
</dependency>
35+
<dependency>
36+
<groupId>commons-io</groupId>
37+
<artifactId>commons-io</artifactId>
38+
<version>2.8.0</version>
39+
</dependency>
3540
<dependency>
3641
<groupId>com.oracle.cloud.spring</groupId>
3742
<artifactId>spring-cloud-oci-starter</artifactId>

java-ai/src/main/java/oracleai/UploadDownloadImage.java

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@
22

33

44
import oracleai.services.ORDSCalls;
5+
import org.apache.tomcat.util.http.fileupload.FileUtils;
6+
import org.springframework.http.ResponseEntity;
57
import org.springframework.stereotype.Controller;
68
import org.springframework.ui.Model;
7-
import org.springframework.web.bind.annotation.GetMapping;
8-
import org.springframework.web.bind.annotation.PostMapping;
9-
import org.springframework.web.bind.annotation.RequestMapping;
10-
import org.springframework.web.bind.annotation.RequestParam;
9+
import org.springframework.web.bind.annotation.*;
1110
import org.springframework.web.multipart.MultipartFile;
1211

12+
import java.io.File;
13+
import java.io.IOException;
14+
import java.nio.file.Path;
15+
import java.nio.file.Paths;
16+
17+
import org.apache.commons.io.FileUtils;
18+
import org.springframework.http.MediaType;
19+
1320
@Controller
1421
@RequestMapping("/transferimage")
1522
public class UploadDownloadImage {
@@ -23,16 +30,54 @@ public String uploadImage(@RequestParam("image") MultipartFile image, Model mode
2330
return "images";
2431
}
2532

33+
// @PostMapping("/uploadimageandvideo")
34+
// public String uploadimageandvideo(@RequestParam("image") MultipartFile image, Model model) {
35+
//// ORDSCalls.uploadImage(image);
36+
//// System.out.println("Image upload complete for: " + image.getOriginalFilename());
37+
// System.out.println("convertImage(): " + ORDSCalls.convertImage());
38+
// ImageStore[] imageStores = ORDSCalls.getImageStoreData();
39+
// model.addAttribute("images", imageStores);
40+
// return "images";
41+
// }
42+
43+
44+
private static final String DIRECTORY = "/tmp/images/";
45+
2646
@PostMapping("/uploadimageandvideo")
27-
public String uploadimageandvideo(@RequestParam("image") MultipartFile image, Model model) {
28-
// ORDSCalls.uploadImage(image);
29-
// System.out.println("Image upload complete for: " + image.getOriginalFilename());
30-
System.out.println("convertImage(): " + ORDSCalls.convertImage());
31-
ImageStore[] imageStores = ORDSCalls.getImageStoreData();
32-
model.addAttribute("images", imageStores);
33-
return "images";
47+
public ResponseEntity<String> uploadImage(@RequestParam("image") MultipartFile file) throws IOException {
48+
if (file.isEmpty()) {
49+
return ResponseEntity.badRequest().body("Cannot upload empty file");
50+
}
51+
52+
try {
53+
FileUtils.forceMkdir(new File(DIRECTORY));
54+
Path path = Paths.get(DIRECTORY + file.getOriginalFilename());
55+
file.transferTo(path);
56+
57+
return ResponseEntity.ok("File uploaded and available at: " + "/images/" + file.getOriginalFilename());
58+
} catch (Exception e) {
59+
return ResponseEntity.internalServerError().body("Could not upload the file: " + e.getMessage());
60+
}
3461
}
3562

63+
@GetMapping("/images/{filename:.+}")
64+
public ResponseEntity<byte[]> getImage(@PathVariable String filename) throws IOException {
65+
try {
66+
File file = new File(DIRECTORY, filename);
67+
byte[] fileContent = FileUtils.readFileToByteArray(file);
68+
return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(fileContent);
69+
} catch (IOException e) {
70+
return ResponseEntity.notFound().build();
71+
}
72+
}
73+
74+
75+
76+
77+
78+
79+
80+
3681

3782

3883
@GetMapping("/downloadimages")

java-ai/src/main/java/oracleai/services/ORDSCalls.java

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.springframework.web.client.RestTemplate;
1212
import org.springframework.web.multipart.MultipartFile;
1313

14+
1415
import java.io.IOException;
1516
import java.util.Base64;
1617
import java.util.Collections;
@@ -122,6 +123,7 @@ public static ImageStore[] make3Drequest() {
122123
}
123124
}
124125

126+
125127
public static String convertImage() {
126128
String apiUrl = "https://api.meshy.ai/v1/image-to-3d";
127129
RestTemplate restTemplate = new RestTemplate();
@@ -136,18 +138,47 @@ public static String convertImage() {
136138
ObjectMapper mapper = new ObjectMapper();
137139
try {
138140
JsonNode root = mapper.readTree(response.getBody());
139-
return root.path("result").asText(); // Return the result value
141+
String theResultString = root.path("result").asText();
142+
return pollApiUntilSuccess(theResultString);
140143
} catch (IOException e) {
141144
e.printStackTrace();
142145
return "Error parsing JSON";
143146
}
144147
}
148+
public static String pollApiUntilSuccess(String theResultString) {
149+
RestTemplate restTemplate = new RestTemplate();
150+
HttpHeaders headers = new HttpHeaders();
151+
headers.set("Authorization", "Bearer " + AIApplication.THREEDEY);
152+
headers.setContentType(MediaType.APPLICATION_JSON);
153+
HttpEntity<String> entity = new HttpEntity<>(headers);
154+
155+
ObjectMapper mapper = new ObjectMapper();
156+
while (true) {
157+
try {
158+
ResponseEntity<String> response =
159+
restTemplate.exchange(
160+
"https://api.meshy.ai/v1/image-to-3d/" + theResultString,
161+
HttpMethod.GET, entity, String.class);
162+
JsonNode rootNode = mapper.readTree(response.getBody());
163+
String status = rootNode.path("status").asText();
164+
System.out.println("ORDSCalls.pollApiUntilSuccess status:" + status);
165+
if ("SUCCEEDED".equals(status)) {
166+
String modelUrl = rootNode.path("model_url").asText();
167+
String modelGlbUrl = rootNode.path("model_urls").path("glb").asText();
168+
String modelFbxUrl = rootNode.path("model_urls").path("fbx").asText();
169+
String modelUsdzUrl = rootNode.path("model_urls").path("usdz").asText();
170+
String thumbnailUrl = rootNode.path("thumbnail_url").asText();
171+
return String.format("Model URL: %s\nGLB URL: %s\nFBX URL: %s\nUSDZ URL: %s\nThumbnail URL: %s",
172+
modelUrl, modelGlbUrl, modelFbxUrl, modelUsdzUrl, thumbnailUrl);
173+
}
174+
Thread.sleep(1000);
175+
} catch (Exception e) {
176+
e.printStackTrace();
177+
return "Failed to retrieve data: " + e.getMessage();
178+
}
179+
}
180+
return "Did not succeed";
181+
}
145182

146-
/**
147-
148-
curl https://api.meshy.ai/v1/image-to-3d/01917ce6-09a1-7bf6-8240-ff5d77f01d97 \
149-
-H "Authorization: Bearer msy_oCS1X5nuRxS06AjdlTJ0vCHg3OFyOhpaCMoa"
150-
151-
*/
152183
}
153184

java-ai/src/main/resources/application.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
spring:
2+
servlet:
3+
multipart:
4+
max-file-size: 200MB
5+
max-request-size: 200MB
26
cloud:
37
oci:
48
config:

0 commit comments

Comments
 (0)