Skip to content

Commit 752b01f

Browse files
committed
Fix file upload issue
1 parent 73b71f0 commit 752b01f

File tree

5 files changed

+117
-11
lines changed

5 files changed

+117
-11
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.continiousdisappointment.apigw.config;
2+
3+
import org.springframework.core.env.Environment;
4+
import org.springframework.http.*;
5+
import org.springframework.util.LinkedMultiValueMap;
6+
import org.springframework.util.MultiValueMap;
7+
import org.springframework.web.bind.annotation.*;
8+
import org.springframework.web.client.RestTemplate;
9+
import org.springframework.web.multipart.MultipartHttpServletRequest;
10+
11+
import lombok.RequiredArgsConstructor;
12+
13+
import jakarta.servlet.http.HttpServletRequest;
14+
import java.util.Arrays;
15+
16+
@RestController
17+
@RequiredArgsConstructor
18+
public class FileUploadProxyController {
19+
20+
private final Environment environment;
21+
private final RestTemplate restTemplate;
22+
23+
@PostMapping("/genai/**")
24+
public ResponseEntity<?> proxyMultipartRequest(
25+
HttpServletRequest request,
26+
@RequestHeader HttpHeaders headers) {
27+
28+
try {
29+
// Extract the remaining path after /genai
30+
String requestPath = request.getRequestURI();
31+
String targetPath = requestPath; // Keep the full path including /genai
32+
33+
// Build target URL
34+
String genaiServiceUrl = getGenAiServiceUrl();
35+
String targetUrl = genaiServiceUrl + targetPath;
36+
37+
// Prepare headers for forwarding (exclude host and content-length)
38+
HttpHeaders forwardHeaders = new HttpHeaders();
39+
headers.forEach((name, values) -> {
40+
String lowerName = name.toLowerCase();
41+
if (!lowerName.equals("host") && !lowerName.equals("content-length")) {
42+
forwardHeaders.put(name, values);
43+
}
44+
});
45+
46+
// Check if this is a multipart request
47+
if (request instanceof MultipartHttpServletRequest multipartRequest) {
48+
// Handle multipart request
49+
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
50+
51+
// Add all multipart files
52+
multipartRequest.getFileMap().forEach((name, file) -> {
53+
body.add(name, file.getResource());
54+
});
55+
56+
// Add all form parameters
57+
multipartRequest.getParameterMap().forEach((name, values) -> {
58+
for (String value : values) {
59+
body.add(name, value);
60+
}
61+
});
62+
63+
forwardHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
64+
65+
HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(body, forwardHeaders);
66+
return restTemplate.postForEntity(targetUrl, entity, Object.class);
67+
} else {
68+
// Handle non-multipart request
69+
HttpEntity<Object> entity = new HttpEntity<>(forwardHeaders);
70+
return restTemplate.postForEntity(targetUrl, entity, Object.class);
71+
}
72+
73+
} catch (Exception e) {
74+
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
75+
.body("Proxy error: " + e.getMessage());
76+
}
77+
}
78+
79+
private String getGenAiServiceUrl() {
80+
if (Arrays.asList(environment.getActiveProfiles()).contains("dev")) {
81+
return "http://localhost:8000";
82+
}
83+
return "http://genai-service:8000";
84+
}
85+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.continiousdisappointment.apigw.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
6+
7+
@Configuration
8+
public class MultipartConfig {
9+
10+
@Bean
11+
public StandardServletMultipartResolver multipartResolver() {
12+
StandardServletMultipartResolver resolver = new StandardServletMultipartResolver();
13+
resolver.setResolveLazily(true);
14+
return resolver;
15+
}
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.continiousdisappointment.apigw.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.client.RestTemplate;
6+
7+
@Configuration
8+
public class RestTemplateConfig {
9+
10+
@Bean
11+
public RestTemplate restTemplate() {
12+
return new RestTemplate();
13+
}
14+
}

server/api-gw/src/main/resources/application-dev.yml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,13 @@ spring:
55
gateway:
66
mvc:
77
routes:
8-
- id: genai
9-
uri: http://localhost:8000
10-
predicates:
11-
- Path=/genai/**
12-
- id: chat
8+
- id: user
139
uri: http://localhost:8081
1410
predicates:
1511
- Path=/user/**
16-
- id: user
12+
- id: chat
1713
uri: http://localhost:8082
1814
predicates:
1915
- Path=/chat/**
20-
2116
server:
2217
port: 8080

server/api-gw/src/main/resources/application.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ spring:
77
gateway:
88
mvc:
99
routes:
10-
- id: genai
11-
uri: http://genai-service:8000
12-
predicates:
13-
- Path=/genai/**
1410
- id: user
1511
uri: http://user-service:8081
1612
predicates:

0 commit comments

Comments
 (0)