Skip to content

Commit 2cecd98

Browse files
authored
Merge pull request #5 from 4-Image-Module/feature/#4-FetchLibrary
Feat: #4 fetch library
2 parents f5fb9bf + 373f08e commit 2cecd98

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.example.ImageModuleLibrary.Test;
2+
3+
import com.example.ImageModuleLibrary.fetch.FetchLibrary;
4+
import java.util.List;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RequestParam;
8+
import org.springframework.web.bind.annotation.RestController;
9+
import org.springframework.web.client.RestTemplate;
10+
11+
@RestController
12+
@RequestMapping("/test/fetch")
13+
public class FetchLibraryTest {
14+
15+
private final RestTemplate restTemplate = new RestTemplate();
16+
private final FetchLibrary fetchLibrary = new FetchLibrary(restTemplate);
17+
18+
@GetMapping
19+
public String getOriginalCdnUrlTest(@RequestParam("id") String id) {
20+
return fetchLibrary.getOriginalCdnUrl(id);
21+
}
22+
23+
@GetMapping("/resize")
24+
public String getResizedCdnUrlTest(@RequestParam("id") String id, @RequestParam("size") Integer size) {
25+
return fetchLibrary.getResizedCdnUrl(id, size);
26+
}
27+
28+
@GetMapping("/multi")
29+
public List<String> getMultiOriginalCdnUrlTest(@RequestParam("id") List<String> id) {
30+
return fetchLibrary.getMultiOriginalCdnUrl(id);
31+
}
32+
33+
34+
@GetMapping("/resize/multi")
35+
public List<String> getMultiResizedCdnUrlTest(@RequestParam("id") List<String> id,
36+
@RequestParam("size") List<Integer> size) {
37+
return fetchLibrary.getMultiResizedCdnUrl(id, size);
38+
}
39+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.example.ImageModuleLibrary.Test;
2+
3+
import com.example.ImageModuleLibrary.upload.UploadLibrary;
4+
import java.io.IOException;
5+
import java.util.List;
6+
import org.springframework.web.bind.annotation.PostMapping;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RequestParam;
9+
import org.springframework.web.bind.annotation.RestController;
10+
import org.springframework.web.client.RestTemplate;
11+
import org.springframework.web.multipart.MultipartFile;
12+
13+
@RestController
14+
@RequestMapping("/test/upload")
15+
public class UploadLibraryTest {
16+
17+
private final RestTemplate restTemplate = new RestTemplate();
18+
private final UploadLibrary uploadLibrary = new UploadLibrary(restTemplate);
19+
20+
@PostMapping
21+
public String uploadTest(@RequestParam("file") MultipartFile file) throws IOException {
22+
return uploadLibrary.uploadImage(file, 100, 1);
23+
}
24+
25+
@PostMapping("/multi")
26+
public List<String> uploadTestMulti(@RequestParam("files") MultipartFile[] files) throws IOException {
27+
Integer[] sizes = new Integer[]{100, 200};
28+
Integer[] cachingTimes = new Integer[]{1, 2};
29+
return uploadLibrary.uploadMultiImage(files, sizes, cachingTimes);
30+
}
31+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.example.ImageModuleLibrary.fetch;
2+
3+
import java.net.URI;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import org.springframework.web.client.RestTemplate;
7+
import org.springframework.web.util.UriComponentsBuilder;
8+
9+
public class FetchLibrary {
10+
private final RestTemplate restTemplate;
11+
12+
private static final String FETCH_ORIGINAL_CDN_URL = "http://localhost:19091/fetch/cdnUrl";
13+
14+
public FetchLibrary(RestTemplate restTemplate) {
15+
this.restTemplate = restTemplate;
16+
}
17+
18+
public String getOriginalCdnUrl(String uuid) {
19+
URI uri = UriComponentsBuilder.fromHttpUrl(FETCH_ORIGINAL_CDN_URL)
20+
.queryParam("id", uuid).build().toUri();
21+
22+
return restTemplate.getForObject(uri, String.class);
23+
}
24+
25+
public String getResizedCdnUrl(String uuid, Integer size) {
26+
URI uri = UriComponentsBuilder.fromHttpUrl(FETCH_ORIGINAL_CDN_URL)
27+
.queryParam("id", uuid)
28+
.queryParam("size", size)
29+
.build().toUri();
30+
31+
return restTemplate.getForObject(uri, String.class);
32+
}
33+
34+
public List<String> getMultiOriginalCdnUrl(List<String> uuids) {
35+
List<String> cdnUrls = new ArrayList<>();
36+
for (String uuid : uuids) {
37+
String cdnUrl = getOriginalCdnUrl(uuid);
38+
cdnUrls.add(cdnUrl);
39+
}
40+
return cdnUrls;
41+
}
42+
43+
public List<String> getMultiResizedCdnUrl(List<String> uuids, List<Integer> sizes) {
44+
List<String> cdnUrls = new ArrayList<>();
45+
for (int i = 0; i < uuids.size(); i++) {
46+
String cdnUrl = getResizedCdnUrl(uuids.get(i), sizes.get(i));
47+
cdnUrls.add(cdnUrl);
48+
}
49+
return cdnUrls;
50+
}
51+
}

0 commit comments

Comments
 (0)