Skip to content

Commit 9fc0d90

Browse files
committed
digital double logic
1 parent 73c8912 commit 9fc0d90

File tree

6 files changed

+349
-54
lines changed

6 files changed

+349
-54
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package oracleai;
2+
3+
public class DigitalDoubleDownloadInfo {
4+
5+
String email;
6+
String modelUrl;
7+
String modelGlbUrl;
8+
String modelFbxUrl ;
9+
String modelUsdzUrl ;
10+
String thumbnailUrl;
11+
String animatedVideoLocation;
12+
String similarImageUrl;
13+
14+
15+
16+
public DigitalDoubleDownloadInfo() {
17+
18+
}
19+
public DigitalDoubleDownloadInfo(String modelUrl, String modelGlbUrl, String modelFbxUrl,
20+
String modelUsdzUrl, String thumbnailUrl) {
21+
this.modelUrl = modelUrl;
22+
this.modelGlbUrl = modelGlbUrl;
23+
this.modelFbxUrl = modelFbxUrl;
24+
this.modelUsdzUrl = modelUsdzUrl;
25+
this.thumbnailUrl = thumbnailUrl;
26+
}
27+
28+
public DigitalDoubleDownloadInfo(String modelGlbUrl, String modelFbxUrl, String modelUsdzUrl,
29+
String thumbnailUrl, String animatedVideoLocation,
30+
String email, String similarImageUrl) {
31+
this.modelGlbUrl = modelGlbUrl;
32+
this.modelFbxUrl = modelFbxUrl;
33+
this.modelUsdzUrl = modelUsdzUrl;
34+
this.thumbnailUrl = thumbnailUrl;
35+
this.animatedVideoLocation = animatedVideoLocation;
36+
this.email = email;
37+
this.similarImageUrl = similarImageUrl;
38+
}
39+
40+
public void setEmail(String email) {
41+
this.email = email;
42+
}
43+
44+
public void setModelUrl(String modelUrl) {
45+
this.modelUrl = modelUrl;
46+
}
47+
48+
public void setModelGlbUrl(String modelGlbUrl) {
49+
this.modelGlbUrl = modelGlbUrl;
50+
}
51+
52+
public void setModelFbxUrl(String modelFbxUrl) {
53+
this.modelFbxUrl = modelFbxUrl;
54+
}
55+
56+
public void setModelUsdzUrl(String modelUsdzUrl) {
57+
this.modelUsdzUrl = modelUsdzUrl;
58+
}
59+
60+
public void setThumbnailUrl(String thumbnailUrl) {
61+
this.thumbnailUrl = thumbnailUrl;
62+
}
63+
64+
public void setAnimatedVideoLocation(String animatedVideoLocation) {
65+
this.animatedVideoLocation = animatedVideoLocation;
66+
}
67+
68+
public void setSimilarImageUrl(String similarImageUrl) {
69+
this.similarImageUrl = similarImageUrl;
70+
}
71+
72+
public String getEmail() {
73+
return email;
74+
}
75+
76+
public String getModelUrl() {
77+
return modelUrl;
78+
}
79+
80+
public String getModelGlbUrl() {
81+
return modelGlbUrl;
82+
}
83+
84+
public String getModelFbxUrl() {
85+
return modelFbxUrl;
86+
}
87+
88+
public String getModelUsdzUrl() {
89+
return modelUsdzUrl;
90+
}
91+
92+
public String getThumbnailUrl() {
93+
return thumbnailUrl;
94+
}
95+
96+
public String getAnimatedVideoLocation() {
97+
return animatedVideoLocation;
98+
}
99+
100+
public String getSimilarImageUrl() {
101+
return similarImageUrl;
102+
}
103+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package oracleai;
2+
3+
import org.springframework.stereotype.Service;
4+
import org.springframework.web.client.RestTemplate;
5+
import org.springframework.http.HttpEntity;
6+
import org.springframework.http.HttpHeaders;
7+
import org.springframework.http.MediaType;
8+
import org.springframework.http.ResponseEntity;
9+
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
13+
@Service
14+
public class DigitalDoubleService {
15+
16+
private final RestTemplate restTemplate;
17+
18+
public DigitalDoubleService(RestTemplate restTemplate) {
19+
this.restTemplate = restTemplate;
20+
}
21+
22+
public void updateDigitalDoubleData(DigitalDoubleDownloadInfo info) {
23+
String url = "https://rddainsuh6u1okc-ragdb.adb.us-ashburn-1.oraclecloudapps.com/ords/omlopsuser/update_digital_double_data/";
24+
25+
// Prepare headers
26+
HttpHeaders headers = new HttpHeaders();
27+
headers.setContentType(MediaType.APPLICATION_JSON);
28+
29+
// Prepare the payload
30+
Map<String, Object> requestBody = new HashMap<>();
31+
requestBody.put("p_participant_email", info.getEmail());
32+
requestBody.put("p_modelglburl_out", info.getModelGlbUrl());
33+
requestBody.put("p_modelfbxurl_out", info.getModelFbxUrl());
34+
requestBody.put("p_modelusdzurl_out", info.getModelUsdzUrl());
35+
requestBody.put("p_thumbnailurl_out", info.getThumbnailUrl());
36+
requestBody.put("p_videourl_out", info.getAnimatedVideoLocation());
37+
requestBody.put("p_video_out", null); // Optional field
38+
requestBody.put("p_similar_image_out", info.getSimilarImageUrl());
39+
40+
// Create the HttpEntity that includes headers and the body
41+
HttpEntity<Map<String, Object>> entity = new HttpEntity<>(requestBody, headers);
42+
43+
// Make the POST request
44+
ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class);
45+
46+
// Handle the response (optional)
47+
if (response.getStatusCode().is2xxSuccessful()) {
48+
System.out.println("Request successful: " + response.getBody());
49+
} else {
50+
System.err.println("Request failed with status code: " + response.getStatusCode());
51+
}
52+
}
53+
}

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

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import oracleai.services.ORDSCalls;
55
import oracleai.services.OracleObjectStore;
66
import org.apache.tomcat.util.http.fileupload.FileUtils;
7+
import org.jetbrains.annotations.Nullable;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.http.HttpEntity;
10+
import org.springframework.http.HttpHeaders;
711
import org.springframework.http.ResponseEntity;
812
import org.springframework.stereotype.Controller;
913
import org.springframework.ui.Model;
@@ -14,19 +18,30 @@
1418
import java.io.IOException;
1519
import java.nio.file.Path;
1620
import java.nio.file.Paths;
21+
import java.util.HashMap;
22+
import java.util.Map;
1723

1824
import org.springframework.http.MediaType;
1925

2026
@Controller
2127
@RequestMapping("/digitaldoubles")
2228
public class DigitalDoubles {
2329

30+
private final ImageProcessor imageProcessor;
31+
32+
// Inject the ImageProcessor using constructor injection
33+
@Autowired
34+
public DigitalDoubles(ImageProcessor imageProcessor) {
35+
this.imageProcessor = imageProcessor;
36+
}
37+
private static final String DIRECTORY = "/tmp/images/";
38+
2439
@GetMapping("/uploadordownload")
2540
public String digitaldouble(@RequestParam("action") String action, Model model) {
26-
return action.equals("uploading")?"digitaldoubleupload":"digitaldoubledownload";
41+
return action.equals("uploading") ? "digitaldoubleupload" : "digitaldoubledownload";
2742
}
2843

29-
private static final String DIRECTORY = "/tmp/images/";
44+
3045
@PostMapping("/uploadimageandvideo")
3146
public String uploadimageandvideo(
3247
@RequestParam("image") MultipartFile image,
@@ -43,43 +58,42 @@ public String uploadimageandvideo(
4358
Model model) throws IOException {
4459

4560
String commentsWithAnimStyleAndPrompt = animstyle + " " + animprompt + " " + comments;
46-
System.out.println("image = " + image + ", video = " + video +", animstyle = " + animstyle +
61+
System.out.println("image = " + image + ", video = " + video + ", animstyle = " + animstyle +
4762
", firstName = " + firstName + ", lastName = " + lastName +
4863
", email = " + email + ", company = " + company +
4964
", jobRole = " + jobRole + ", tshirtSize = " + tshirtSize +
5065
", comments = " + comments + ", model = " + model +
5166
"\ncomments with animstyle and prompt = " + commentsWithAnimStyleAndPrompt);
52-
if (!image.isEmpty()) {
53-
ORDSCalls.insertDigitalDoubleData(
54-
image,null, firstName, lastName, email, company,jobRole, tshirtSize, commentsWithAnimStyleAndPrompt);
55-
if (!video.isEmpty()) {
56-
OracleObjectStore.sendToObjectStorage(
57-
email + "_" + animstyle + "_" + video.getOriginalFilename(), video.getInputStream());
58-
}
59-
try {
60-
org.apache.commons.io.FileUtils.forceMkdir(new File(DIRECTORY));
61-
Path path = Paths.get(DIRECTORY + image.getOriginalFilename());
62-
image.transferTo(path);
63-
String fbxUrl = ORDSCalls.convertImage("http://129.80.168.144/digitaldoubles/images/",
64-
image.getOriginalFilename());
65-
model.addAttribute("resultlink", fbxUrl);
66-
model.addAttribute("resulttext", "Click here for your FBX 3D model");
67-
return "resultswithlinkpage";
68-
// return ResponseEntity.ok(
69-
// ORDSCalls.convertImage("http://129.80.168.144/transferimage/images/" + file.getOriginalFilename())
70-
// );
71-
// return ResponseEntity.ok("File uploaded and available at: " + "/images/" + file.getOriginalFilename());
72-
} catch (Exception e) {
73-
return e.toString();
74-
// ResponseEntity.internalServerError().body("Could not upload the file: " + e.getMessage());
75-
}
76-
// Save or process the image
77-
} else {
78-
model.addAttribute("resultlink", "http://129.80.168.144/UploadDigitalDouble.html");
79-
model.addAttribute("resulttext",
80-
"Image not provided or is empty. Click here to try again.");
81-
return "resultswithlinkpage";
67+
ORDSCalls.insertDigitalDoubleData(
68+
image, null, firstName, lastName, email, company, jobRole, tshirtSize, commentsWithAnimStyleAndPrompt);
69+
70+
String fullVideoName ="";
71+
if (!video.isEmpty()) {
72+
fullVideoName = email + "_" + animstyle + "_" + video.getOriginalFilename();
73+
OracleObjectStore.sendToObjectStorage(fullVideoName, video.getInputStream());
8274
}
75+
imageProcessor.handleImageUpload(email, image, fullVideoName);
76+
77+
// try {
78+
// org.apache.commons.io.FileUtils.forceMkdir(new File(DIRECTORY));
79+
// String imageFileNameWithEmailPrefix = email + "_" + image.getOriginalFilename();
80+
// Path path = Paths.get(DIRECTORY + imageFileNameWithEmailPrefix);
81+
// image.transferTo(path);
82+
// String fbxUrl = ORDSCalls.convertImage("http://129.80.168.144/digitaldoubles/images/",
83+
// imageFileNameWithEmailPrefix);
84+
// model.addAttribute("resultlink", fbxUrl);
85+
// model.addAttribute("resulttext", "Click here for your FBX 3D model");
86+
// return "resultswithlinkpage";
87+
// } catch (Exception e) {
88+
// return e.toString();
89+
//// ResponseEntity.internalServerError().body("Could not upload the file: " + e.getMessage());
90+
// }
91+
92+
// model.addAttribute("resultlink", "http://129.80.168.144/UploadDigitalDouble.html");
93+
// model.addAttribute("resulttext",
94+
// "Image not provided or is empty. Click here to try again.");
95+
return "digitaldoubledownload";
96+
8397
}
8498

8599
@GetMapping("/images/{filename:.+}")
@@ -96,11 +110,10 @@ public ResponseEntity<byte[]> getImage(@PathVariable String filename) throws IOE
96110

97111
@PostMapping("/downloaddigitaldouble")
98112
public String downloaddigitaldouble(@RequestParam("email") String email, Model model) {
99-
System.out.println("DigitalDoubles.downloaddigitaldouble lookup email:" + email);
100-
model.addAttribute("resultlink", email);
101-
model.addAttribute("resulttext", "Click here for your FBX 3D model");
113+
ORDSCalls.getDigitalDoubleData(email, model);
102114
return "resultswithlinkpage";
103115
// return "digitaldoubleresults";
104116
}
105117

118+
106119
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package oracleai;
2+
3+
import oracleai.services.ORDSCalls;
4+
import org.apache.commons.io.FileUtils;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Service;
7+
import org.springframework.web.multipart.MultipartFile;
8+
9+
import java.io.File;
10+
import java.io.IOException;
11+
import java.nio.file.Path;
12+
import java.nio.file.Paths;
13+
import java.util.concurrent.BlockingQueue;
14+
import java.util.concurrent.LinkedBlockingQueue;
15+
16+
@Service
17+
public class ImageProcessor {
18+
19+
private static final String DIRECTORY = "/tmp/images/";
20+
private static final BlockingQueue<ImageTask> queue = new LinkedBlockingQueue<>();
21+
22+
// Inject DigitalDoubleService to make REST call
23+
private final DigitalDoubleService digitalDoubleService;
24+
25+
@Autowired
26+
public ImageProcessor(DigitalDoubleService digitalDoubleService) {
27+
this.digitalDoubleService = digitalDoubleService;
28+
new Thread(this::processQueue).start();
29+
}
30+
31+
public String handleImageUpload(String email, MultipartFile image, String fullVideoName) throws IOException {
32+
String imageFileNameWithEmailPrefix = "";
33+
if (image != null && !image.isEmpty()) {
34+
FileUtils.forceMkdir(new File(DIRECTORY));
35+
imageFileNameWithEmailPrefix = email + "_" + image.getOriginalFilename();
36+
Path path = Paths.get(DIRECTORY + imageFileNameWithEmailPrefix);
37+
image.transferTo(path);
38+
}
39+
queue.offer(new ImageTask(email, imageFileNameWithEmailPrefix, fullVideoName));
40+
return "Image is being processed";
41+
}
42+
43+
private static String objectStoreLocation =
44+
"https://" + AIApplication.OBJECTSTORAGE_NAMESPACE + ".compat.objectstorage.us-ashburn-1.oraclecloud.com/" +
45+
AIApplication.OBJECTSTORAGE_BUCKETNAME + "/anim/";
46+
47+
private void processQueue() {
48+
while (true) {
49+
try {
50+
ImageTask task = queue.take();
51+
DigitalDoubleDownloadInfo digitalDoubleDownloadInfo;
52+
if (!task.getImageFileNameWithEmailPrefix().equals("")) {
53+
digitalDoubleDownloadInfo = ORDSCalls.convertImageAndQueueResults(
54+
"http://129.80.168.144/digitaldoubles/images/",
55+
task.getImageFileNameWithEmailPrefix());
56+
} else {
57+
digitalDoubleDownloadInfo = new DigitalDoubleDownloadInfo();
58+
}
59+
60+
digitalDoubleDownloadInfo.animatedVideoLocation = objectStoreLocation + task.getFullVideoName();
61+
62+
// Call the method to update Digital Double data
63+
digitalDoubleService.updateDigitalDoubleData(
64+
new DigitalDoubleDownloadInfo(
65+
digitalDoubleDownloadInfo.modelGlbUrl,
66+
digitalDoubleDownloadInfo.modelFbxUrl,
67+
digitalDoubleDownloadInfo.modelUsdzUrl,
68+
digitalDoubleDownloadInfo.thumbnailUrl,
69+
digitalDoubleDownloadInfo.animatedVideoLocation,
70+
task.getEmail(),
71+
"" // Similar image can be passed here if available
72+
)
73+
);
74+
75+
} catch (InterruptedException e) {
76+
Thread.currentThread().interrupt();
77+
break;
78+
} catch (Exception e) {
79+
System.err.println("Failed to process image for: " + e.getMessage());
80+
}
81+
}
82+
}
83+
84+
private static class ImageTask {
85+
private final String email;
86+
private final String imageFileNameWithEmailPrefix;
87+
private final String fullVideoName;
88+
89+
public ImageTask(String email, String imageFileNameWithEmailPrefix, String fullVideoName) {
90+
this.email = email;
91+
this.imageFileNameWithEmailPrefix = imageFileNameWithEmailPrefix;
92+
this.fullVideoName = fullVideoName;
93+
}
94+
95+
public String getEmail() {
96+
return email;
97+
}
98+
99+
public String getImageFileNameWithEmailPrefix() {
100+
return imageFileNameWithEmailPrefix;
101+
}
102+
103+
public String getFullVideoName() {
104+
return fullVideoName;
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)