Skip to content

Commit f5fb9bf

Browse files
authored
Merge pull request #3 from 4-Image-Module/feature/#2-UploadLibrary
Feat: #2 upload library
2 parents 9ef706c + 90ad871 commit f5fb9bf

File tree

3 files changed

+138
-1
lines changed

3 files changed

+138
-1
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package com.example.ImageModuleLibrary.upload;
2+
3+
import java.io.IOException;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import org.springframework.core.io.ByteArrayResource;
7+
import org.springframework.http.HttpEntity;
8+
import org.springframework.http.HttpHeaders;
9+
import org.springframework.http.MediaType;
10+
import org.springframework.http.ResponseEntity;
11+
import org.springframework.util.LinkedMultiValueMap;
12+
import org.springframework.util.MultiValueMap;
13+
import org.springframework.web.client.RestTemplate;
14+
import org.springframework.web.multipart.MultipartFile;
15+
16+
public class UploadLibrary {
17+
private final RestTemplate restTemplate;
18+
19+
private static final String UPLOAD_URL = "http://localhost:19091/image/upload";
20+
private static final String MULTI_UPLOAD_URL = "http://localhost:19091/image/upload/multi";
21+
22+
public UploadLibrary(RestTemplate restTemplate) {
23+
this.restTemplate = restTemplate;
24+
}
25+
26+
public String uploadImage(MultipartFile image, Integer size, Integer cachingTime) throws IOException {
27+
// body 설정
28+
MultiValueMap<String, Object> body = makeRequestBody(size, cachingTime, image);
29+
ResponseEntity<String> responseEntity = sendRequest(UPLOAD_URL, body);
30+
return getBody(responseEntity);
31+
}
32+
33+
public List<String> uploadMultiImage(MultipartFile[] files, Integer[] sizes, Integer[] cachingTimes)
34+
throws IOException {
35+
MultiValueMap<String, Object> body = makeMultiRequestBody(files, sizes, cachingTimes);
36+
ResponseEntity<String> responseEntity = sendRequest(MULTI_UPLOAD_URL, body);
37+
return getMultiBody(responseEntity);
38+
}
39+
40+
private static MultiValueMap<String, Object> makeMultiRequestBody(MultipartFile[] files, Integer[] sizes,
41+
Integer[] cachingTimes)
42+
throws IOException {
43+
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
44+
45+
for (int i = 0; i < files.length; i++) {
46+
ByteArrayResource resource = getImageByteArrayResource(files[i]);
47+
body.add("files", resource);
48+
body.add("sizes", sizes[i]);
49+
body.add("cachingTimes", cachingTimes[i]);
50+
}
51+
return body;
52+
}
53+
54+
private ResponseEntity<String> sendRequest(String url, MultiValueMap<String, Object> body) {
55+
// 헤더 설정
56+
HttpHeaders headers = new HttpHeaders();
57+
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
58+
59+
// HttpEntity 생성
60+
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
61+
62+
return restTemplate.postForEntity(url, requestEntity, String.class);
63+
}
64+
65+
private static MultiValueMap<String, Object> makeRequestBody(Integer size, Integer cachingTime,
66+
MultipartFile image) throws IOException {
67+
// form-data에 포함할 데이터 구성
68+
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
69+
70+
ByteArrayResource resource = getImageByteArrayResource(image);
71+
body.add("file", resource);
72+
body.add("size", size);
73+
body.add("cachingTime", cachingTime);
74+
return body;
75+
}
76+
77+
private static List<String> getMultiBody(ResponseEntity<String> responseEntity) {
78+
if (responseEntity.getStatusCode().is2xxSuccessful()) {
79+
80+
return getMultiSplitUUID(responseEntity);
81+
82+
} else {
83+
throw new RuntimeException("Failed to upload image: " + responseEntity.getStatusCode());
84+
}
85+
}
86+
87+
private static List<String> getMultiSplitUUID(ResponseEntity<String> responseEntity) {
88+
String answer = responseEntity.getBody();
89+
90+
String[] lines = answer.split("\n");
91+
92+
// 결과 리스트
93+
List<String> resultList = new ArrayList<>();
94+
95+
for (int i = 0; i < lines.length; i++) {
96+
if (lines[i].startsWith("event:SUCCESS")) {
97+
String line = lines[i + 1];
98+
String[] parts = line.split(":");
99+
resultList.add(parts[2]);
100+
}
101+
}
102+
return resultList;
103+
}
104+
105+
private static String getBody(ResponseEntity<String> responseEntity) {
106+
if (responseEntity.getStatusCode().is2xxSuccessful()) {
107+
return getSplitUUID(responseEntity);
108+
109+
} else {
110+
throw new RuntimeException("Failed to upload image: " + responseEntity.getStatusCode());
111+
}
112+
}
113+
114+
private static String getSplitUUID(ResponseEntity<String> responseEntity) {
115+
String answer = responseEntity.getBody();
116+
// 문자열을 줄 단위로 나누기
117+
String[] lines = answer.split("\n");
118+
119+
String dataLine = lines[4].trim();
120+
if (dataLine.startsWith("data:")) {
121+
// data: 부분을 ':'로 나누고 두 번째 부분을 반환
122+
return dataLine.split(":")[2].trim();
123+
}
124+
throw new RuntimeException("Data format is wrong: " + responseEntity.getStatusCode());
125+
}
126+
127+
private static ByteArrayResource getImageByteArrayResource(MultipartFile image) throws IOException {
128+
// 파일을 ByteArrayResource로 변환
129+
byte[] bytes = image.getBytes();
130+
131+
return new ByteArrayResource(bytes) {
132+
@Override
133+
public String getFilename() {
134+
return image.getOriginalFilename(); // 파일 이름 설정
135+
}
136+
};
137+
}
138+
}

src/main/resources/application.properties

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/main/resources/application.yml

Whitespace-only changes.

0 commit comments

Comments
 (0)