-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
179 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/java/ac/kr/deu/connect/luck/image/ImageRestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package ac.kr.deu.connect.luck.image; | ||
|
||
import io.swagger.v3.oas.annotations.Parameter; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.AllArgsConstructor; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestPart; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
@RestController | ||
@Tag(name = "Image", description = "Image API") | ||
@RequestMapping("/api/image") | ||
@AllArgsConstructor | ||
public class ImageRestController { | ||
|
||
private final ImageUploader imageUploader; | ||
|
||
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE) | ||
public String uploadImage( | ||
@Parameter(description = "multipart/form-data 형식의 이미지 리스트를 input으로 받습니다. 이때 key 값은 image 입니다.") | ||
@RequestPart("image") MultipartFile multipartFile) { | ||
return imageUploader.uploadImage(multipartFile).getData().getUrl(); | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/ac/kr/deu/connect/luck/image/ImageUploadResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package ac.kr.deu.connect.luck.image; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
|
||
@Getter | ||
@Setter | ||
@ToString | ||
public class ImageUploadResponse { | ||
private Data data; | ||
private boolean success; | ||
private int status; | ||
|
||
@Getter | ||
@Setter | ||
@ToString | ||
public static class Data { | ||
private String id; | ||
private String title; | ||
private String urlViewer; | ||
private String url; | ||
private String displayUrl; | ||
private String width; | ||
private String height; | ||
private String size; | ||
private String time; | ||
private String expiration; | ||
private ImageInfo image; | ||
private ImageInfo thumb; | ||
private ImageInfo medium; | ||
private String deleteUrl; | ||
} | ||
|
||
@Getter | ||
@Setter | ||
@ToString | ||
public static class ImageInfo { | ||
private String filename; | ||
private String name; | ||
private String mime; | ||
private String extension; | ||
private String url; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/ac/kr/deu/connect/luck/image/ImageUploader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package ac.kr.deu.connect.luck.image; | ||
|
||
import ac.kr.deu.connect.luck.exception.CustomErrorCode; | ||
import ac.kr.deu.connect.luck.exception.CustomException; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.LinkedMultiValueMap; | ||
import org.springframework.util.MultiValueMap; | ||
import org.springframework.web.client.RestTemplate; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.util.Base64; | ||
|
||
@Component | ||
@Slf4j | ||
public class ImageUploader { | ||
|
||
@Value("${imgbb.api-key}") | ||
private String API_KEY; | ||
|
||
private String BASE_URL = "https://api.imgbb.com/1/upload"; | ||
|
||
private Long EXPIRATION = 15552000L; | ||
|
||
private RestTemplate restTemplate = new RestTemplate(); | ||
|
||
|
||
public ImageUploadResponse uploadImage(MultipartFile file) { | ||
try { | ||
byte[] image = file.getBytes(); | ||
String base64Image = Base64.getEncoder().encodeToString(image); | ||
|
||
String apiUrl = String.format("%s?key=%s&expiration=%d", BASE_URL, API_KEY, EXPIRATION); | ||
|
||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); | ||
|
||
MultiValueMap<String, String> bodyMap = new LinkedMultiValueMap<>(); | ||
bodyMap.add("image", base64Image); | ||
|
||
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(bodyMap, headers); | ||
|
||
ResponseEntity<ImageUploadResponse> response = restTemplate.postForEntity(apiUrl, request, ImageUploadResponse.class); | ||
|
||
log.info("Image upload response: {}", response.getBody()); | ||
log.info("Image upload URL: {}", response.getBody().getData().getUrl()); | ||
return response.getBody(); | ||
} catch (Exception e) { | ||
log.error("Image upload failed: {}", e.getMessage()); | ||
throw new CustomException(CustomErrorCode.IMAGE_UPLOAD_FAILED); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/test/java/ac/kr/deu/connect/luck/image/ImageUploaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package ac.kr.deu.connect.luck.image; | ||
|
||
import ac.kr.deu.connect.luck.image.ImageUploader; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.mock.web.MockMultipartFile; | ||
|
||
import java.io.FileInputStream; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
|
||
@SpringBootTest | ||
class ImageUploaderTest { | ||
|
||
@Autowired | ||
private ImageUploader imageUploader; | ||
|
||
@Test | ||
void uploadImage() throws Exception { | ||
// Given | ||
final String filePath = "src/test/resources/test.jpg"; | ||
FileInputStream fis = new FileInputStream(filePath); | ||
MockMultipartFile file = new MockMultipartFile("file", fis); | ||
|
||
// When | ||
ImageUploadResponse imageUrl = imageUploader.uploadImage(file); | ||
|
||
// Then | ||
assertEquals(200, imageUrl.getStatus()); | ||
assertNotNull(imageUrl.getData().getUrl()); | ||
} | ||
|
||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.